Mybatis两种整合方法
1. 配置模式
导入依赖
1
2
3
4
5
6
7<!--myBatis,注意是非spring-boot官方的依赖-->
<!-- 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>配置数据库连接信息
1
2
3
4
5
6
7
8
9spring.datasource.username=root
spring.datasource.password=zzz
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=zone.yiqing.pojo
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
mybatis.configuration.map-underscore-to-camel-case=true测试连接是否成功
1
2
3
4
5
6
7
8
9
10
11
class Springboot05MybatisApplicationTests {
DataSource dataSource;
void contextLoads() throws Exception {
System.out.println(dataSource.getClass());
System.out.println(dataSource.getConnection());
}
}创建实体类
1
2
3
4
5
6
7
8
public class User {
private int id;
private String name;
private String pwd;
}创建mapper目录以及mapper接口文件
1
2
3
4
5
6
7
8
9
10
11
12
13// 这个注解标识这个类是mybatis的mapper类
// 将类的实现类交给spring管理
public interface UserMapper {
List<User> queryUserList();
User queryUserById(int id);
int addUser(User user);
int updateUser(User user);
int deleteUser(int id);
}编写对应的Mapper.xml
注意: 写在resources下面 (resources/mybatis/mapper/UserMapper.xml)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<mapper namespace="zone.yiqing.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) values(#{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>
如果有资源过滤问题注意配置maven资源过滤问题
编写controller
1
2
3
4
5
6
7
8
9
10
11
12
public class UserController {
private UserMapper userMapper;
public List<User> queryUserList(){
List<User> userList = userMapper.queryUserList();
return userList;
}
}
2. 注解模式
直接在mapper接口上添加注解
1 |
|
3. 混合模式
1 |
|
最佳方法
引入mybatis-starter
配置application.yaml中,指定mapper-location位置即可
编写Mapper接口并标注@Mapper注解
简单方法直接注解方式
复杂方法编写mapper.xml进行绑定映射
@MapperScan(“com.atguigu.admin.mapper”) 简化,其他的接口就可以不用标注@Mapper注解
Mybatis-Plus
pom引入starter
1
2
3
4
5<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>自动配置原理
- MybatisPlusAutoConfiguration. 其中MybatisPlusProperties配置项绑定了,所以mybatis-plus:xxx就是对mybatis-plus的定制
- SqlSessionFactory自动配置.底层是我们自己配置 的的数据源
- mapperLocations 自动配置. 默认值: classpath*:/mapper/*/.xml,即任意包的类路径下的所有mapper文件夹下的所有xml都是sql映射文件
- 约定大于配置
- 建议所有sql映射文件,都放在mapper下
- 容器中也自动配置好了SqlSessionTemplate
- @Mapper标注的接口也会被自动扫描
- 直接在主启动类上面标注
@MapperScan("zone.yiqing.mapper")
就可以批量扫描,就不用标注@Mapper
了
- 直接在主启动类上面标注
优点
- 只需要我们的Mapper继承BaseMapper.简单CURD都有了
注意
- 实体类所有的属性都应该在数据库中
- 如果不在,使用
@TableField(exist=flase)
标注属性