parent
1b57ed4622
commit
6d2d5c7da7
@ -0,0 +1,73 @@ |
|||||||
|
package com.company.project.configurer; |
||||||
|
|
||||||
|
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.boot.autoconfigure.AutoConfigureAfter; |
||||||
|
import org.springframework.context.annotation.Bean; |
||||||
|
import org.springframework.context.annotation.Configuration; |
||||||
|
import org.springframework.core.io.support.PathMatchingResourcePatternResolver; |
||||||
|
import org.springframework.core.io.support.ResourcePatternResolver; |
||||||
|
import tk.mybatis.spring.mapper.MapperScannerConfigurer; |
||||||
|
|
||||||
|
import javax.annotation.Resource; |
||||||
|
import javax.sql.DataSource; |
||||||
|
import java.util.Properties; |
||||||
|
|
||||||
|
import static com.company.project.core.ProjectConstant.*; |
||||||
|
|
||||||
|
/** |
||||||
|
* Mybatis & Mapper & PageHelper 配置 |
||||||
|
*/ |
||||||
|
@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); |
||||||
|
|
||||||
|
//分页插件
|
||||||
|
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"); |
||||||
|
pageHelper.setProperties(properties); |
||||||
|
|
||||||
|
//添加插件
|
||||||
|
bean.setPlugins(new Interceptor[]{pageHelper}); |
||||||
|
|
||||||
|
//添加XML目录
|
||||||
|
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); |
||||||
|
bean.setMapperLocations(resolver.getResources("classpath:mapper/*.xml")); |
||||||
|
return bean.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); |
||||||
|
|
||||||
|
return mapperScannerConfigurer; |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
} |
||||||
|
|
@ -0,0 +1,109 @@ |
|||||||
|
package com.company.project.configurer; |
||||||
|
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSON; |
||||||
|
import com.alibaba.fastjson.serializer.SerializerFeature; |
||||||
|
import com.alibaba.fastjson.support.config.FastJsonConfig; |
||||||
|
import com.alibaba.fastjson.support.spring.FastJsonpHttpMessageConverter4; |
||||||
|
import com.company.project.core.Result; |
||||||
|
import com.company.project.core.ResultCode; |
||||||
|
import com.company.project.core.ServiceException; |
||||||
|
import org.slf4j.Logger; |
||||||
|
import org.slf4j.LoggerFactory; |
||||||
|
import org.springframework.context.annotation.Configuration; |
||||||
|
import org.springframework.http.converter.HttpMessageConverter; |
||||||
|
import org.springframework.web.method.HandlerMethod; |
||||||
|
import org.springframework.web.servlet.HandlerExceptionResolver; |
||||||
|
import org.springframework.web.servlet.ModelAndView; |
||||||
|
import org.springframework.web.servlet.NoHandlerFoundException; |
||||||
|
import org.springframework.web.servlet.config.annotation.CorsRegistry; |
||||||
|
import org.springframework.web.servlet.config.annotation.InterceptorRegistry; |
||||||
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; |
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest; |
||||||
|
import javax.servlet.http.HttpServletResponse; |
||||||
|
import java.io.IOException; |
||||||
|
import java.nio.charset.Charset; |
||||||
|
import java.util.List; |
||||||
|
|
||||||
|
/** |
||||||
|
* Spring MVC 配置 |
||||||
|
*/ |
||||||
|
@Configuration |
||||||
|
public class WebMvcConfigurer extends WebMvcConfigurerAdapter { |
||||||
|
|
||||||
|
private final Logger logger = LoggerFactory.getLogger(WebMvcConfigurer.class); |
||||||
|
|
||||||
|
//使用阿里 FastJson 作为JSON MessageConverter
|
||||||
|
@Override |
||||||
|
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { |
||||||
|
FastJsonpHttpMessageConverter4 converter = new FastJsonpHttpMessageConverter4(); |
||||||
|
FastJsonConfig config = new FastJsonConfig(); |
||||||
|
config.setSerializerFeatures(SerializerFeature.WriteMapNullValue,//保留空的字段
|
||||||
|
SerializerFeature.WriteNullStringAsEmpty,//String null -> ""
|
||||||
|
SerializerFeature.WriteNullNumberAsZero);//Number null -> 0
|
||||||
|
converter.setFastJsonConfig(config); |
||||||
|
converter.setDefaultCharset(Charset.forName("UTF-8")); |
||||||
|
converters.add(converter); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//统一异常处理
|
||||||
|
@Override |
||||||
|
public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) { |
||||||
|
exceptionResolvers.add(new HandlerExceptionResolver() { |
||||||
|
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception e) { |
||||||
|
Result result = new Result(); |
||||||
|
if (handler instanceof HandlerMethod) { |
||||||
|
HandlerMethod handlerMethod = (HandlerMethod) handler; |
||||||
|
|
||||||
|
if (e instanceof ServiceException) {//业务失败的异常,如“账号或密码错误”
|
||||||
|
result.setCode(ResultCode.FAIL).setMessage(e.getMessage()); |
||||||
|
logger.info(e.getMessage()); |
||||||
|
} else { |
||||||
|
result.setCode(ResultCode.INTERNAL_SERVER_ERROR).setMessage("接口 [" + request.getRequestURI() + "] 内部错误,请联系管理员"); |
||||||
|
String message = String.format("接口 [%s] 出现异常,方法:%s.%s,异常摘要:%s", |
||||||
|
request.getRequestURI(), |
||||||
|
handlerMethod.getBean().getClass().getName(), |
||||||
|
handlerMethod.getMethod().getName(), |
||||||
|
e.getMessage()); |
||||||
|
logger.error(message, e); |
||||||
|
} |
||||||
|
} else { |
||||||
|
if (e instanceof NoHandlerFoundException) { |
||||||
|
result.setCode(ResultCode.NOT_FOUND).setMessage("接口 [" + request.getRequestURI() + "] 不存在"); |
||||||
|
} else { |
||||||
|
result.setCode(ResultCode.INTERNAL_SERVER_ERROR).setMessage(e.getMessage()); |
||||||
|
logger.error(e.getMessage(), e); |
||||||
|
} |
||||||
|
} |
||||||
|
responseResult(response, result); |
||||||
|
return new ModelAndView(); |
||||||
|
} |
||||||
|
|
||||||
|
}); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void addInterceptors(InterceptorRegistry registry) { |
||||||
|
//添加拦截器
|
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void addCorsMappings(CorsRegistry registry) { |
||||||
|
//解决跨域问题
|
||||||
|
//registry.addMapping("/**");
|
||||||
|
} |
||||||
|
|
||||||
|
private void responseResult(HttpServletResponse response, Result result) { |
||||||
|
response.setCharacterEncoding("UTF-8"); |
||||||
|
response.setHeader("Content-type", "application/json;charset=UTF-8"); |
||||||
|
response.setStatus(200); |
||||||
|
try { |
||||||
|
response.getWriter().write(JSON.toJSONString(result)); |
||||||
|
} catch (IOException ex) { |
||||||
|
logger.error(ex.getMessage()); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue