SpringBoot+Mybatis实现增删改差(Maven项目)
总结:
- 编写数据库
- 配置文件信息.yml/.properties
- 实体类
- dao接口
- Service层
- Controller层
- 编写mybatis .xml sql语句
- postman接口测试
一、创建数据库文件
/* Navicat Premium Data Transfer Source Server : 本地localhost Source Server Type : MySQL Source Server Version : 50713 Source Host : localhost:3306 Source Schema : springboot Target Server Type : MySQL Target Server Version : 50713 File Encoding : 65001 Date: 04/03/2020 15:45:41 */ SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- -- Table structure for user -- ---------------------------- DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, `password` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, `address` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic; SET FOREIGN_KEY_CHECKS = 1;
项目配置文件信息.yml
server: port: 8080 spring: datasource: username: root password: root url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC driver-class-name: com.mysql.cj.jdbc.Driver mybatis: mapper-locations: classpath:mapping/*Mapper.xml type-aliases-package: com.example.entity.demo logging: level: com: example: mapper : debug
项目依赖jar包:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
二、编写实体类
package com.example.demo.entity; public class User { private Integer id; private String username; private String password; private String address; private String is_deleted; public User(Integer id, String username, String password, String address, String is_deleted) { this.id = id; this.username = username; this.password = password; this.address = address; this.is_deleted = is_deleted; } public User() { } @Override public String toString() { return "User{" + "id=" + id + ", username='" + username + '\'' + ", password='" + password + '\'' + ", address='" + address + '\'' + ", is_deleted='" + is_deleted + '\'' + '}'; } public Integer getId() { return id; } public void setId(Integer 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 getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getIs_deleted() { return is_deleted; } public void setIs_deleted(String is_deleted) { this.is_deleted = is_deleted; } }
三、编写dao层接口
package com.example.demo.mapper; import com.example.demo.entity.User; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; import org.springframework.stereotype.Repository; @Repository public interface UserMapper { User Sel(@Param("user")User user); int Add(@Param("user")User user); int Update(@Param("user")User user); int Delete(@Param("user")User user); }
四、编写Service层
package com.example.demo.service; import com.example.demo.entity.User; import com.example.demo.mapper.UserMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class UserService { @Autowired UserMapper userMapper; public User Sel(User user) { return userMapper.Sel(user); } public String Add(User user) { int a = userMapper.Add(user); if (a == 1) { return "添加成功"; } else { return "添加失败"; } } public String Update(User user) { int a = userMapper.Update(user); if (a == 1) { return "修改成功"; } else { return "修改失败"; } } public String Delete(User user) { int a = userMapper.Delete(user); if (a == 1) { return "删除成功"; } else { return "删除失败"; } } }
五、编写Controller层
package com.example.demo.controller; import com.example.demo.entity.User; import com.example.demo.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.web.bind.annotation.*; import javax.validation.Valid; @RestController @RequestMapping("/test") public class UserController { @Autowired private UserService userService; @PostMapping(value = "/selectUserByid", produces = "application/json;charset=UTF-8") public String GetUser(@Valid @RequestBody User user) { return userService.Sel(user).toString(); } @PostMapping(value = "/add", produces = "application/json;charset=UTF-8") public String Add(@Valid @RequestBody User user) { return userService.Add(user); } @PostMapping(value = "/update", produces = "application/json;charset=UTF-8") public String Update(@Valid @RequestBody User user) { return userService.Update(user); } @PostMapping(value = "/delete", produces = "application/json;charset=UTF-8") public String Delete(@Valid @RequestBody User user) { return userService.Delete(user); } }
六、编写项目启动类
package com.example.demo; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @MapperScan("com.example.demo.mapper") //扫描的mapper @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
七、编写mybatis .xml sql语句
<?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.example.demo.mapper.UserMapper"> <resultMap id="BaseResultMap" type="com.example.demo.entity.User"> <result column="id" jdbcType="INTEGER" property="id"/> <result column="userName" jdbcType="VARCHAR" property="username"/> <result column="passWord" jdbcType="VARCHAR" property="password"/> <result column="address" jdbcType="VARCHAR" property="address"/> <result column="is_deleted" jdbcType="VARCHAR" property="is_deleted"/> </resultMap> <select id="Sel" resultType="com.example.demo.entity.User"> select * from user where is_deleted='0' <if test="user.id != null"> AND id = #{user.id} </if> </select> <insert id="Add" parameterType="com.example.demo.entity.User"> INSERT INTO user values(null, <if test="user.username!=null and user.username!='' "> #{user.username}, </if> <if test="user.password!=null and user.password!='' "> #{user.password}, </if> <if test="user.address!=null and user.address!='' "> #{user.address}, </if> 0) </insert> <update id="Update" parameterType="com.example.demo.entity.User"> UPDATE user <set> <if test="user.username != null and user.username != ''"> username = #{user.username}, </if> <if test="user.password != null and user.password != ''"> password = #{user.password}, </if> <if test="user.address != null and user.address != ''"> address = #{user.address}, </if> </set> WHERE id=#{user.id} and is_deleted='0' </update> <delete id="Delete" parameterType="com.example.demo.entity.User"> update user set is_deleted='1' WHERE id = #{user.id} </delete> </mapper>
八、postman测试CRUD接口
测试接口地址在controller层
测试URL:http://localhost:8080/test/selectUserByid

项目截图:
1.数据库:

2.项目架构



浙公网安备 33010602011771号