在做项目的过程中接触到,之后自己在搭建框架时整合了一下顺便记录一下整合时候出现的问题
1 .首先 导入pom依赖(这里只讲spring boot +tk.mybatis)
<!-- pagehelper -->
分页插件
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.0</version>
</dependency>
通用mapper
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>RELEASE</version>
</dependency>
2.配置mybaits(这里只做java代码配置)
package com.orhonit.common.config;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import tk.mybatis.spring.mapper.MapperScannerConfigurer;
import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.Properties;
/**
*
* @author cyf
* @date 2018/10/29 下午8:53:08
*/
@Configuration
public class MybatisConfig {
@Resource
private DataSource dataSource;
@Bean
public SqlSessionFactory sqlSessionFactoryBean() throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
//添加XML目录 注意: resource目录下必须要有mapper文件夹 否则报错,此处也是坑
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
bean.setMapperLocations(resolver.getResources("mapper/*.xml"));
return bean.getObject();
}
//注意此类的加载必须要在MybatisConfig之后在加载
@Configuration
@AutoConfigureAfter(MybatisConfig.class)
public static class MyBatisMapperScannerConfig {
@Bean
public MapperScannerConfigurer mapperScannerConfig() {
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactoryBean");
//扫描mapper位置
mapperScannerConfigurer.setBasePackage("com.orhonit.modules.*.*");
//配置通用mappers
Properties properties = new Properties();
//注册自定义mapper
properties.setProperty("mappers", "com.orhonit.common.mapper.CommonMapper");
properties.setProperty("notEmpty", "false");
properties.setProperty("IDENTITY", "MYSQL");
mapperScannerConfigurer.setProperties(properties);
return mapperScannerConfigurer;
}
}
/**
* 配置mybatis的分页插件pageHelper
* @return
*/
@Bean
public PageHelper pageHelper(){
PageHelper pageHelper = new PageHelper();
Properties properties = new Properties();
properties.setProperty("offsetAsPageNum","true");
properties.setProperty("rowBoundsWithCount","true");
properties.setProperty("reasonable","true");
//指定为MySQL数据库
properties.setProperty("helperDialect","mysql");
pageHelper.setProperties(properties);
return pageHelper;
}
}
在这里要注意一下:MyBatisMapperScannerConfig 类的加载要在MybatisConfig 之后 ,否则报错
3.现在就可以直接用了,在需要的mapper类上继承mapper,或者其他通用mapper就可以,例如
4. 自定义通用mapper
注意:自定义通用mapper时会出现一个问题,务必别让自定义通用mapper扫描到spring 中,否则报错!!!!!!!!!!!
由于在MyBatisMapperScannerConfig 类中已经将自定义通用mapper注册了,再次注册就会报错.
properties.setProperty("mappers", "com.orhonit.common.mapper.CommonMapper");
注意:创建完commonMapper,此时还有一个问题,自定义mapper必须放置基础mapper类的父包中,也就是放到扫描不到的地方,由于之前在MyBatisMapperSca.nnerConfig 类中配置了mapper扫描,也就是这句话
//扫描mapper位置
mapperScannerConfigurer.setBasePackage("com.orhonit.modules.*.*");
使用时boot主入口添加
@MapperScan(basePackages = {“com.yunsuibi.model.*.mapper”})