这篇文章将为大家详细讲解有关Spring 容器中如何实现MyBatis初始化,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
MyBatis 初始化过程就是生成一些必须的对象放到 Spring 容器中。问题是这个过程到底生成了哪些对象?当遇到 MyBatis 初始化失败时,如何正确的找到分析问题的切入点?本文将针对这些问题进行介绍。
本文基于 MyBatis 3 和 Spring,假设读者已经知道如何使用 Maven 和 MyBatis,以及了解 Spring 的容器机制。
一、Mybatis 三件套
我们知道 MyBatis 的主要功能是由 SqlSessionFactory 和 Mapper 两者提供的,初始化 MyBatis 就是初始化这两类对象。除此之外 DataSource 作为数据库访问对象也是必不可少。因此首先我们应该记住 MyBatis 初始化的核心三件套:
具体来说,一个简单的初始化过程就是下面这样:
@Configuration
public class SpringMyBatisApplication {
public static void main(String[] args) {
new AnnotationConfigApplicationContext(SpringMyBatisApplication.class);
}
@Bean
public DataSource dataSource() {
return ...;
}
@Bean
public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource) {
return ...;
}
@Bean
public MapperScannerConfigurer mapperScannerConfigurer() {
return ...;
}
}
接下来介绍三件套各自如何初始化,下面的内容是可以实际操作的,不妨动手试试。
1. DataSource 初始化
首先我们创建一个空的 Maven 项目,在 pom.xml 中加入下面的依赖关系:
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<!-- 数据库 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>com.h3database</groupId>
<artifactId>h3</artifactId>
<version>1.4.199</version>
</dependency>
本文重在演示 MyBatis 的初始化过程,所以没有复杂的 SQL,数据库用的是嵌入式数据库 h3。
然后我们在 com.hyd.mybatis3test 包下面创建一个 SpringMyBatisApplication 类,代码在前面给过了。
对应的 DataSource 初始化实现如下:
@Bean
public DataSource dataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName("org.h3.Driver");
dataSource.setUrl("jdbc:h3:mem:test");
return dataSource;
}
2. SqlSessionFactoryBean 初始化
SqlSessionFactoryBean 是对 SqlSessionFactory 初始化过程的封装,Spring 会在适当的时候执行这个初始化过程,得到最终的 SqlSessionFactory 对象。
SqlSessionFactoryBean 的创建过程如下(注意方法签名在前面的基础上有变动):
@Bean
public SqlSessionFactoryBean sqlSessionFactory(
DataSource dataSource,
ResourcePatternResolver resolver
) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(resolver.getResources("classpath*:mappers/*.xml"));
return bean;
}
其中:
3. MapperScannerConfigurer 初始化
MapperScannerConfigurer 的职责是在指定路径下搜索所有的 Mapper 接口类(参考它的 postProcessBeanDefinitionRegistry() 方法),并通过 MapperFactoryBean 将其注册到 MapperRegistry 中。
@Bean
public MapperScannerConfigurer mapperScannerConfigurer() {
MapperScannerConfigurer configurer = new MapperScannerConfigurer();
configurer.setBasePackage("com.hyd.mybatis3test");
return configurer;
}
4. 验证初始化过程成功
为了验证上面的初始化过程完成了,我们在 com.hyd.mybatis3test 包下面创建一个 Mapper 类:
@Mapper
public interface SampleMapper {
@Update("create table if not exists user(id int)")
void createUserTable();
}
以及一个 Service 类:
@Service
public static class SampleService {
@Autowired
private SampleMapper sampleMapper;
@PostConstruct
public void init() {
sampleMapper.createUserTable();
}
}
然后别忘了在 SpringMyBatisApplication 顶上添加一个 @ComponentScan("com.hyd.mybatis3test") 注解,否则 Spring 会找不到 SampleService。
运行 SpringMyBatisApplication.main() 方法,我们就能在输出中找到这样的内容:
...
SampleMapper.createUserTable - ==> Preparing: create table if not exists user(id int)
SampleMapper.createUserTable - ==> Parameters:
SampleMapper.createUserTable - <== Updates: 0
...
这说明这条创建表格的 SQL 语句成功执行了。
在前面三件套的基础上,MyBatis 也提供了更多的封装。有了本文上面的铺垫,相信读者对这些封装方式理解起来也会轻松很多。
二、@MapperScan 注解
@MapperScan 注解只不过是 MapperScannerConfigurer 的启动器而已,使用这个注解,可以代替前面的 MapperScannerConfigurer 初始化。
三、SpringBoot 自动初始化
MyBatis 提供 mybatis-spring-boot-starter 库用于在 Spring Boot 项目中自动初始化:
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
这个所谓的自动初始化实际上就是初始化 SqlSessionFactory 对象。初始化的过程由 org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration 完成,所需的配置都从 "mybatis-" 前缀的配置属性中获取,具体可以参考 org.mybatis.spring.boot.autoconfigure.MybatisProperties 类。
总结
总之,MyBatis 的初始化核心过程就是三件套的初始化。而在 Spring Boot 应用中,结合自动初始化和 @MapperScan 注解,我们无需手工初始化上这三件套,就能直接从容器中得到 Mapper 对象。
关于Spring 容器中如何实现MyBatis初始化就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。