14.1 整合 Mybatis
其实 也可以说是 Mybatis 整合了 SpringBoot,哈哈。开个玩笑 ~
MyBatis Spring Boot Starter
- 导入 依赖
<!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
2. 写对应的 pojo 类
package top.muquanyu.pojo;
public class User {
private Integer id;
private String name;
private String pwd;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public User(){}
public User(Integer id, String name, String pwd) {
this.id = id;
this.name = name;
this.pwd = pwd;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", pwd='" + pwd + '\'' +
'}';
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
}
- 编写 Mapper
package top.muquanyu.mapper;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import top.muquanyu.pojo.User;
import java.util.List;
// 表示 这个类 是 mybatis 的 mapper 类
@Mapper
@Repository // Dao 层
public interface UserMapper {
List<User> getList();
User getUserByID(Integer id);
int addUser(User user);
int updateUser(User user);
int deleteUser(Integer id);
}
这个地方 核心的 注解是不能不写的。
@Mapper
:如果不写它,那么 就 无法 自动的 匹配 Mapper.xml 实现 注册 一个 Mapper 的效果。
@Repository
:如果不写它,那么 ICO 容器里面 就没有 这个 bean,那么我们 怎么 样 才能直接的 拿到 这个 东西 去用呢??难道 还要 新建一个 吗?好像 也可以。。但是 没那个必要!!!
- 建议把 xml 文件 写到 resource 下面
<?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="top.muquanyu.mapper.UserMapper">
<select id="getList" resultType="User">
select * from user
</select>
<select id="getUserByID" resultType="User" parameterType="int">
select * from user where id = #{id}
</select>
<insert id="addUser" parameterType="top.muquanyu.pojo.User">
insert into user(id,name,pwd) values (#{id},#{name},#{pwd})
</insert>
<update id="updateUser" parameterType="top.muquanyu.pojo.User">
update user set name = #{name}, pwd = #{pwd} where id = #{id}
</update>
<delete id="deleteUser" parameterType="int">
delete from user where id = #{id}
</delete>
</mapper>
- 利用
application.yaml
整合 Mybatis
spring:
datasource:
username: root
password: 123123
url: jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
type-aliases-package: top.muquanyu.pojo
mapper-locations: classpath:mybatis/mapper/*xml # classpath 就是 resources
- 编写 Controller
package top.muquanyu.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.muquanyu.mapper.UserMapper;
import top.muquanyu.pojo.User;
import java.util.List;
@RestController
public class UserController {
@Autowired
private UserMapper userMapper;
@RequestMapping("userList")
public List<User> userList(){
List<User> list = userMapper.getList();
return list;
}
@RequestMapping("addUser/{id}")
public String addUser(@PathVariable("id") Integer id){
User user = new User(id, "test", "test123");
userMapper.addUser(user);
return "add_ok!";
}
@RequestMapping("delete/{id}")
public String deleteUser(@PathVariable("id") Integer id){
userMapper.deleteUser(id);
return "delete_ok!";
}
@RequestMapping("update/{id}")
public String userList(@PathVariable("id") Integer id){
User user = new User(id, "修改后的名字" + id.toString(), "修改后的密码" + id.toString());
userMapper.updateUser(user);
return "update_ok!";
}
}