前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >原 spring boot 整合mybat

原 spring boot 整合mybat

作者头像
kinbug [进阶者]
发布2018-06-13 15:12:55
1K0
发布2018-06-13 15:12:55
举报

srping boot 与mybatis的整合就不在贴代码了,请看我以前的整和文章:

https://cloud.tencent.com/developer/article/1147249

我的Spring Boot 版本:1.5.9

我的mybatis分页插件pagehelper版本:5.1.2

我在开始整合的时候,还是用的5.0以前的整合方式:

@Bean(name = "sqlSessionFactory")
	public SqlSessionFactory sqlSessionFactory(DataSource dataSource) {
		SqlSessionFactoryBean sqlSessionFactoryBean=new SqlSessionFactoryBean();
		try {
			sqlSessionFactoryBean.setDataSource(dataSource);
	        // 设置别名包(实体类)
	        sqlSessionFactoryBean.setTypeAliasesPackage("com.xin.dream.pojo");
			// 设置mybatis的主配置文件
	        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
	        //设置sql配置文件路径
	        sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath*:com/xin/dream/mapper/*.xml"));
	        //分页插件
	        PageHelper page = new PageHelper();  //采坑:我用的分页插件是最新的,5.0以后就不再是PageHelper了
	        Properties properties = new Properties();  
	        properties.setProperty("dialect", "mysql");  
	        properties.setProperty("reasonable", "false");  
	        properties.setProperty("pageSizeZero", "true");  
	        page.setProperties(properties);  
	        sqlSessionFactoryBean.setPlugins(new Interceptor[]{page});
	        
			return sqlSessionFactoryBean.getObject();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

我是5.1的,结果采坑 : sqlSessionFactoryBean.setPlugins(new Interceptor[]{page});

报错:Type mismatch: cannot convert from PageHelper to Interceptor

这里我发现转换失败,没办法,只能在github上找最新的PageHelper的文档了。 

突然发现这儿坑还挺多的。下面我就把PageHelper 5.0与以前版本不一样的地方给贴出来。

  1. 使用 QueryInterceptor 规范 处理分页逻辑 新的分页插件拦截器为  com.github.pagehelper.PageInterceptor 新的 PageHelper 是一个特殊的 Dialect
  2. 实现类,以更友好的方式实现了以前的功能 新的分页插件仅有 dialect 一个参数,默认的 dialect 实现类为 PageHelper
  3. PageHelper 仍然支持以前提供的参数,在最新的使用文档中已经全部更新 PageHelper 的 helperDialect 参数和以前的 dialect 功能一样,具体可以看文档的参数说明
  4. 增加了基于纯 RowBounds 和 PageRowBounds 的分页实现,在com.github.pagehelper.dialect.rowbounds 包中,这是用于作为 dialect 参数示例的实现,后面会补充更详细的文档 去掉了不适合出现在分页插件中的 orderby功能,以后会提供单独的排序插件
  5. 去掉了PageHelper 中不常用的方法新的文档,更新历来更新日志中提到的重要内容,提供英文版本文档 解决 bug 将 Db2RowDialect 改为Db2RowBoundsDialect 所有分页插件抛出的异常改为 PageException

根据他提供的dome新的整合方式应该是:

//分页插件
PageInterceptor page = new PageInterceptor();  
Properties properties = new Properties();  
properties.setProperty("helperDialect", "mysql");  
properties.setProperty("reasonable", "true");  
properties.setProperty("supportMethodsArguments", "true");
properties.setProperty("params", "count=countSql");  
page.setProperties(properties);  
sqlSessionFactoryBean.setPlugins(new Interceptor[]{page});

使用方式(mysql为例):

sql语句:SELECT * FROM TABLE_name

service中使用:

public PageInfo<DoctorInfoDTO> buildCardDoctors(AppPage page) {
	// TODO Auto-generated method stub
	PageHelper.startPage(page.getCurrentPage(),page.getPageSize());
	List<DoctorInfoDTO> list = dao.buildCardDoctors();
	PageInfo<DoctorInfoDTO> pageInfo = new PageInfo<DoctorInfoDTO>(list);
	return pageInfo;
}

大致就这样了...


另外上一篇文章:https://​​​​​​​my.oschina.net/bianxin/blog/1602958 中:

import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
 * @instructions 添加mybatis的mapper接口扫描
 * 
 * @PS @AutoConfigureAfter(MyBatisConfig.class):
 * 		很多文章,和培训老师讲课,都说必须的加这个注释
 * 		结果我也就入坑了,其实这个注释是没的作用的,
 */
@Configuration
/*@AutoConfigureAfter(MyBatisConfig.class)  */
public class MyBatisMapperScannerConfig {
	
	@Bean
	public MapperScannerConfigurer mapperScannerConfigurer() {
		MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
		mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
		mapperScannerConfigurer.setBasePackage("com.xin.dream.dao");
		return mapperScannerConfigurer;
	}
}

@AutoConfigureAfter(MyBatisConfig.class)这个配置是无效的,很多教程都强调了配置这个,实际上MapperScannerConfigurer还是先SqlSessionFactory类加载。所以这个配置没实际意义。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档