diff --git a/src/main/java/com/company/project/configurer/MybatisConfigurer.java b/src/main/java/com/company/project/configurer/MybatisConfigurer.java index 0d6487d86..08e1d41ca 100644 --- a/src/main/java/com/company/project/configurer/MybatisConfigurer.java +++ b/src/main/java/com/company/project/configurer/MybatisConfigurer.java @@ -4,17 +4,19 @@ 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; +import org.springframework.util.ObjectUtils; import tk.mybatis.spring.mapper.MapperScannerConfigurer; import javax.annotation.Resource; import javax.sql.DataSource; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; import java.util.Properties; import static com.company.project.core.ProjectConstant.*; @@ -24,9 +26,11 @@ */ @Configuration public class MybatisConfigurer { + @Resource + private DataSource dataSource; @Bean - public SqlSessionFactory sqlSessionFactoryBean(DataSource dataSource) throws Exception { + public SqlSessionFactory sqlSessionFactoryBean() throws Exception { SqlSessionFactoryBean factory = new SqlSessionFactoryBean(); factory.setDataSource(dataSource); factory.setTypeAliasesPackage(MODEL_PACKAGE); @@ -34,9 +38,12 @@ public SqlSessionFactory sqlSessionFactoryBean(DataSource dataSource) throws Exc //配置分页插件,详情请查阅官方文档 PageHelper pageHelper = new PageHelper(); Properties properties = new Properties(); - properties.setProperty("pageSizeZero", "true");//分页尺寸为0时查询所有纪录不再执行分页 - properties.setProperty("reasonable", "true");//页码<=0 查询第一页,页码>=总页数查询最后一页 - properties.setProperty("supportMethodsArguments", "true");//支持通过 Mapper 接口参数来传递分页参数 + //分页尺寸为0时查询所有纪录不再执行分页 + properties.setProperty("pageSizeZero", "true"); + //页码<=0 查询第一页,页码>=总页数查询最后一页 + properties.setProperty("reasonable", "true"); + //支持通过 Mapper 接口参数来传递分页参数 + properties.setProperty("supportMethodsArguments", "true"); pageHelper.setProperties(properties); //添加插件 @@ -44,25 +51,56 @@ public SqlSessionFactory sqlSessionFactoryBean(DataSource dataSource) throws Exc //添加XML目录 ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); - factory.setMapperLocations(resolver.getResources("classpath:mapper/*.xml")); + //如果没有此目录的话,后续初始化SqlSessionFactory会报错,这里参考MybatisProperties的代码 + //这样纯使用注解,没有mapper目录也可以了 + org.springframework.core.io.Resource[] resources = null; + if (!ObjectUtils.isEmpty(resources = resolveMapperLocations("classpath:mapper/**/*.xml"))) { + factory.setMapperLocations(resources); + } return factory.getObject(); } - @Bean - public MapperScannerConfigurer mapperScannerConfigurer() { - MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer(); - mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactoryBean"); - mapperScannerConfigurer.setBasePackage(MAPPER_PACKAGE); + @Configuration + @AutoConfigureAfter(MybatisConfigurer.class) + public static class MyBatisMapperScannerConfigurer { - //配置通用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); + @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); + //insert、update是否判断字符串类型!='' 即 test="str != null"表达式内是否追加 and str != '' + properties.setProperty("notEmpty", "false"); + properties.setProperty("IDENTITY", "MYSQL"); + mapperScannerConfigurer.setProperties(properties); + + return mapperScannerConfigurer; + } - return mapperScannerConfigurer; } + public org.springframework.core.io.Resource[] resolveMapperLocations(String... mapperLocations) { + + PathMatchingResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver(); + ArrayList resources = new ArrayList(); + if(mapperLocations != null) { + int total = mapperLocations.length; + + for(int i = 0; i < total; ++i) { + String mapperLocation = mapperLocations[i]; + try { + org.springframework.core.io.Resource[] mappers = resourceResolver.getResources(mapperLocation); + resources.addAll(Arrays.asList(mappers)); + } catch (IOException ex) { + ; + } + } + } + + return (org.springframework.core.io.Resource[])resources.toArray(new org.springframework.core.io.Resource[resources.size()]); + } } diff --git a/src/main/java/com/company/project/core/ResultCode.java b/src/main/java/com/company/project/core/ResultCode.java index 7768df9ab..6f4eab5fd 100644 --- a/src/main/java/com/company/project/core/ResultCode.java +++ b/src/main/java/com/company/project/core/ResultCode.java @@ -1,14 +1,14 @@ package com.company.project.core; /** - * 响应码枚举,参考HTTP状态码的语义 + * 响应码枚举,参考微信全局返回码说明 */ public enum ResultCode { - SUCCESS(200),//成功 - FAIL(400),//失败 - UNAUTHORIZED(401),//未认证(签名错误) - NOT_FOUND(404),//接口不存在 - INTERNAL_SERVER_ERROR(500);//服务器内部错误 + SUCCESS(0),//请求成功 + FAIL(40000),//请求失败(比如请求参数不匹配,请求body之类的错误) + UNAUTHORIZED(40001),//未认证(签名错误) + NOT_FOUND(40004),//接口不存在 + INTERNAL_SERVER_ERROR(-1);//服务器内部错误,系统繁忙 private final int code;