From 414e0f9776f17a019b90217e19a9f1b354d18ac2 Mon Sep 17 00:00:00 2001 From: lihengming <89921218@qq.com> Date: Sun, 2 Jul 2017 18:13:51 +0800 Subject: [PATCH] update > optimize MybatisConfigurer --- .../project/configurer/MybatisConfigurer.java | 57 +++++++++---------- 1 file changed, 26 insertions(+), 31 deletions(-) diff --git a/src/main/java/com/company/project/configurer/MybatisConfigurer.java b/src/main/java/com/company/project/configurer/MybatisConfigurer.java index 4e4ef85..0d6487d 100644 --- a/src/main/java/com/company/project/configurer/MybatisConfigurer.java +++ b/src/main/java/com/company/project/configurer/MybatisConfigurer.java @@ -4,8 +4,10 @@ import com.github.pagehelper.PageHelper; import org.apache.ibatis.plugin.Interceptor; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.core.io.support.ResourcePatternResolver; @@ -22,52 +24,45 @@ import static com.company.project.core.ProjectConstant.*; */ @Configuration public class MybatisConfigurer { - @Resource - private DataSource dataSource; @Bean - public SqlSessionFactory sqlSessionFactoryBean() throws Exception { - SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); - bean.setDataSource(dataSource); - bean.setTypeAliasesPackage(MODEL_PACKAGE); + public SqlSessionFactory sqlSessionFactoryBean(DataSource dataSource) throws Exception { + SqlSessionFactoryBean factory = new SqlSessionFactoryBean(); + factory.setDataSource(dataSource); + factory.setTypeAliasesPackage(MODEL_PACKAGE); - //分页插件 + //配置分页插件,详情请查阅官方文档 PageHelper pageHelper = new PageHelper(); Properties properties = new Properties(); - properties.setProperty("reasonable", "true"); - properties.setProperty("supportMethodsArguments", "true"); - properties.setProperty("returnPageInfo", "check"); - properties.setProperty("params", "count=countSql"); + properties.setProperty("pageSizeZero", "true");//分页尺寸为0时查询所有纪录不再执行分页 + properties.setProperty("reasonable", "true");//页码<=0 查询第一页,页码>=总页数查询最后一页 + properties.setProperty("supportMethodsArguments", "true");//支持通过 Mapper 接口参数来传递分页参数 pageHelper.setProperties(properties); //添加插件 - bean.setPlugins(new Interceptor[]{pageHelper}); + factory.setPlugins(new Interceptor[]{pageHelper}); //添加XML目录 ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); - bean.setMapperLocations(resolver.getResources("classpath:mapper/*.xml")); - return bean.getObject(); + factory.setMapperLocations(resolver.getResources("classpath:mapper/*.xml")); + return factory.getObject(); } - @Configuration - @AutoConfigureAfter(MybatisConfigurer.class) - public static class MyBatisMapperScannerConfigurer { - - @Bean - public MapperScannerConfigurer mapperScannerConfigurer() { - MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer(); - mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactoryBean"); - mapperScannerConfigurer.setBasePackage(MAPPER_PACKAGE); - //配置通用mappers - Properties properties = new Properties(); - properties.setProperty("mappers", MAPPER_INTERFACE_REFERENCE); - properties.setProperty("notEmpty", "false"); - properties.setProperty("IDENTITY", "MYSQL"); - mapperScannerConfigurer.setProperties(properties); + @Bean + public MapperScannerConfigurer mapperScannerConfigurer() { + MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer(); + mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactoryBean"); + mapperScannerConfigurer.setBasePackage(MAPPER_PACKAGE); - return mapperScannerConfigurer; - } + //配置通用Mapper,详情请查阅官方文档 + Properties properties = new Properties(); + properties.setProperty("mappers", MAPPER_INTERFACE_REFERENCE); + properties.setProperty("notEmpty", "false");//insert、update是否判断字符串类型!='' 即 test="str != null"表达式内是否追加 and str != '' + properties.setProperty("IDENTITY", "MYSQL"); + mapperScannerConfigurer.setProperties(properties); + return mapperScannerConfigurer; } + }