直接结构图

数据表
CREATE TABLE `user` (
`id` int(32) NOT NULL AUTO_INCREMENT,
`user_name` varchar(32) NOT NULL,
`pass_word` varchar(50) NOT NULL,
`real_name` varchar(32) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
CREATE TABLE `t_userinfo` (
`ID` int(11) unsigned NOT NULL AUTO_INCREMENT,
`UserName` varchar(50) DEFAULT NULL,
`PassWord` varchar(50) DEFAULT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8
pom.xm文件内容
1<?xml version="1.0" encoding="UTF-8"?> 2<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 <parent> 6 <groupId>org.springframework.boot</groupId> 7 <artifactId>spring-boot-starter-parent</artifactId> 8 <version>2.2.5.RELEASE</version> 9 <relativePath/> <!-- lookup parent from repository --> 10 </parent> 11 <groupId>com</groupId> 12 <artifactId>example</artifactId> 13 <version>0.0.1-SNAPSHOT</version> 14 <name>LinMyBatis</name> 15 <description>demo project for Spring Boot</description> 16 17 <properties> 18 <java.version>1.8</java.version> 19 </properties> 20 21 <dependencies> 22 <dependency> 23 <groupId>org.springframework.boot</groupId> 24 <artifactId>spring-boot-starter-web</artifactId> 25 </dependency> 26 <dependency> 27 <groupId>org.springframework.boot</groupId> 28 <artifactId>spring-boot-starter-web-services</artifactId> 29 </dependency> 30 <dependency> 31 <groupId>mysql</groupId> 32 <artifactId>mysql-connector-java</artifactId> 33 </dependency> 34 <dependency> 35 <groupId>org.mybatis.spring.boot</groupId> 36 <artifactId>mybatis-spring-boot-starter</artifactId> 37 <version>2.1.1</version> 38 </dependency> 39 <dependency> 40 <groupId>org.springframework.boot</groupId> 41 <artifactId>spring-boot-starter-test</artifactId> 42 <scope>test</scope> 43 <exclusions> 44 <exclusion> 45 <groupId>org.junit.vintage</groupId> 46 <artifactId>junit-vintage-engine</artifactId> 47 </exclusion> 48 </exclusions> 49 </dependency> 50 </dependencies> 51 52 <build> 53 <plugins> 54 <plugin> 55 <groupId>org.springframework.boot</groupId> 56 <artifactId>spring-boot-maven-plugin</artifactId> 57 </plugin> 58 </plugins> 59 </build> 60 61</project>
配置数据库
src/main/resources/application.properties
1spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver 2spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC 3spring.datasource.username=root 4spring.datasource.password= 5 6mybatis.mapper-locations=classpath:mapping/*Mapper.xml 7mybatis.type-aliases-package=com.example.entity
1.编写实体代码
src/main/java/com/example/entity/User.java
1package com.example.entity; 2 3 4public class User 5{ 6 private Integer id; 7 private String userName; 8 private String passWord; 9 private String realName; 10 public Integer getId() { 11 return id; 12 } 13 public void setId(Integer id) { 14 this.id = id; 15 } 16 public String getUserName() { 17 return userName; 18 } 19 public void setUserName(String userName) { 20 this.userName = userName; 21 } 22 public String getPassWord() { 23 return passWord; 24 } 25 public void setPassWord(String passWord) { 26 this.passWord = passWord; 27 } 28 public String getRealName() { 29 return realName; 30 } 31 public void setRealName(String realName) { 32 this.realName = realName; 33 } 34 35 public String toString() 36 { 37 return "User:{id="+id+",userName="+userName+",passWord="+passWord+",realName="+realName+"}"; 38 } 39}
2.编写请求的控制器名
src/main/java/com/example/controller/UserController.java
1package com.example.controller; 2 3import java.util.List; 4 5import org.springframework.beans.factory.annotation.Autowired; 6import org.springframework.web.bind.annotation.PathVariable; 7import org.springframework.web.bind.annotation.RequestBody; 8import org.springframework.web.bind.annotation.RequestMapping; 9import org.springframework.web.bind.annotation.RequestMethod; 10import org.springframework.web.bind.annotation.RestController; 11 12import com.example.entity.User; 13import com.example.service.UserService; 14 15@RestController 16@RequestMapping("/user") 17public class UserController 18{ 19 @Autowired 20 private UserService userService; 21 /** 22 * 查找单一记录 23 * @param id 24 * @return 25 */ 26 @RequestMapping("findOne/{id}") 27 public String findOne(@PathVariable int id) 28 { 29 return userService.findOne(id).toString(); 30 } 31 32 @RequestMapping("getDataMultiTable/{id}") 33 public List<Object> getDataMultiTable(@PathVariable int id) 34 { 35 return userService.getDataMultiTable(id); 36 } 37 38 /** 39 * 查找所有 40 * @return 41 */ 42 @RequestMapping("findAll") 43 public List<User> findAll() 44 { 45 return userService.findAll(); 46 } 47 48 /** 49 * 增加数据 50 * @param args 51 * @return 52 */ 53 @RequestMapping(value="addData",method = RequestMethod.POST) 54 public int addData(@RequestBody User args) 55 { 56 User user = new User(); 57 user.setUserName(args.getUserName()); 58 user.setPassWord(args.getPassWord()); 59 user.setRealName(args.getRealName()); 60 System.out.println(args); 61 return userService.addData(user); 62 } 63 64 /** 65 * 删除指定记录 66 * @param id 67 * @return 68 */ 69 @RequestMapping("deleteUser/{id}") 70 public int deleteUser(@PathVariable int id) 71 { 72 return userService.deleteData(id); 73 } 74 75 /** 76 * 更新数据 77 * @param args 78 * @return 79 */ 80 @RequestMapping(value="updateData",method=RequestMethod.POST) 81 public int updateData(@RequestBody User args) 82 { 83 User user = new User(); 84 user.setId(args.getId()); 85 user.setRealName(args.getRealName()); 86 return userService.updateData(user); 87 } 88 89 90}
3.编写请求的服务
1package com.example.service; 2 3import java.util.List; 4 5import org.springframework.beans.factory.annotation.Autowired; 6import org.springframework.stereotype.Service; 7 8import com.example.entity.User; 9import com.example.mapper.UserMapper; 10 11@Service 12public class UserService 13{ 14 @Autowired 15 UserMapper userMapper; 16 public User findOne(int id) 17 { 18 return userMapper.findOne(id); 19 } 20 21 public List<User> findAll() 22 { 23 return userMapper.findAll(); 24 } 25 26 public int deleteData(int id) 27 { 28 return userMapper.deleteData(id); 29 } 30 31 public int addData(User user) 32 { 33 return userMapper.addData(user); 34 } 35 36 public int updateData(User user) 37 { 38 return userMapper.updateData(user); 39 } 40 41 public List<Object> getDataMultiTable(int id) 42 { 43 return userMapper.getDataMultiTable(id); 44 } 45}
4.服务映射
src/main/java/com/example/mapper/UserMapper.java
1package com.example.mapper; 2 3import java.util.List; 4 5import org.springframework.stereotype.Repository; 6 7import com.example.entity.User; 8 9@Repository 10public interface UserMapper 11{ 12 User findOne(int id); 13 14 List<User> findAll(); 15 16 int deleteData(int id); 17 18 int addData(User user); 19 20 int updateData(User user); 21 22 List<Object> getDataMultiTable(int id); 23}
5.编写增删改查
src/main/resources/mapping/UserMapper.xml
1<?xml version="1.0" encoding="UTF-8"?> 2<!DOCTYPE mapper PUBLIC "-//mybatis.org/DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 3<mapper namespace="com.example.mapper.UserMapper"> 4 <resultMap id="BaseResultMap" type="com.example.entity.User"> 5 <result column="id" jdbcType="INTEGER" property="id" /> 6 <result column="user_name" jdbcType="VARCHAR" property="userName" /> 7 <result column="pass_word" jdbcType="VARCHAR" property="passWord" /> 8 <result column="real_name" jdbcType="VARCHAR" property="realName" /> 9 </resultMap> 10 11 <select id="findOne" resultType="com.example.entity.User"> 12 select * from user where id = #{id} 13 </select> 14 15 <select id="findAll" resultType="hashmap"> 16 select * from user 17 </select> 18 19 20 <select id="getDataMultiTable" resultType="hashmap"> 21 select u.*,tu.id as tu_id,tu.UserName as tu_username 22 from user as u join t_userinfo as tu on u.id = tu.id 23 where u.id > #{id} limit 1 24 </select> 25 26 <insert id="addData" keyColumn="id" useGeneratedKeys="true" keyProperty="id" parameterType="User"> 27 insert into user (user_name,pass_word,real_name) values (#{userName},#{passWord},#{realName}) 28 </insert> 29 30 <delete id="deleteData"> 31 delete from user where id = #{id} 32 </delete> 33 34 <update id="updateData" keyColumn="id" useGeneratedKeys="true" keyProperty="id" parameterType="User"> 35 update user set real_name=#{realName} where id = #{id} 36 </update> 37</mapper>
请求结果如下
1.获取所有数据

删除指定数据

修改指定的数据

新增数据

参考文档与代码
https://www.w3cschool.cn/mybatis/f4uw1ilx.html