From 308d7796e8e5b83bd5998006cc8910c6b3941eed Mon Sep 17 00:00:00 2001 From: zhangjie Date: Fri, 25 Aug 2017 14:37:46 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=BD=93mapper=E7=9B=AE?= =?UTF-8?q?=E5=BD=95=E6=B2=A1=E6=9C=89mapper=E7=9A=84xml=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E7=9A=84=E6=97=B6=E5=80=99=EF=BC=8C=E5=90=AF?= =?UTF-8?q?=E5=8A=A8=E6=8A=A5=E9=94=99=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../project/configurer/MybatisConfigurer.java | 31 ++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/company/project/configurer/MybatisConfigurer.java b/src/main/java/com/company/project/configurer/MybatisConfigurer.java index 4e4ef8555..f30fd9960 100644 --- a/src/main/java/com/company/project/configurer/MybatisConfigurer.java +++ b/src/main/java/com/company/project/configurer/MybatisConfigurer.java @@ -9,10 +9,14 @@ 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.*; @@ -45,7 +49,11 @@ public SqlSessionFactory sqlSessionFactoryBean() throws Exception { //添加XML目录 ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); - bean.setMapperLocations(resolver.getResources("classpath:mapper/*.xml")); + //如果没有此目录的话,后续初始化SqlSessionFactory会报错,这里参考MybatisProperties的代码 + org.springframework.core.io.Resource[] resources = null; + if (!ObjectUtils.isEmpty(resources = resolveMapperLocations("classpath:mapper/**/*.xml"))) { + bean.setMapperLocations(resources); + } return bean.getObject(); } @@ -69,5 +77,26 @@ public MapperScannerConfigurer 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()]); + } } From d86363f747c8e8169c078f342fc79e39464dcaaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=B4=81?= Date: Mon, 30 Jul 2018 21:04:36 +0800 Subject: [PATCH 2/2] =?UTF-8?q?0=E6=9D=A1=E8=AE=B0=E5=BD=95=EF=BC=8C?= =?UTF-8?q?=E4=B8=8D=E6=89=A7=E8=A1=8C=E5=88=86=E9=A1=B5=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../project/configurer/MybatisConfigurer.java | 16 +++++++++------- .../com/company/project/core/ResultCode.java | 12 ++++++------ 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/company/project/configurer/MybatisConfigurer.java b/src/main/java/com/company/project/configurer/MybatisConfigurer.java index 1748d9ad9..08e1d41ca 100644 --- a/src/main/java/com/company/project/configurer/MybatisConfigurer.java +++ b/src/main/java/com/company/project/configurer/MybatisConfigurer.java @@ -4,10 +4,8 @@ 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; @@ -40,9 +38,12 @@ public SqlSessionFactory sqlSessionFactoryBean() throws Exception { //配置分页插件,详情请查阅官方文档 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); //添加插件 @@ -51,11 +52,12 @@ public SqlSessionFactory sqlSessionFactoryBean() throws Exception { //添加XML目录 ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); //如果没有此目录的话,后续初始化SqlSessionFactory会报错,这里参考MybatisProperties的代码 + //这样纯使用注解,没有mapper目录也可以了 org.springframework.core.io.Resource[] resources = null; if (!ObjectUtils.isEmpty(resources = resolveMapperLocations("classpath:mapper/**/*.xml"))) { - bean.setMapperLocations(resources); + factory.setMapperLocations(resources); } - return bean.getObject(); + return factory.getObject(); } @Configuration 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;