一个简单的增删改查Spring boot项目教程(完整过程,附代码)(从搭建数据库到实现增删改查功能),Springboot学习,Springboot项目,
这���将会介绍怎么去搭建一个简单增删改查的Springboot项目,认真看完我相信你一定能够学会,并且附有完整代码;
目录
- 一、搭建数据库
- 1.建数据库
- 2.建表
- 二、新建IDEA项目
- 三、连接数据库
- 1.编写文件
- 2.连接MySQL
- 四、写代码
- pojo层
- mapper层
- Service层
- Controller层
- 写mapper层的映射文件
- 五、测试
- Apifox测试结果(没Apifox直接跳过)
- 网页测试
- 六、完整代码
- UserController
- UserMapper
- User
- UserService
- UserMapper.xml
- application.properties
- pom.xml
一、搭建数据库
首先要进行增删改查肯定是要有供操作的数据库;
这里我是用的SQLyog来搭建的,随便用什么都可以,只要能确保给项目一个配套的数据库就行;
1.建数据库
CREATE DATABASE springBoot;
2.建表
CREATE TABLE USER( NAME VARCHAR(30), id INT(11), sex VARCHAR(10) ) DEFAULT CHARSET=utf8
二、新建IDEA项目
打开IDEA,创建一个新项目,准备开始动手
然后选择插件
点击创建
在pom.xml里导入Lombok依赖
org.projectlombok lombok
然后搭建四层架构
controller,mapper,pojo,service
创建mapper的映射文件
三、连接数据库
1.编写文件
在resources包下找到application.properties,将如下代码写入:
spring.datasource.username=root spring.datasource.password=admin spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver #??mybatis mybatis.type-aliases-package=com.example.springboot.pojo mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
2.连接MySQL
选择我们之前建好的数据库
点击Apply和OK后,我们就可以看到我们连接了数据库
四、写代码
经过前面的准备工作后终于可以开始写代码了;
这里我就只介绍一个接口(查找所有用户),其他的都是差不多的流程,文章末尾附完整代码;
pojo层
实体类里的属性要与数据表一一对应;
mapper层
查询所有用户是要返回多个User,所以用List;
Service层
先用@Autowired注入userMapper;
然后声明方法;
Controller层
同样注入
然后调用Service层
写mapper层的映射文件
将查询的SQL语句写上
到此这个接口就算是写完了;
五、测试
我建议是拿Apifox来测试,自己拿网页测试也行;
先手动添加一些数据到数据库里去;
Apifox测试结果(没Apifox直接跳过)
网页测试
注:一定要先让项目保持运行状态
打开浏览器,访问接口路径:
http://localhost:8080/queryUserList
六、完整代码
UserController
package com.example.springboot.controller; import com.example.springboot.pojo.User; import com.example.springboot.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController public class UserController { @Autowired UserService userService; @GetMapping ("/queryUserList") public List queryUserList(){ return userService.queryUserList(); } @GetMapping ("queryUserById") public User queryUserById(int user_id){ return userService.queryUserById(user_id); } @GetMapping("/addUser") public String addUser(User user){ return userService.addUser(user); } @GetMapping("/deleteUserById") public String deleteUserById(int id){ return userService.deleteUserById(id); } }
UserMapper
package com.example.springboot.mapper; import com.example.springboot.pojo.User; import org.apache.ibatis.annotations.Mapper; import org.springframework.stereotype.Repository; import java.util.List; @Mapper @Repository public interface UserMapper { List queryUserList(); User queryUserById(int id); void addUser(User user); void deleteUserById(int id); }
User
package com.example.springboot.pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @NoArgsConstructor @AllArgsConstructor public class User { private String name; private int id; private String sex; }
UserService
package com.example.springboot.service; import com.example.springboot.mapper.UserMapper; import com.example.springboot.pojo.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class UserService { @Autowired UserMapper userMapper; public List queryUserList(){ return userMapper.queryUserList(); } public User queryUserById(int id){ User user= userMapper.queryUserById(id); return user; } public String addUser(User user){ userMapper.addUser(user); return "新增成功"; } public String deleteUserById(int id){ userMapper.deleteUserById(id); return "删除成功"; } }
UserMapper.xml
select * from springboot.user select * from springboot.user where id = #{id}; insert into springboot.user (name, id, sex) VALUES (#{name},#{id},#{sex}) delete from springboot.user where id=#{id}
application.properties
spring.datasource.username=root spring.datasource.password=admin spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver #??mybatis mybatis.type-aliases-package=com.example.springboot.pojo mybatis.mapper-locations=classpath:mybatis/mapper/*.xml server.port=8080
pom.xml
4.0.0 com.example springBoot 0.0.1-SNAPSHOT springBoot springBoot 1.8 UTF-8 UTF-8 2.6.13 org.mybatis.spring.boot mybatis-spring-boot-starter 2.1.1 org.projectlombok lombok org.springframework.boot spring-boot-starter-jdbc org.springframework.boot spring-boot-starter-web com.mysql mysql-connector-j runtime org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-dependencies ${spring-boot.version} pom import org.apache.maven.plugins maven-compiler-plugin 3.8.1 1.8 1.8 UTF-8 org.springframework.boot spring-boot-maven-plugin ${spring-boot.version} com.example.springboot.Application true repackage repackage
The End