整合MyBatis
- M:数据业务
- V:交接
- C:HTML
官方文档:http://mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/
Maven仓库地址:https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter/2.1.4
准备工作
- 1.导入依赖
- 2.配置文件
- 3.mybatis配置
- 4.编写SQL
- 5.业务层调用dao层
- 6.controller调用service层
创建项目springboot-06-mybatis ,导入依赖Spring Web
、JDBC API
、Mysql Driver
整合测试
1、导入 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.4</version>
</dependency>
官方的依赖,以spring开头
2、配置数据库连接信息(不变)
spring:
datasource:
username: root
password: root
#?serverTimezone=UTC解决时区的报错
url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
driver-class-name: com.mysql.cj.jdbc.Driver
3、测试数据库是否连接成功!
@SpringBootTest
class Springboot06MybatisApplicationTests {
@Autowired
DataSource dataSource;
@Test
void contextLoads() throws SQLException {
System.out.println(dataSource.getClass());
System.out.println(dataSource.getConnection());
}
}
连接成功:
DIEA绑定mysql:
3、创建实体类
User.java
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
private Integer id;
private String name;
private String pwd;
}
4、创建对应的 Mapper 接口
创建mapper目录以及对应的 Mapper 接口
//@Mapper : 表示本类是一个 MyBatis 的 Mapper
@Mapper
@Repository
public interface UserMapper {
// 获取所有部门信息
List<User> queryUserList();
// 通过id获得部门
User queryUserById(Integer id);
int addUser(User user);
int updateUser(User user);
int deleteUser(int id);
}
5.在application.yml中绑定xml
# 整合mybatis
mybatis:
type-aliases-package: com.cy.pojo
mapper-locations: classpath:mybatis/mapper/*.xml
6、对应的Mapper映射文件
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.cy.mapper.UserMapper">
<select id="queryUserList" resultType="User">
select * from user
</select>
<select id="queryUserById" resultType="User">
select * from user where id=#{id}
</select>
<insert id="addUser" parameterType="User">
insert into user(id,name,pwd) value(#{id},#{name},#{pwd})
</insert>
<update id="updateUser" parameterType="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>
7、编写部门的 UserController 进行测试!
@RestController
public class UserController {
@Autowired
private UserMapper userMapper;
//查询所有用户
@GetMapping("/query")
public List<User> queryUserList(){
List<User> userList=userMapper.queryUserList();
for (User user : userList){
System.out.println(user);
}
return userList;
}
//添加一个用户
@GetMapping("/add")
public String addUser(){
userMapper.addUser(new User(5,"张三","456789"));
return "添加成功";
}
//修改一个用户
@GetMapping("/update")
public String updateUser(){
userMapper.updateUser(new User(5,"李四","159753"));
return "修改成功";
}
//根据id删除用户
@GetMapping("/delete")
public String deleteUser(){
userMapper.deleteUser(5);
return "删除成功";
}
}
启动项目访问进行测试!
- http://localhost:8080/query
- http://localhost:8080/add
- http://localhost:8080/update
- http://localhost:8080/delete