这里将会介绍怎么去搭建一个简单增删改查的Springboot项目,认真看完我相信你一定能够学会,并且附有完整代码;
首先要进行增删改查肯定是要有供操作的数据库;
这里我是用的SQLyog来搭建的,随便用什么都可以,只要能确保给项目一个配套的数据库就行;
CREATE DATABASE springBoot;
CREATE TABLE USER( NAME VARCHAR(30), id INT(11), sex VARCHAR(10) ) DEFAULT CHARSET=utf8
打开IDEA,创建一个新项目,准备开始动手
然后选择插件
点击创建
在pom.xml里导入Lombok依赖
org.projectlombok lombok
然后搭建四层架构
controller,mapper,pojo,service
创建mapper的映射文件
在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
选择我们之前建好的数据库
点击Apply和OK后,我们就可以看到我们连接了数据库
经过前面的准备工作后终于可以开始写代码了;
这里我就只介绍一个接口(查找所有用户),其他的都是差不多的流程,文章末尾附完整代码;
实体类里的属性要与数据表一一对应;
查询所有用户是要返回多个User,所以用List;
先用@Autowired注入userMapper;
然后声明方法;
同样注入
然后调用Service层
将查询的SQL语句写上
到此这个接口就算是写完了;
我建议是拿Apifox来测试,自己拿网页测试也行;
先手动添加一些数据到数据库里去;
注:一定要先让项目保持运行状态
打开浏览器,访问接口路径:
http://localhost:8080/queryUserList
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 ListqueryUserList(){ 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); } }
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 { ListqueryUserList(); User queryUserById(int id); void addUser(User user); void deleteUserById(int id); }
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; }
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 ListqueryUserList(){ 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 "删除成功"; } }
insert into springboot.user (name, id, sex) VALUES (#{name},#{id},#{sex}) delete from springboot.user where id=#{id}
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
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 UTF-8 org.springframework.boot spring-boot-maven-plugin ${spring-boot.version} com.example.springboot.Application true repackage repackage