开发工具:Intellij IDEA 2020.2.1
springboot:2.4.3
jdk:1.8.0_251
maven:3.6.3
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>2.1.1version>
dependency>
目录结构, 注意 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
需要绑定接口
<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>
注意 因为我把 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
在 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
成功
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>2.1.1version>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-jdbcartifactId>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<scope>runtimescope>
dependency>