为什么要学习mybatis-spring注解版开发呢?可以帮助我们了解到mybatis在spring-boot中自动装配做了什么事情。
1:环境搭建
maven依赖:
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.10.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
<!-- mybatis 基础包依赖 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<!-- mybatis spring包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.2</version>
</dependency>
</dependencies>
配置类
@Configuration
/**
* Spring 扫包
*/
@ComponentScan({"com.liuqi"})
/**
* 启用 spring 事务
*/
@EnableTransactionManagement
/**
*引入 properties资源文件
*/
@PropertySource(value = "mysql.properties")
/**
* Mapper接口扫包
*/
@MapperScan({"com.liuqi.mapper"})
public class MeMyBatisConfig {
@Value("${jdbc.driver}")
private String driver;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
/**
* 注入数据源
*
* @return {@link PooledDataSource} MyBatis提供的
*/
@Bean
public DataSource dataSource() {
MysqlDataSource dataSource = new MysqlDataSource();
dataSource.setUrl(url);
dataSource.setUser(username);
dataSource.setPassword(password);
return dataSource;
}
/**
* 注入一个 SqlSessionFactoryBean
* @param dataSource
* @return
* @throws Exception
*/
@Bean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(dataSource);
/* Spring 资源解析器 */
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
/* 设置 mapper.xml 所在的路径 */
factoryBean.setMapperLocations(resolver.getResources("classpath:/mybatis-mapper/**/*.xml"));
return factoryBean.getObject();
}
/**
* 注入一个 SqlSessionTemplate 防止创建 Mapper接口代理的时候频繁创建 SqlSessionTemplate实例
* @param sqlSessionFactory
* @return
*/
@Bean
public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
/**
* spring 事务
* @param dataSource
* @return
*/
@Bean
public PlatformTransactionManager platformTransactionManager(DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
}
Mapper接口,Mapper.XML,Service这些就不写了。效果演示
- 首先我们需要注入一个数据源。
- 通过SqlSessionFactoryBean创建一个SqlSessionFactory,注入到容器。
- 利用SqlSessionFactory去注入一个SqlSessionTemplate。
- 用同一个数据源注入PlatformTransactionManager事务管理器。
SqlSessionFactoryBean,用于创建SqlSessionFactory,基本配置
- setDataSource 设置数据源。
- setConfigLocation 设置config.xml资源文件。
- setTypeAliases 注册别名。
- setCache 设置三级缓存。
- setPlugins 设置插件。
- setMapperLocations 设置 mapper.xml资源路径。
- setTransactionFactory 设置事务管理,在Spring环境下不要设置,否则不会使用spring的事务管理。