深度解析:理解MyBatis是如何在Spring容器中初始化 |
您所在的位置:网站首页 › spring是啥牌子 › 深度解析:理解MyBatis是如何在Spring容器中初始化 |
![]() MyBatis 初始化过程就是生成一些必须的对象放到 Spring 容器中。问题是这个过程到底生成了哪些对象?当遇到 MyBatis 初始化失败时,如何正确找到分析问题的切入点?本文将针对这些问题进行介绍。 本文基于 MyBatis 3 和 Spring,假设读者已经知道如何使用 Maven 和 MyBatis,以及了解 Spring 的容器机制。 一、Mybatis 三件套我们知道 MyBatis 的主要功能是由 SqlSessionFactory 和 Mapper 两者提供的,初始化 MyBatis 就是初始化这两类对象。除此之外 DataSource 作为数据库访问对象也是必不可少。因此首先我们应该记住 MyBatis 初始化的核心三件套: DataSource:它是访问数据库所必须的数据源对象,这个初始化失败就无法直接访问数据库。SqlSessionFactoryBean:这是在 Spring 容器中对 SqlSessionFactory 初始化过程的封装。MapperScannerConfigurer:这是在 Spring 容器中对 Mapper 初始化过程的封装。具体来说,一个简单的初始化过程就是下面这样: 代码语言:javascript复制@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 中加入下面的依赖关系: 代码语言:javascript复制 org.springframework spring-beans 5.2.0.RELEASE org.springframework spring-context-support 5.2.0.RELEASE org.springframework spring-jdbc 5.2.0.RELEASE org.springframework spring-tx 5.2.0.RELEASE org.apache.commons commons-dbcp2 2.7.0 com.h2database h2 1.4.199本文重在演示 MyBatis 的初始化过程,所以没有复杂的 SQL,数据库用的是嵌入式数据库 h2。 然后我们在 com.hyd.mybatis3test 包下面创建一个 SpringMyBatisApplication 类,代码在前面给过了。 对应的 DataSource 初始化实现如下: 代码语言:javascript复制@Bean public DataSource dataSource() { BasicDataSource dataSource = new BasicDataSource(); dataSource.setDriverClassName("org.h2.Driver"); dataSource.setUrl("jdbc:h2:mem:test"); return dataSource; }2. SqlSessionFactoryBean 初始化SqlSessionFactoryBean 是对 SqlSessionFactory 初始化过程的封装,Spring 会在适当的时候执行这个初始化过程,得到最终的 SqlSessionFactory 对象。 SqlSessionFactoryBean 的创建过程如下(注意方法签名在前面的基础上有变动): 代码语言:javascript复制@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; }其中: 第一个参数 dataSource 就是前面生成的数据源对象;第二个参数 resolver 是 Spring 自动提供的,用于搜索指定路径下的所有 xml 文件。本文不会包含 xml 文件,所以这个配置是无效的,这行可以不写,不过写了也不影响程序运行。3. MapperScannerConfigurer 初始化MapperScannerConfigurer 的职责是在指定路径下搜索所有的 Mapper 接口类(参考它的 postProcessBeanDefinitionRegistry() 方法),并通过 MapperFactoryBean 将其注册到 MapperRegistry 中。 代码语言:javascript复制@Bean public MapperScannerConfigurer mapperScannerConfigurer() { MapperScannerConfigurer configurer = new MapperScannerConfigurer(); configurer.setBasePackage("com.hyd.mybatis3test"); return configurer; }4. 验证初始化过程成功为了验证上面的初始化过程完成了,我们在 com.hyd.mybatis3test 包下面创建一个 Mapper 类: 代码语言:javascript复制@Mapper public interface SampleMapper { @Update("create table if not exists user(id int)") void createUserTable(); }以及一个 Service 类: 代码语言:javascript复制@Service public static class SampleService { @Autowired private SampleMapper sampleMapper; @PostConstruct public void init() { sampleMapper.createUserTable(); } }然后别忘了在 SpringMyBatisApplication 顶上添加一个 @ComponentScan("com.hyd.mybatis3test") 注解,否则 Spring 会找不到 SampleService。 运行 SpringMyBatisApplication.main() 方法,我们就能在输出中找到这样的内容: 代码语言:javascript复制... SampleMapper.createUserTable - ==> Preparing: create table if not exists user(id int) SampleMapper.createUserTable - ==> Parameters: SampleMapper.createUserTable - |
今日新闻 |
点击排行 |
|
推荐新闻 |
图片新闻 |
|
专题文章 |
CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭 |