SpringBoot整合MyBatis的步骤
第1步 导入MyBatis 整合包
<!-- Mybatis 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.1.1</version>
</dependency>
第2步 编写接口和xml
目录结构, 注意 mapper
包和 resources 目录下的 /mybatis/mapper
目录
第一 在 com/pro/ 目录新增 mapper 包
然后在 com/pro/mapper 包下新增 UsersMapper 接口
package com.pro.mapper;
import com.pro.pojo.Users;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
@Mapper
@Repository
public interface UsersMapper {
// 查询所有用户
List<Users> queryUserList();
Users queryUserById(int id);
int addUser(Users users);
int updateUser(Users users);
int deleteUser(int id);
}
第二 在 resources 目录下新增目录 /mybatis/mapper
然后在 resources/mybatis/mapper 目录新增 UsersMapper.xml 文件
注意 namespace
需要绑定接口
<?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.pro.mapper.UsersMapper">
<select id="queryUserList" resultType="Users">
select * from users
</select>
<select id="queryUserById" parameterType="int" resultType="Users">
select * from users where id = #{id}
</select>
<insert id="addUser" parameterType="Users">
insert into users(name, pwd) values(#{name},#{pwd})
</insert>
<update id="updateUser" parameterType="Users">
update users set name = #{name}, pwd = #{pwd} where id = #{id}
</update>
<delete id="deleteUser" parameterType="int">
delete from users where id = #{id}
</delete>
</mapper>
第3步 编写配置文件 application.properties
注意 因为我把 xml 写在了 resources 目录下了, 所以这里 mybatis.mapper-locations 属性对应了这个路径 classpath:mybatis/mapper/*.xml
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# 整合 MyBatis 配置
# 别名
mybatis.type-aliases-package=com.pro.pojo
# 绑定 mapper.xml
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
第4步 编写 controller 测试使用
在 com/pro/ 目录新增 controller 包, 编写 UsersController 类进行测试
为了方便测试, 这里就没有写 services 层了, 直接在 controller 层调用了
package com.pro.controller;
import com.pro.mapper.UsersMapper;
import com.pro.pojo.Users;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class UsersController {
@Autowired
private UsersMapper usersMapper;
@GetMapping("/user/list")
public List<Users> queryUserList() {
List<Users> users = usersMapper.queryUserList();
return users;
}
@GetMapping("/user/add")
public Object addUser() {
Users users = new Users(null, "ControllerAddUser", "add123");
int i = usersMapper.addUser(users);
if (i > 0) {
return "新增成功";
}
return "新增失败";
}
@GetMapping("/user/{id}")
public Object deleteUser(@PathVariable("id") int id) {
int i = usersMapper.deleteUser(id);
if (i > 0) {
return "删除成功";
}
return "删除失败";
}
}
启动项目, 测试访问 http://localhost:8080/user/list
成功
所有依赖
<!--web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Mybatis 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.1.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--数据库驱动-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>