1.苞米豆MyBatis-Plus介绍
MyBatis Plus 是国内人员开发的 MyBatis 增强工具,在 MyBatis 的基础上只做增强不做改变。核心功能为提供一套通用的CRUD框架,使用者只要专注拼接sql逻辑即可
单测之前假设:
- 已顺利完成Mybatis的单测
- 基于SpringBoot,但是希望单测执行快速稳定
2.Mybatis单测支持baomidou
使用baomidou框架做单测,区别于直接使用mybatis的是, 使用MybatisSqlSessionFactoryBean代替原生****SqlSessionFactoryBean即可
2.1 环境准备
引入依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.2</version>
</dependency>
Mapper接口:
public interface BaseLiveReportMapper extends BaseMapper<LiveReportPO> {
}
Model:
@TableName("live_report")
public class LiveReportPO extends Model<LiveReportPO> {
}
2.2 开始基于Spring容器的单测
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = BaseLiveReportMapperTest.Config.class)
public class BaseLiveReportMapperTest {
@Autowired
private BaseLiveReportMapper baseLiveReportMapper;
@Test
public void getTest() {
LiveReportPO liveReportPO = baseLiveReportMapper.selectById(1L);
Assert.assertNotNull(liveReportPO);
}
@Configuration
@Import(value = MybatisConfig.class)
static class Config {
}
}
2.3 增加数据源配置依赖
import java.sql.SQLException;
import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.zaxxer.hikari.HikariDataSource;
@Configuration
@MapperScan(basePackages = { "com.x.live.center.dao.mapper" },
sqlSessionFactoryRef = "dataSessionFactory",
sqlSessionTemplateRef = "dataSessionTemplate")
@PropertySource( "classpath:application-test.properties")
public class MybatisConfig {
@Value("${spring.datasource.url}")
private String url;
@Value("${spring.datasource.username}")
private String username;
@Value("${spring.datasource.password}")
private String password;
@Bean(initMethod = "getConnection", destroyMethod = "close" ,name = "testDataSource")
public DataSource dataSource() throws SQLException {
HikariDataSource dataSource = new HikariDataSource();
dataSource.setJdbcUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
return dataSource;
}
@Bean(name = "dataSessionFactory")
public SqlSessionFactory sessionFactory(@Qualifier("testDataSource") DataSource dataSource) throws Exception {
// SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
// 替换SqlSessionFactoryBean为苞米豆的MybatisSqlSessionFactoryBean
MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
sqlSessionFactoryBean.setTypeAliasesPackage("com.x.live.center.dao.mapper");
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath*:/mapper/*Mapper.xml"));
return sqlSessionFactoryBean.getObject();
}
@Bean(name = "dataSessionTemplate")
public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory dataSessionFactory) {
return new SqlSessionTemplate(dataSessionFactory);
}
}
单测运行成功:
image.png3. 配置详解
值得一提的是,如果不使用苞米豆提供的Spring环境session工厂(MybatisSqlSessionFactoryBean),执行就会提示没有对应的可执行sql:
image.png image.png那么可以思考一下baomidou是如何实现BaseMapper接口的,又为什么会报这个错的?
原理:
简单看一下源码,可以看到MybatisSqlSessionFactoryBean 和sqlSessionFactoryBean的实现基本是一样的, 只在方法 buildSqlSessionFactory() 中做了比较多修改。 其中主要在此处加在所有的Mapper,根据配置生成CRUD代码,注入 CURD 动态 SQL
所以说苞米豆框架并不完全基于Mybatis实现的,从SqlSessionFacotryBean开始,而是通过copy部分代码
111.png4.最后
具体Mybatis中用到的配置做用处解释:
- SqlSessionFactoryBean是生产SqlSessionFactory的一种工厂bean
- SqlSessionFactory是打开SqlSession会话的工厂接口,默认实现类DefaultSqlSessionFactory使用数据库连接池
- SqlSession是客户端和数据库服务端之间的会话信息,里面有操作数据库的方法
- SqlSessionTemplate是SqlSession的一个实现
- DataSource方法:为Spring返回需要注册的数据源,这里使用HikariDataSource实现
下一章详细解一下苞米豆是如何实现的源码解析
网友评论