1,导入mybatis的依赖
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.4</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency>
2,配置properties,
spring.datasource.username=root spring.datasource.password=123456 spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver #整合mybatis mybatis.type-aliases-package=com.king.pojo mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
3,写实体类
4,编写mapper接口
package com.king.mapper; import com.king.pojo.User; import org.apache.ibatis.annotations.Mapper; import org.springframework.stereotype.Repository; import java.util.List; //这个注解表示这个是mybatis的mapper类,相当于注册到mybatis了 //@Repository,声明这是spring的组件,交给spring容器托管 @Mapper @Repository public interface UserMapper { List<User> queryUserList(); /* User queryUserById(int id); int addUser(User user); int updateUser(User user); int deleteUser(int id);*/ }
4.编写mapper.xml
注意:springBoot的mapper.xml需写在resources文件夹下,新建一个mapper文件夹,在这里写*mapper.xml
resources/mapper/*mapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.king.mapper.UserMapper"> <select id="queryUserList" resultType="User"> select * from user </select> </mapper>
最后如何整合SpringBoot+durid+Mybatis,看我之前的就足够 了