赞
踩
整体想法:因为想学习vuejs,前期准备一个能运行的springboot后台,具备增删改查功能,要做的东西是:在家里时间长了,总是记不住东西放哪了,所以我想做的是一个简单的“忘了”系统,有user表,可以记录用户的信息,有house表,可以记录用户家里不同的房间信息,有personal表可以记录具体到某个房间、某个位置,存了什么东西
目的:希望做完这些,我还没放弃能接着学习vuejs ^_^
DB:目前能想到的表关系
User表
CREATE TABLE `user` (
`id` int NOT NULL AUTO_INCREMENT,
`username` varchar(45) DEFAULT NULL,
`password` varchar(45) DEFAULT NULL,
`age` varchar(45) DEFAULT NULL,
`sex` varchar(45) DEFAULT NULL,
`tel` varchar(45) DEFAULT NULL,
`picture` varchar(45) DEFAULT NULL,
`createDate` varchar(45) DEFAULT NULL,
`updateDate` varchar(45) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
house表
CREATE TABLE `house` (
`id` int NOT NULL AUTO_INCREMENT,
`user_id` int NOT NULL,
`room` varchar(45) NOT NULL,
`createDate` varchar(45) DEFAULT NULL,
`updateDate` varchar(45) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `user_id_idx` (`user_id`),
CONSTRAINT `user_id` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
personal表
CREATE TABLE `personal` (
`id` int NOT NULL AUTO_INCREMENT,
`house_id` int NOT NULL,
`name` varchar(45) NOT NULL,
`location` varchar(45) NOT NULL,
`picture` varchar(45) DEFAULT NULL,
`createDate` varchar(45) DEFAULT NULL,
`updateDate` varchar(45) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `house_id_idx` (`house_id`),
CONSTRAINT `house_id` FOREIGN KEY (`house_id`) REFERENCES `house` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
1.File->New->Spring Starter Project
2.项目名、包名入力
3.如图选择
4.初始项目一览
5.给application.properties文件添加mysql配置信息,mysql数据库就用https://blog.csdn.net/charles542307299/article/details/121449246
docker做的mysql-dev容器
mybatis.config-location=classpath:mapper/mybatis-config.xml
mybatis.mapper-locations=classpath:mapper/*Mapper.xml
mybatis.type-aliases-package=com.sixdog.forget.entity
spring.datasource.url=jdbc:mysql://192.168.0.102:3307/forget
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driverClassName =com.mysql.cj.jdbc.Driver
6.先从用户做起,当前主体文件结构
7.开始填肉,实体类User.java
package com.sixdog.forget.entity; import java.io.Serializable; public class User implements Serializable { /** * serialVersionUID */ private static final long serialVersionUID = 1L; private String id; private String userName; private String passWord; private String age; private String sex; private String tel; private String picture; public User() { super(); } public User(String userName, String passWord) { super(); this.userName = userName; this.passWord = passWord; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassWord() { return passWord; } public void setPassWord(String passWord) { this.passWord = passWord; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getTel() { return tel; } public void setTel(String tel) { this.tel = tel; } public String getPicture() { return picture; } public void setPicture(String picture) { this.picture = picture; } @Override public String toString() { return "User [id=" + id + ", userName=" + userName + ", passWord=" + passWord + ", age=" + age + ", sex=" + sex + ", tel=" + tel + ", picture=" + picture + "]"; } }
8.UserController控制器
package com.sixdog.forget.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.sixdog.forget.entity.User; import com.sixdog.forget.service.UserService; @RestController @RequestMapping("/forget") public class UserController { @Autowired private UserService userService; @RequestMapping("/getAllUser") public List<User> getAllUser(){ List<User> list = userService.getAllUser(); return list; } @RequestMapping("/saveUser") public void saveUser(User user){ userService.saveUser(user); } @RequestMapping("/deleteUserById") public void deleteUserById(Long id){ userService.deleteUserById(id); } @RequestMapping("/updateUser") public void updateUser(Long id,String userName,String passWord,String age,String sex,String tel,String picture){ userService.updateUser(id, userName, passWord,age,sex,tel,picture); } }
9.UserService
package com.sixdog.forget.service; import java.util.List; import com.sixdog.forget.entity.User; public interface UserService { public List<User> getAllUser(); public void saveUser(User user); public void deleteUserById(Long id); public void updateUser(Long id, String userName, String passWord, String age, String sex, String tel, String picture); }
10.UserServiceImpl
package com.sixdog.forget.service.impl; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.sixdog.forget.entity.User; import com.sixdog.forget.mapper.UserMapper; import com.sixdog.forget.service.UserService; @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public List<User> getAllUser() { List<User> list = userMapper.getAll(); return list; } @Override public void saveUser(User user) { userMapper.saveUser(user); } @Override public void deleteUserById(Long id) { userMapper.deleteUserById(id); } @Override public void updateUser(Long id, String userName, String passWord, String age, String sex, String tel, String picture) { userMapper.updateUser(id, userName, passWord, age, sex, tel, picture); } }
11.UserMapper
package com.sixdog.forget.mapper; import java.util.List; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import com.sixdog.forget.entity.User; @Mapper public interface UserMapper { public List<User> getAll(); public void saveUser(User user); public void deleteUserById(Long id); public void updateUser(@Param("id") Long id, @Param("userName") String userName, @Param("passWord") String passWord, @Param("age") String age, @Param("sex") String sex, @Param("tel") String tel, @Param("picture") String picture); }
12.resources/mapper/mybatis-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
</configuration>
13.resources/mapper/UserMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.sixdog.forget.mapper.UserMapper"> <select id="getAll" resultType="user"> select * from user </select> <insert id="saveUser" useGeneratedKeys="true" keyProperty="id"> insert into user( username ,password ,age ,sex ,tel ,picture ,createDate ) values( #{userName} ,#{passWord} ,#{age} ,#{sex} ,#{tel} ,#{picture} ,curdate() ); </insert> <delete id="deleteUserById"> delete from user where id=#{id} </delete> <update id="updateUser"> update user set username = #{userName} ,password = #{passWord} ,age = #{age} ,sex = #{sex} ,tel = #{tel} ,picture = #{picture} ,updateDate = curdate() where id = #{id} </update> </mapper>
14.命令行窗口进入项目路径下,执行:./mvnw spring-boot:run
启动服务,访问http://localhost:8080/forget/saveUser?userName=段子羽&passWord=666666&age=18&sex=男&tel=13478998888
数据库确认:
15.插入结果确认
http://localhost:8080/forget/getAllUser
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。