test
wj 1 year ago
parent b1a99cced7
commit 006ada7ca6

@ -27,6 +27,20 @@ public class ResultVOUtils {
return baseResponse;
}
/**
*
*
* @param data data
* @return {@link BaseResponse}
*/
public static BaseResponse success(String msg,Object data) {
BaseResponse<Object> baseResponse = new BaseResponse<>();
baseResponse.setCode(ResultEnum.SUCCESS.getCode());
baseResponse.setMessage(msg);
baseResponse.setData(data);
return baseResponse;
}
/**
*
*
@ -37,6 +51,20 @@ public class ResultVOUtils {
return success(data);
}
/**
*
*
* @return {@link BaseResponse}
*/
public static BaseResponse successMsg(String message) {
BaseResponse<Object> baseResponse = new BaseResponse<>();
baseResponse.setCode(ResultEnum.SUCCESS.getCode());
baseResponse.setMessage(message);
Map data = new HashMap();
baseResponse.setData(data);
return baseResponse;
}
/**
*
*

@ -0,0 +1,35 @@
package com.glxp.api.config;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.glxp.api.config.serializer.*;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import java.time.LocalDate;
import java.time.LocalDateTime;
@Configuration
public class JacksonConfig {
@Bean
@Primary
// @ConditionalOnMissingBean(ObjectMapper.class)
public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
ObjectMapper objectMapper = builder.createXmlMapper(false).build();
SimpleModule simpleModule = new SimpleModule();
simpleModule.addSerializer(Long.class, new LongJsonSerializer());
simpleModule.addDeserializer(Long.class, new LongJsonDeserializer());
simpleModule.addSerializer(LocalDate.class, new LocalDateJsonSerializer());
simpleModule.addDeserializer(LocalDate.class, new LocalDateJsonDeserializer());
simpleModule.addSerializer(LocalDateTime.class, new LocalDateTimeJsonSerializer());
simpleModule.addDeserializer(LocalDateTime.class, new LocalDateTimeJsonDeserializer());
objectMapper.registerModule(simpleModule);
return objectMapper;
}
}

@ -3,10 +3,10 @@ package com.glxp.api.config;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ -19,6 +19,7 @@ import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
@Slf4j
@Configuration
// 扫描 Mapper 接口并容器管理
@MapperScan(basePackages = MasterDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "masterSqlSessionFactory")
@ -58,8 +59,20 @@ public class MasterDataSourceConfig {
sessionFactory.setConfiguration(configuration);
globalConfig.setSqlInjector(new CustomerSqlInjector());
sessionFactory.setGlobalConfig(globalConfig);
sessionFactory.setPlugins(mybatisPlusInterceptor());
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources(MasterDataSourceConfig.MAPPER_LOCATION));
return sessionFactory.getObject();
}
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
log.error("配置分页拦截器");
MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
mybatisPlusInterceptor.addInnerInterceptor(paginationInnerInterceptor); // 分页插件
//防全表修改删除
// mybatisPlusInterceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());
return mybatisPlusInterceptor;
}
}

@ -11,15 +11,4 @@ import org.springframework.context.annotation.Configuration;
@Configuration
public class YudaoMybatisAutoConfiguration {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
log.error("配置分页拦截器");
MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
mybatisPlusInterceptor.addInnerInterceptor(paginationInnerInterceptor); // 分页插件
//防全表修改删除
mybatisPlusInterceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());
return mybatisPlusInterceptor;
}
}

@ -0,0 +1,17 @@
package com.glxp.api.config.serializer;
import cn.hutool.json.JSONNull;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import org.springframework.boot.jackson.JsonComponent;
import java.io.IOException;
@JsonComponent
public class JsonNullSerializer extends JsonSerializer<JSONNull> {
@Override
public void serialize(JSONNull jsonNull, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
jsonGenerator.writeNull();
}
}

@ -0,0 +1,31 @@
package com.glxp.api.config.serializer;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import lombok.extern.slf4j.Slf4j;
import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
/**
* Long
*/
@Slf4j
public class LocalDateJsonDeserializer extends JsonDeserializer<LocalDate> {
@Override
public LocalDate deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
String value = p.getText();
try {
return value == null ? null : LocalDateTimeUtil.parseDate(value, DateTimeFormatter.ofPattern(DatePattern.NORM_DATE_PATTERN));
} catch (Exception e) {
log.error("日期格式解析错误", e);
return null;
}
}
}

@ -0,0 +1,23 @@
package com.glxp.api.config.serializer;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
/**
* Longjs 14
*/
public class LocalDateJsonSerializer extends JsonSerializer<LocalDate> {
@Override
public void serialize(LocalDate value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
String text = (value == null ? null : LocalDateTimeUtil.format(value, DateTimeFormatter.ofPattern(DatePattern.NORM_DATE_PATTERN)));
jsonGenerator.writeString(text);
}
}

@ -0,0 +1,31 @@
package com.glxp.api.config.serializer;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import lombok.extern.slf4j.Slf4j;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
/**
* Long
*/
@Slf4j
public class LocalDateTimeJsonDeserializer extends JsonDeserializer<LocalDateTime> {
@Override
public LocalDateTime deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
String value = p.getText();
try {
return value == null ? null : LocalDateTimeUtil.parse(value, DateTimeFormatter.ofPattern(DatePattern.NORM_DATETIME_PATTERN));
} catch (Exception e) {
log.error("日期格式解析错误", e);
return null;
}
}
}

@ -0,0 +1,23 @@
package com.glxp.api.config.serializer;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
/**
* Longjs 14
*/
public class LocalDateTimeJsonSerializer extends JsonSerializer<LocalDateTime> {
@Override
public void serialize(LocalDateTime value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
String text = (value == null ? null : LocalDateTimeUtil.format(value, DateTimeFormatter.ofPattern(DatePattern.NORM_DATETIME_PATTERN)));
jsonGenerator.writeString(text);
}
}

@ -0,0 +1,22 @@
package com.glxp.api.config.serializer;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import lombok.extern.slf4j.Slf4j;
import java.io.IOException;
/**
* Long
*/
@Slf4j
public class LongJsonDeserializer extends JsonDeserializer<Long> {
@Override
public Long deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
String value = p.getText();
return value == null ? null : Long.parseLong(value);
}
}

@ -0,0 +1,25 @@
package com.glxp.api.config.serializer;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import java.io.IOException;
/**
* Longjs 14
*/
public class LongJsonSerializer extends JsonSerializer<Long> {
@Override
public void serialize(Long value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
String text = (value == null ? null : String.valueOf(value));
if (text != null) {
if (text.length() > 14) {
jsonGenerator.writeString(text);
} else {
jsonGenerator.writeNumber(text);
}
}
}
}

@ -5,6 +5,8 @@ import com.glxp.api.common.enums.ResultEnum;
import com.glxp.api.common.res.BaseResponse;
import com.glxp.api.common.util.ResultVOUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@ -31,4 +33,22 @@ public class GlobalExceptionHandler {
return ResultVOUtils.error(ResultEnum.NOT_NETWORK);
}
@ExceptionHandler({BindException.class})
public BaseResponse handlerBindException(BindException e) {
log.error(e.getMessage(), e);
boolean bindingFailure = e.getFieldError().isBindingFailure();
if(bindingFailure){
return ResultVOUtils.error(ResultEnum.PARAM_VERIFY_FALL);
}
return ResultVOUtils.error(ResultEnum.PARAM_VERIFY_FALL, e.getBindingResult().getFieldError().getDefaultMessage());
}
@ExceptionHandler({HttpMessageNotReadableException.class})
public BaseResponse handlerHttpMessageNotReadableException(HttpMessageNotReadableException e) {
log.error(e.getMessage(), e);
return ResultVOUtils.error(ResultEnum.DATA_ERROR);
}
}

@ -1,6 +1,8 @@
package com.glxp.api.res;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
@ -8,6 +10,8 @@ import java.util.List;
*
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class PageSimpleResponse<T> {
// 总数
private Integer total;

@ -3,10 +3,11 @@ package com.glxp.api.service;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.glxp.api.dao.BaseMapperPlus;
import org.apache.poi.ss.formula.functions.T;
import java.util.List;
public class CustomServiceImpl<M extends BaseMapper<T>, T> extends ServiceImpl<BaseMapperPlus<M,T,T>, T> implements CustomService<T>{
public class CustomServiceImpl<M extends BaseMapperPlus<M,T, T>, T> extends ServiceImpl<M, T> implements CustomService<T>{
@Override
public int insertIgnore(T entity) {
return baseMapper.insertIgnore(entity);

@ -1,34 +1,18 @@
package com.glxp.api.util;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.net.NetUtil;
import cn.hutool.core.util.IdUtil;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.springframework.stereotype.Component;
import cn.hutool.core.util.RandomUtil;
import javax.annotation.PostConstruct;
/**
* @Author tan
* @Date 2021/10/16 16:23
*/
@Component
public class SnowflakeUtil {
@JsonFormat(shape = JsonFormat.Shape.STRING)
private long workerId = 0;//为终端ID
private long dataCenterId = 1;//数据中心ID
private Snowflake snowflake = IdUtil.createSnowflake(workerId,dataCenterId);
@PostConstruct
public void init(){
workerId = NetUtil.ipv4ToLong(NetUtil.getLocalhostStr());
}
public synchronized String snowflakeId(){
return String.valueOf(snowflake.nextId());
}
public synchronized long snowflakeId(long workerId,long dataCenterId){
Snowflake snowflake = IdUtil.createSnowflake(workerId, dataCenterId);
return snowflake.nextId();
/**
* id
*/
public static Long getId() {
int workId = RandomUtil.randomInt(0, 31);
int dataCenterId = RandomUtil.randomInt(0, 31);
return IdUtil.getSnowflake(workId, dataCenterId).nextId();
}
}

@ -1,5 +1,6 @@
package com.glxp.api.util.page;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Data;
import javax.validation.constraints.Max;
@ -13,12 +14,16 @@ public class ListPageRequest {
@Min(message = "分页参数错误", value = 1)
private Integer page;
private Integer page = 1;
@Min(value = 1, message = "分页参数不能小于1")
@Max(value = 500, message = "分页参数不能大于500")
private Integer limit;
private Integer limit = 10;
private String orderBy;
private String sort;
public Page getPageObj() {
return new Page(this.page, this.limit);
}
}

Loading…
Cancel
Save