springboot2.2和mybatis2.1的增删改查
直接结构图
数据表
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"?>4.0.0 org.springframework.boot spring-boot-starter-parent 2.2.5.RELEASE com example 0.0.1-SNAPSHOT LinMyBatis demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-web-services mysql mysql-connector-java org.mybatis.spring.boot mybatis-spring-boot-starter 2.1.1 org.springframework.boot spring-boot-starter-test test org.junit.vintage junit-vintage-engine org.springframework.boot spring-boot-maven-plugin
配置数据库
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
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 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
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 findAll();
int deleteData(int id);
int addData(User user);
int updateData(User user);
List
5.编写增删改查
src/main/resources/mapping/UserMapper.xml
<?xml version="1.0" encoding="UTF-8"?>insert into user (user_name,pass_word,real_name) values (#{userName},#{passWord},#{realName}) delete from user where id = #{id} update user set real_name=#{realName} where id = #{id}
请求结果如下
1.获取所有数据
删除指定数据
修改指定的数据
新增数据
参考文档与代码
https://www.w3cschool.cn/mybatis/f4uw1ilx.html
https://blog.csdn.net/iku5200/article/details/82856621
https://blog.mybatis.org/