mybatis plus springboot集成 使用
mybaits优势:
1.mybaitsplus只是在mybatis上做增强,0入侵,替换无感
2.配置简单,使用简单
3.功能丰富(自动生成代码、分页简单、性能分析等功能)
pom:
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.0</version>
</dependency>
config:
#MyBatis paging config
pageHelper.reasonable = false
pageHelper.supportMethodsArguments = true
pageHelper.returnPageInfo = check
pageHelper.params = count=countSql
如果用spring管理线程池方式配置的话,需要把mybatis的factory更改为mybatisplus的。不然会报注入错误
@Bean(name = "ermpOracleSessionFactory")
public SqlSessionFactory ermpOracleSessionFactory() throws Exception {
MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean ();
sqlSessionFactoryBean.setDataSource(dataSource());
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
try {
//设置sql执行的路径
sqlSessionFactoryBean.setMapperLocations(
resolver.getResources("/com/hs/report/oracle/mapper/*.xml"));
return sqlSessionFactoryBean.getObject();
} catch (Exception e) {
throw new Exception("初始化sessionFactory错误!", e);
}
}
这样就可以完成集成了。
语法糖:
可以直接调用dao操作:
也可以:
更推荐:
提供了丰富多彩的语法糖,自己可以点着看看。
dao层:
basemapper提供了丰富的包装语法。
其实个人理解,就是把我们一些经常写的crud操作,进行了语法库封装,源码上来看,是在连接操作上进行了一个嵌套,利用模板方法类进行操作。
源码写的不错,还有中文注释,应该是中国人开发的。可以看看