Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修复当mapper目录没有mapper的xml配置文件的时候,启动报错的问题 #44

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 57 additions & 19 deletions src/main/java/com/company/project/configurer/MybatisConfigurer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand All @@ -24,45 +26,81 @@
*/
@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);

//配置分页插件,详情请查阅官方文档
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);

//添加插件
factory.setPlugins(new Interceptor[]{pageHelper});

//添加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()]);
}
}

12 changes: 6 additions & 6 deletions src/main/java/com/company/project/core/ResultCode.java
Original file line number Diff line number Diff line change
@@ -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;

Expand Down