本节学习SpringBoot整合Mybatis框架以及Mybatis模块starter的配置。
1. Mybatis简介2. 导入Mybatis模块starterMyBatis 是一款优秀的持久层框架,它支持自定义 SQL、存储过程以及高级映射。MyBatis 免除了几乎所有的 JDBC 代码以及设置参数和获取结果集的工作。MyBatis 可以通过简单的 XML 或注解来配置和映射原始类型、接口和 Java POJO(Plain Old Java Objects,普通老式 Java 对象)为数据库中的记录。
什么是Mybatis - Mybatis官网
Mybatis官方提供了第三方模块启动器,可以通过SpringBoot的自动配置直接使用。
2.1 导入模块starterpom.xml:
org.mybatis.spring.boot mybatis-spring-boot-starter 2.1.4
2.1.X适用于SpringBoot2.1-2.4版本,2.2.X适用于SpringBoot2.5+版本。
2.2 分析导入的依赖分析导入Mybatis模块starter后引入的依赖:
org.mybatis.spring.boot:mybatis-spring-boot-starter:2.1.4 │ ├── org.springframework.boot:spring-boot-starter:2.4.5 (父启动器) │ ├── org.springframework.boot:spring-boot-starter-jdbc:2.4.5 (JDBC模块启动器) │ │ │ ├── org.springframework.boot:spring-boot-starter:2.4.5 (父启动器) │ │ │ ├── com.zaxxer:HikariCP:3.4.5 (Hikari数据源(连接池)) │ │ │ └── org.springframework:spring-jdbc:5.3.6 (SpringJDBC模块) │ ├── org.mybatis.spring.boot:mybatis-spring-boot-autoconfigure:2.1.4 (Mybatis自动配置包) │ ├── org.mybatis:mybatis:3.5.6 (Mybatis) │ └── org.mybatis:mybatis-spring:2.0.6 (Mybatis与Spring整合包)
导入Mybatis模块starter后,为我们引入了SpringJDBC模块及其启动器,Mybatis自动配置包,Mybatis和Mybatis与Spring整合包。
同样未提供数据库驱动依赖,需要我们自己手动导入。
学习新的starter,就不得不牵扯到其自动配置原理。
来到Mybatis自动配置包org.mybatis.spring.boot.autoconfigure下,简单分析重要的自动配置类:
| 名称 | 功能 |
|---|---|
| MybatisAutoConfiguration | Mybatis自动配置类 |
| MybatisProperties | Mybatis配置参数类 |
传统Mybatis配置需要我们自己手动配置SqlSessionFactory、SqlSession和Mapper,现在Mybatis的自动配置类已经帮我们配置好了这些组件:
// org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration
// 配置类
@org.springframework.context.annotation.Configuration
@ConditionalOnClass({ SqlSessionFactory.class, SqlSessionFactoryBean.class })
@ConditionalOnSingleCandidate(DataSource.class)
// 使用MybatisPropertis配置参数类绑定其配置参数设置
@EnableConfigurationProperties(MybatisProperties.class)
@AutoConfigureAfter({ DataSourceAutoConfiguration.class, MybatisLanguageDriverAutoConfiguration.class })
public class MybatisAutoConfiguration implements InitializingBean {
// ...省略部分代码
// Mybatis自动配置类为我们配置好了SqlSessionFactory组件
@Bean
@ConditionalOnMissingBean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
// ...省略部分代码
}
// Mybatis自动配置类为我们配置好了SqlSessionTemplate组件
@Bean
@ConditionalOnMissingBean
public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
// ...省略部分代码
}
// 此内部类负责扫描加载Mapper
// 扫描的位置和SpringBoot包扫描位置相同
public static class AutoConfiguredMapperScannerRegistrar implements BeanFactoryAware, importBeanDefinitionRegistrar {
// ... 省略部分代码
}
// 配置类
@org.springframework.context.annotation.Configuration
// 导入负责扫描加载Mapper的内部类
@import(AutoConfiguredMapperScannerRegistrar.class)
@ConditionalOnMissingBean({MapperFactoryBean.class, MapperScannerConfigurer.class})
public static class MapperScannerRegistrarNotFoundConfiguration implements InitializingBean {
// ...省略部分代码
}
}
MybatisProperties配置参数类负责绑定自动配置类的配置参数,配置节点为mybatis,主要有以下参数:
| 参数名 | 作用 | 配置节点 |
|---|---|---|
| configLocation | Mybatis主配置文件的路径 | mybatis.config-location |
| mapperLocations | Mapper配置文件的路径 | mybatis.mapper-locations |
使用传统编写XML配置文件的方式配置Mybatis。
4.1 编写Mybatis主配置文件在resources目录编写一个Mybatis主配置文件mybatis-config.xml:
然后在SpringBoot配置文件中指定configLocation的值为Mybatis主配置文件的位置:
mybatis: config-location: classpath:mybatis-config.xml
不过Mybatis自动配置已经帮我们配置好了主配置文件中的一切参数,我们当然也可以不用编写主配置文件,直接在SpringBoot配置文件中指定即可:
mybatis:
configuration:
aggressive-lazy-loading: false
lazy-loading-enabled: true
关于Mybatis的其它设置,可以参考Mybatis官网设置列表
4.2 编写Mapper配置文件编写POJO类:
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Account {
private Integer id;
private String name;
private Double money;
}
编写Mapper接口:
@Mapper
public interface AccountMapper {
List getAll();
}
@Mapper注解可以作用在类、方法、变量和参数上,一般作用在接口类上,表示此接口是一个Mapper接口,可以被Mybatis识别并扫描。
编写Mapper配置文件:
SELECT * FROM account;
最后在SpringBoot配置文件中指定mapperLocations的值为Mapper配置文件所在的位置:
# 可以使用*通配符来扫描所有Mapper配置文件 mybatis: mapper-locations: classpath:mybatis/mapper/*.xml4.3 测试
为了方便,在此表现层直接调用持久层代码。实际上还需要编写业务层处理逻辑。指定数据库的配置信息后,编写后端控制器:
@RestController
public class AccountController {
private AccountMapper mapper;
@Autowired
public void setMapper(AccountMapper mapper) {
this.mapper = mapper;
}
@GetMapping("/all")
public Object getAll() {
return mapper.getAll();
}
}
发送/all请求测试,显示结果:
[
{
"id": 1,
"name": "张三",
"money": 1000.0
},
{
"id": 2,
"name": "李四",
"money": 2000.0
}
]
5. 注解配置方式使用Mybatis
在上节的基础上,不用编写Mapper配置文件,直接在Mapper接口里面使用注解编写sql语句即可:
@Mapper
public interface AccountMapper {
@Select("SELECT * FROM account;")
List getAll();
}
注解配置方式可以和XML配置方式混合使用。
6. 总结- 引入Mybatis模块starter;
- 配置Mybatis主配置设置,可以通过配置SpringBoot配置文件或者编写Mybatis主配置文件两种方式;
- 编写Mapper接口并标注@Mapper注解;
- 配置Mapper配置,可以通过使用注解或者编写Mapper配置文件两种方式;
- 编写业务层逻辑进行测试。
对于简单的SQL语句可以直接使用注解方式,复杂的SQL语句可以使用XML配置方式。两者可以混合使用。



