直接结构图
数据表
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文件内容
<?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</groupId>
<artifactId>example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>LinMyBatis</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-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</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>
配置数据库
src/main/resources/application.properties
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=
mybatis.mapper-locations=classpath:mapping/*Mapper.xml
mybatis.type-aliases-package=com.example.entity
1.编写实体代码
src/main/java/com/example/entity/User.java
package com.example.entity;
public class User
{
private Integer id;
private String userName;
private String passWord;
private String realName;
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 getRealName() {
return realName;
}
public void setRealName(String realName) {
this.realName = realName;
}
public String toString()
{
return "User:{id="+id+",userName="+userName+",passWord="+passWord+",realName="+realName+"}";
}
}
2.编写请求的控制器名
src/main/java/com/example/controller/UserController.java
package com.example.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.example.entity.User;
import com.example.service.UserService;
@RestController
@RequestMapping("/user")
public class UserController
{
@Autowired
private UserService userService;
/**
* 查找单一记录
* @param id
* @return
*/
@RequestMapping("findOne/{id}")
public String findOne(@PathVariable int id)
{
return userService.findOne(id).toString();
}
@RequestMapping("getDataMultiTable/{id}")
public List<Object> getDataMultiTable(@PathVariable int id)
{
return userService.getDataMultiTable(id);
}
/**
* 查找所有
* @return
*/
@RequestMapping("findAll")
public List<User> findAll()
{
return userService.findAll();
}
/**
* 增加数据
* @param args
* @return
*/
@RequestMapping(value="addData",method = RequestMethod.POST)
public int addData(@RequestBody User args)
{
User user = new User();
user.setUserName(args.getUserName());
user.setPassWord(args.getPassWord());
user.setRealName(args.getRealName());
System.out.println(args);
return userService.addData(user);
}
/**
* 删除指定记录
* @param id
* @return
*/
@RequestMapping("deleteUser/{id}")
public int deleteUser(@PathVariable int id)
{
return userService.deleteData(id);
}
/**
* 更新数据
* @param args
* @return
*/
@RequestMapping(value="updateData",method=RequestMethod.POST)
public int updateData(@RequestBody User args)
{
User user = new User();
user.setId(args.getId());
user.setRealName(args.getRealName());
return userService.updateData(user);
}
}
3.编写请求的服务
package com.example.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.entity.User;
import com.example.mapper.UserMapper;
@Service
public class UserService
{
@Autowired
UserMapper userMapper;
public User findOne(int id)
{
return userMapper.findOne(id);
}
public List<User> findAll()
{
return userMapper.findAll();
}
public int deleteData(int id)
{
return userMapper.deleteData(id);
}
public int addData(User user)
{
return userMapper.addData(user);
}
public int updateData(User user)
{
return userMapper.updateData(user);
}
public List<Object> getDataMultiTable(int id)
{
return userMapper.getDataMultiTable(id);
}
}
4.服务映射
src/main/java/com/example/mapper/UserMapper.java
package com.example.mapper;
import java.util.List;
import org.springframework.stereotype.Repository;
import com.example.entity.User;
@Repository
public interface UserMapper
{
User findOne(int id);
List<User> findAll();
int deleteData(int id);
int addData(User user);
int updateData(User user);
List<Object> getDataMultiTable(int id);
}
5.编写增删改查
src/main/resources/mapping/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.example.mapper.UserMapper">
<resultMap id="BaseResultMap" type="com.example.entity.User">
<result column="id" jdbcType="INTEGER" property="id" />
<result column="user_name" jdbcType="VARCHAR" property="userName" />
<result column="pass_word" jdbcType="VARCHAR" property="passWord" />
<result column="real_name" jdbcType="VARCHAR" property="realName" />
</resultMap>
<select id="findOne" resultType="com.example.entity.User">
select * from user where id = #{id}
</select>
<select id="findAll" resultType="hashmap">
select * from user
</select>
<select id="getDataMultiTable" resultType="hashmap">
select u.*,tu.id as tu_id,tu.UserName as tu_username
from user as u join t_userinfo as tu on u.id = tu.id
where u.id > #{id} limit 1
</select>
<insert id="addData" keyColumn="id" useGeneratedKeys="true" keyProperty="id" parameterType="User">
insert into user (user_name,pass_word,real_name) values (#{userName},#{passWord},#{realName})
</insert>
<delete id="deleteData">
delete from user where id = #{id}
</delete>
<update id="updateData" keyColumn="id" useGeneratedKeys="true" keyProperty="id" parameterType="User">
update user set real_name=#{realName} where id = #{id}
</update>
</mapper>
请求结果如下
1.获取所有数据
删除指定数据
修改指定的数据
新增数据
参考文档与代码
https://www.w3cschool.cn/mybatis/f4uw1ilx.html