Excel 导入导出 带字典翻译

v1.4.1
Parker 4 years ago
parent a8ae3e0b1d
commit 43243e24d3

@ -46,35 +46,40 @@ public abstract class ApiWrapper implements Serializable {
/** ID */
@TableId
@ApiModelProperty(value = "ID")
@ExcelProperty(value = "ID", order = 1000)
@ExcelIgnore
//@ApiModelProperty(value = "ID")
//@ExcelProperty(value = "ID", order = 1000)
@CellStyleFormat
private String id;
/** 创建人 */
@ApiModelProperty(value = "创建人")
@ExcelProperty(value = "创建人", order = 1001)
@ExcelIgnore
//@ApiModelProperty(value = "创建人")
//@ExcelProperty(value = "创建人", order = 1001)
@CellStyleFormat
private String createBy;
/** 创建时间 */
@ApiModelProperty(value = "创建时间")
@ExcelProperty(value = "创建时间", order = 1002)
@CellStyleFormat
@ExcelIgnore
//@ExcelProperty(value = "创建时间", order = 1002)
//@CellStyleFormat
@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date createTime;
/** 更新人 */
@ApiModelProperty(value = "修改人")
@ExcelProperty(value = "修改人", order = 1003)
@CellStyleFormat
@ExcelIgnore
//@ExcelProperty(value = "修改人", order = 1003)
//@CellStyleFormat
private String updateBy;
/** 更新时间 */
@ApiModelProperty(value = "修改时间")
@ExcelProperty(value = "修改时间", order = 1004)
@CellStyleFormat
@ExcelIgnore
//@ExcelProperty(value = "修改时间", order = 1004)
//@CellStyleFormat
@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date updateTime;

@ -7,6 +7,7 @@ import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.opsli.api.base.warpper.ApiWrapper;
import org.opsli.common.annotation.DictType;
import org.opsli.plugins.excel.annotation.CellStyleFormat;
/**
@ -15,6 +16,9 @@ import org.opsli.plugins.excel.annotation.CellStyleFormat;
* @Author: Parker
* @CreateTime: 2020-09-16 17:33
* @Description:
*
*
*
*/
@ApiModel(value="测试接口返回Model",description="测试接口返回Model")
@Data
@ -25,8 +29,14 @@ public class TestModel extends ApiWrapper {
@CellStyleFormat
private String name;
@ApiModelProperty(value = "分类")
@DictType("testType")
@ExcelProperty(value = "分类", order = 2)
@CellStyleFormat
private String type;
@ApiModelProperty(value = "备注")
@ExcelProperty(value = "备注", order = 2)
@ExcelProperty(value = "备注", order = 3)
@CellStyleFormat
private String remark;

@ -0,0 +1,23 @@
package org.opsli.common.annotation;
import java.lang.annotation.*;
/**
* @BelongsProject: opsli-boot
* @BelongsPackage: org.opsli.common.annotation
* @Author: Parker
* @CreateTime: 2020-09-16 16:36
* @Description:
*
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@Documented
public @interface DictType {
/** 字典类型 code */
String value();
}

@ -0,0 +1,25 @@
package org.opsli.common.enums;
/**
* @BelongsProject: opsli-boot
* @BelongsPackage: org.opsli.common.enums
* @Author: Parker
* @CreateTime: 2020-09-17 23:40
* @Description: Excel
*/
public enum ExcelOperate {
/**
*
*/
READ,
/**
*
*/
WRITE;
;
}

@ -21,7 +21,7 @@ import org.opsli.core.base.entity.BaseEntity;
import org.opsli.core.base.service.interfaces.CrudServiceInterface;
import org.opsli.core.cache.local.CacheUtil;
import org.opsli.core.msg.CoreMsg;
import org.opsli.plugins.excel.ExcelUtil;
import org.opsli.core.utils.ExcelUtil;
import org.opsli.plugins.excel.exception.ExcelPluginException;
import org.opsli.plugins.redis.RedisLockPlugins;
import org.opsli.plugins.redis.lock.RedisLock;
@ -50,24 +50,26 @@ import java.util.List;
*/
@Slf4j
@RestController
public abstract class BaseRestController <E extends ApiWrapper, T extends BaseEntity, S extends CrudServiceInterface<E,T>>{
public abstract class BaseRestController <T extends BaseEntity, E extends ApiWrapper, S extends CrudServiceInterface<T,E>>{
/** 开启热点数据状态 */
protected boolean hotDataFlag = false;
/** Model Clazz 类 */
protected Class<E> modelClazz;
/** Model 泛型游标 */
private static final int modelIndex = 0;
/** Entity Clazz 类 */
protected Class<T> entityClazz;
/** Entity 泛型游标 */
private static final int entityIndex = 1;
private static final int entityIndex = 0;
/** Model Clazz 类 */
protected Class<E> modelClazz;
/** Model 泛型游标 */
private static final int modelIndex = 1;
@Autowired(required = false)
protected S IService;
@Autowired
private RedisLockPlugins redisLockPlugins;
@Autowired
private ExcelUtil excelUtil;
/**
*
@ -159,8 +161,10 @@ public abstract class BaseRestController <E extends ApiWrapper, T extends BaseEn
return ResultVo.error(CoreMsg.EXCEL_FILE_NULL.getCode(),
CoreMsg.EXCEL_FILE_NULL.getMessage());
}
ResultVo<?> resultVo;
String msgInfo = "";
try {
List<E> modelList = ExcelUtil.readExcel(files.get(0), modelClazz);
List<E> modelList = excelUtil.readExcel(files.get(0), modelClazz);
boolean ret = IService.insertBatch(modelList);
if(!ret){
throw new ExcelPluginException(CoreMsg.EXCEL_IMPORT_NO);
@ -168,18 +172,21 @@ public abstract class BaseRestController <E extends ApiWrapper, T extends BaseEn
// 花费毫秒数
long timerCount = timer.interval();
// 提示信息
String msg = StrUtil.format(CoreMsg.EXCEL_IMPORT_SUCCESS.getMessage(), modelList.size(), timerCount);
// 导入成功
return ResultVo.success(msg);
msgInfo = StrUtil.format(CoreMsg.EXCEL_IMPORT_SUCCESS.getMessage(), timerCount);
// 导出成功
resultVo = ResultVo.success(msgInfo);
resultVo.setCode(CoreMsg.EXCEL_IMPORT_SUCCESS.getCode());
} catch (ExcelPluginException e) {
// 花费毫秒数
long timerCount = timer.interval();
log.error(e.getMessage(),e);
// 提示信息
String msg = StrUtil.format(CoreMsg.EXCEL_IMPORT_ERROR.getMessage(), e.getMessage(), timerCount);
msgInfo = StrUtil.format(CoreMsg.EXCEL_IMPORT_ERROR.getMessage(), timerCount, e.getMessage());
// 导入失败
return ResultVo.error(CoreMsg.EXCEL_IMPORT_ERROR.getCode(), msg);
resultVo = ResultVo.error(CoreMsg.EXCEL_IMPORT_ERROR.getCode(), msgInfo);
}
// 记录导出日志
log.info(msgInfo);
return resultVo;
}
/**
@ -198,6 +205,10 @@ public abstract class BaseRestController <E extends ApiWrapper, T extends BaseEn
* @param response
*/
protected ResultVo<?> excelExport(String fileName, QueryWrapper<T> queryWrapper, HttpServletResponse response){
// 计时器
TimeInterval timer = DateUtil.timer();
String msgInfo = "";
ResultVo<?> resultVo;
try {
List<E> modelList = Lists.newArrayList();
if(queryWrapper != null){
@ -206,16 +217,25 @@ public abstract class BaseRestController <E extends ApiWrapper, T extends BaseEn
modelList = WrapperUtil.transformInstance(entityList, modelClazz);
}
// 导出Excel
ExcelUtil.writeExcel(response, modelList ,fileName,"sheet", modelClazz ,ExcelTypeEnum.XLSX);
excelUtil.writeExcel(response, modelList ,fileName,"sheet", modelClazz ,ExcelTypeEnum.XLSX);
// 花费毫秒数
long timerCount = timer.interval();
// 提示信息
msgInfo = StrUtil.format(CoreMsg.EXCEL_EXPORT_SUCCESS.getMessage(), modelList.size(), timerCount);
// 导出成功
return ResultVo.success(CoreMsg.EXCEL_EXPORT_SUCCESS.getMessage());
resultVo = ResultVo.success(msgInfo);
resultVo.setCode(CoreMsg.EXCEL_EXPORT_SUCCESS.getCode());
} catch (ExcelPluginException e) {
log.error(e.getMessage(),e);
// 花费毫秒数
long timerCount = timer.interval();
// 提示信息
String msg = StrUtil.format(CoreMsg.EXCEL_EXPORT_ERROR.getMessage(), e.getMessage());
msgInfo = StrUtil.format(CoreMsg.EXCEL_EXPORT_ERROR.getMessage(), timerCount, e.getMessage());
// 导出失败
return ResultVo.error(CoreMsg.EXCEL_EXPORT_ERROR.getCode(),msg);
resultVo = ResultVo.error(CoreMsg.EXCEL_EXPORT_ERROR.getCode(), msgInfo);
}
// 记录导出日志
log.info(msgInfo);
return resultVo;
}
// =================================================

@ -43,19 +43,18 @@ import java.util.List;
*/
@Slf4j
@Transactional(readOnly = true)
public abstract class CrudServiceImpl<M extends BaseMapper<T>, E extends ApiWrapper, T extends BaseEntity>
extends BaseService<M, T> implements CrudServiceInterface<E,T> {
public abstract class CrudServiceImpl<M extends BaseMapper<T>, T extends BaseEntity, E extends ApiWrapper>
extends BaseService<M, T> implements CrudServiceInterface<T,E> {
/** Model Clazz 类 */
protected Class<E> modelClazz;
/** Model 泛型游标 */
private static final int modelIndex = 1;
/** Entity Clazz 类 */
protected Class<T> entityClazz;
/** Entity 泛型游标 */
private static final int entityIndex = 2;
private static final int entityIndex = 1;
/** Model Clazz 类 */
protected Class<E> modelClazz;
/** Model 泛型游标 */
private static final int modelIndex = 2;
@Override
public E get(String id) {
@ -154,7 +153,7 @@ public abstract class CrudServiceImpl<M extends BaseMapper<T>, E extends ApiWrap
}
@Override
public Page<E,T> findPage(Page<E,T> page) {
public Page<T,E> findPage(Page<T,E> page) {
page.pageHelperBegin();
try{
List<T> list = this.findList(page.getQueryWrapper());
@ -217,8 +216,8 @@ public abstract class CrudServiceImpl<M extends BaseMapper<T>, E extends ApiWrap
@PostConstruct
public void init(){
try {
this.modelClazz = this.getModelClass();
this.entityClazz = this.getEntityClass();
this.modelClazz = this.getModelClazz();
this.entityClazz = this.getEntityClazz();
}catch (Exception e){
log.error(e.getMessage(),e);
}
@ -229,7 +228,7 @@ public abstract class CrudServiceImpl<M extends BaseMapper<T>, E extends ApiWrap
* Clazz
* @return
*/
private Class<E> getModelClass(){
private Class<E> getModelClazz(){
Class<E> tClass = null;
Type typeArgument = TypeUtil.getTypeArgument(getClass().getGenericSuperclass(), modelIndex);
if(typeArgument != null){
@ -242,7 +241,7 @@ public abstract class CrudServiceImpl<M extends BaseMapper<T>, E extends ApiWrap
* Clazz
* @return
*/
private Class<T> getEntityClass(){
private Class<T> getEntityClazz(){
Class<T> tClass = null;
Type typeArgument = TypeUtil.getTypeArgument(getClass().getGenericSuperclass(), entityIndex);
if(typeArgument != null){

@ -18,10 +18,10 @@ import java.util.List;
*
*
*
* @param <E>
* @param <T>
* @param <E>
*/
public interface CrudServiceInterface<E,T> extends BaseServiceInterface<T> {
public interface CrudServiceInterface<T,E> extends BaseServiceInterface<T> {
/**
@ -158,7 +158,7 @@ public interface CrudServiceInterface<E,T> extends BaseServiceInterface<T> {
*
* @return Page<T>
*/
Page<E,T> findPage(Page<E,T> page);
Page<T,E> findPage(Page<T,E> page);
}

@ -28,10 +28,10 @@ public enum CoreMsg implements BaseMsg {
/**
* Excel
*/
EXCEL_EXPORT_SUCCESS(10300,"Excel 导出成功!"),
EXCEL_EXPORT_ERROR(10301,"Excel 导出失败!{}"),
EXCEL_IMPORT_SUCCESS(10302,"EXCEL 导入成功!数据行数:{} 耗时(秒){}"),
EXCEL_IMPORT_ERROR(10303,"Excel导入失败{} 耗时(秒){}"),
EXCEL_EXPORT_SUCCESS(10300,"Excel 导出成功! - 数据行数:{} - 耗时:{}毫秒"),
EXCEL_EXPORT_ERROR(10301,"Excel 导出失败! - 耗时:{}毫秒 - 失败信息:{}"),
EXCEL_IMPORT_SUCCESS(10302,"EXCEL 导入成功! - 耗时:{}毫秒"),
EXCEL_IMPORT_ERROR(10303,"Excel导入失败! - 耗时:{}毫秒 - 失败信息{}"),
EXCEL_IMPORT_NO(10304,""),
EXCEL_FILE_NULL(10305,"请选择文件"),

@ -8,7 +8,7 @@ import com.github.pagehelper.PageSerializable;
import java.util.HashMap;
import java.util.Map;
public class Page<E,T> extends PageSerializable<E>{
public class Page<T,E> extends PageSerializable<E>{
private int pageNo = 1;

@ -1,5 +1,6 @@
package org.opsli.core.utils;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
@ -7,6 +8,7 @@ import org.opsli.api.base.result.ResultVo;
import org.opsli.api.web.system.dict.DictDetailApi;
import org.opsli.api.wrapper.system.dict.DictModel;
import org.opsli.api.wrapper.system.dict.SysDictDetailModel;
import org.opsli.common.constants.CacheConstants;
import org.opsli.common.constants.DictConstants;
import org.opsli.core.cache.local.CacheUtil;
import org.opsli.plugins.redis.RedisLockPlugins;
@ -195,11 +197,16 @@ public class DictUtil {
public static List<DictModel> getDictList(String typeCode){
List<DictModel> dictModels = Lists.newArrayList();
try {
Map<Object, Object> dictMap = redisPlugin.hGetAll(DictConstants.CACHE_PREFIX_VALUE + typeCode);
String key = CacheUtil.handleKey(CacheConstants.EDEN_HASH_DATA, DictConstants.CACHE_PREFIX_VALUE + typeCode);
Map<Object, Object> dictMap = redisPlugin.hGetAll(key);
Set<Map.Entry<Object, Object>> entries = dictMap.entrySet();
for (Map.Entry<Object, Object> entry : entries) {
// 赋值
SysDictDetailModel model = (SysDictDetailModel) entry.getValue();
JSONObject jsonObject = (JSONObject) entry.getValue();
if(jsonObject == null){
continue;
}
SysDictDetailModel model = jsonObject.toJavaObject(SysDictDetailModel.class);
DictModel dictModel = new DictModel();
dictModel.setTypeCode(typeCode);
dictModel.setDictName(model.getDictName());

@ -0,0 +1,155 @@
package org.opsli.core.utils;
import cn.hutool.core.util.ReflectUtil;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.opsli.api.wrapper.system.dict.DictModel;
import org.opsli.common.annotation.DictType;
import org.opsli.common.enums.ExcelOperate;
import org.opsli.plugins.excel.ExcelPlugin;
import org.opsli.plugins.excel.exception.ExcelPluginException;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;
/**
* @BelongsProject: opsli-boot
* @BelongsPackage: org.opsli.core.utils
* @Author: Parker
* @CreateTime: 2020-09-23 21:53
* @Description: ExcelUtil
*/
@Slf4j
@Component
public class ExcelUtil extends ExcelPlugin {
@Override
public <T> List<T> readExcel(MultipartFile excel, Class<T> rowModel) throws ExcelPluginException {
List<T> ts = super.readExcel(excel, rowModel);
// 处理数据
return this.handleDict(ts, rowModel, ExcelOperate.READ);
}
@Override
public <T> List<T> readExcel(MultipartFile excel, Class<T> rowModel, String sheetName) throws ExcelPluginException {
List<T> ts = super.readExcel(excel, rowModel, sheetName);
// 处理数据
return this.handleDict(ts, rowModel, ExcelOperate.READ);
}
@Override
public <T> List<T> readExcel(MultipartFile excel, Class<T> rowModel, String sheetName, int headLineNum) throws ExcelPluginException {
List<T> ts = super.readExcel(excel, rowModel, sheetName, headLineNum);
// 处理数据
return this.handleDict(ts, rowModel, ExcelOperate.READ);
}
@Override
public <T> void writeExcel(HttpServletResponse response, List<T> list, String fileName, String sheetName, Class<T> classType, ExcelTypeEnum excelTypeEnum) throws ExcelPluginException {
// 处理数据
List<T> ts = this.handleDict(list, classType, ExcelOperate.WRITE);
super.writeExcel(response, ts, fileName, sheetName, classType, excelTypeEnum);
}
/**
*
* @param datas
* @param typeClazz
* @param operate
* @param <T>
* @return
*/
private <T> List<T> handleDict(List<T> datas, Class<T> typeClazz, ExcelOperate operate){
// 空处理
if(datas == null || datas.size() == 0){
return datas;
}
// 字段名 - 字典code
Map<String,String> fieldAndTypeCode = Maps.newHashMap();
// 字典code - 字典值
Map<String,List<DictModel>> typeCodeAndValue = Maps.newHashMap();
Field[] fields = ReflectUtil.getFields(typeClazz);
for (Field field : fields) {
DictType dictType = field.getAnnotation(DictType.class);
if(dictType != null){
String typeCode = dictType.value();
fieldAndTypeCode.put(field.getName(), typeCode);
}
}
// 如果没有字典 则直接返回
if(fieldAndTypeCode.size() == 0){
return datas;
}
// 取Redis 值
for (Map.Entry<String, String> entry : fieldAndTypeCode.entrySet()) {
String typeCode = entry.getValue();
List<DictModel> dictList = DictUtil.getDictList(typeCode);
// 如果字典 List 为空 则走下一个
if(dictList == null || dictList.size() == 0) continue;
typeCodeAndValue.put(typeCode, dictList);
}
// 数据字典赋值
for (T data : datas) {
for (Map.Entry<String, String> entry : fieldAndTypeCode.entrySet()) {
try {
String fieldName = entry.getKey();
String typeCode = entry.getValue();
String fieldValue = (String) ReflectUtil.getFieldValue(data, fieldName);
List<DictModel> dictModels = typeCodeAndValue.get(typeCode);
// 匹配字典
String dictVal = this.matchingDict(dictModels, fieldValue, operate);
if(StringUtils.isEmpty(dictVal)){
continue;
}
// 赋值
ReflectUtil.setFieldValue(data, fieldName, dictVal);
}catch (Exception e){
log.error(e.getMessage(), e);
}
}
}
return datas;
}
/**
*
* @param dictModels
* @param fieldValue
* @param operate
* @return
*/
private String matchingDict(List<DictModel> dictModels, String fieldValue, ExcelOperate operate){
String val = "";
for (DictModel dictModel : dictModels) {
// 读操作
if(ExcelOperate.READ == operate){
// 判断 Excel 读入 字典名称是否与 当前字典匹配
if(dictModel.getDictName().equals(fieldValue)){
val = dictModel.getDictValue();
break;
}
}
// 写操作
else if(ExcelOperate.WRITE == operate){
// 判断 Excel 写出 字典值是否与 当前字典匹配
if(dictModel.getDictValue().equals(fieldValue)){
val = dictModel.getDictName();
break;
}
}
}
return val;
}
}

@ -33,8 +33,9 @@
(
a.dict_name = #{dictName}
or
a.type_code = #{typeCode}
a.dict_value = #{dictValue}
)
and a.type_code = #{typeCode}
and a.deleted = 0
<if test="id != null and id != ''">
and a.id != #{id}

@ -13,7 +13,7 @@ import java.util.List;
* @CreateTime: 2020-09-17 13:07
* @Description:
*/
public interface IDictDetailService extends CrudServiceInterface<SysDictDetailModel, SysDictDetail> {
public interface IDictDetailService extends CrudServiceInterface<SysDictDetail, SysDictDetailModel> {
/**
* ID

@ -12,7 +12,7 @@ import org.opsli.modulars.system.dict.entity.SysDict;
* @CreateTime: 2020-09-17 13:07
* @Description:
*/
public interface IDictService extends CrudServiceInterface<SysDictModel, SysDict> {
public interface IDictService extends CrudServiceInterface<SysDict, SysDictModel> {
}

@ -39,7 +39,7 @@ import java.util.List;
* @Description:
*/
@Service
public class DictDetailServiceImpl extends CrudServiceImpl<DictDetailMapper, SysDictDetailModel, SysDictDetail> implements IDictDetailService {
public class DictDetailServiceImpl extends CrudServiceImpl<DictDetailMapper, SysDictDetail, SysDictDetailModel> implements IDictDetailService {
@Autowired(required = false)
private DictDetailMapper mapper;
@ -62,7 +62,7 @@ public class DictDetailServiceImpl extends CrudServiceImpl<DictDetailMapper, Sys
Integer count = mapper.uniqueVerificationByNameOrValue(entity);
if(count != null && count > 0){
// 重复
throw new ServiceException(SystemMsg.EXCEL_DICT_UNIQUE);
throw new ServiceException(SystemMsg.EXCEL_DICT_DETAIL_UNIQUE);
}
SysDictDetailModel ret = super.insert(model);

@ -27,7 +27,7 @@ import java.util.List;
* @Description:
*/
@Service
public class DictServiceImpl extends CrudServiceImpl<DictMapper, SysDictModel, SysDict> implements IDictService {
public class DictServiceImpl extends CrudServiceImpl<DictMapper, SysDict, SysDictModel> implements IDictService {
@Autowired(required = false)
private DictMapper mapper;

@ -29,7 +29,7 @@ import java.util.List;
*/
@Slf4j
@ApiRestController("/dict/detail")
public class DictDetailRestController extends BaseRestController<SysDictDetailModel, SysDictDetail, IDictDetailService>
public class DictDetailRestController extends BaseRestController<SysDictDetail, SysDictDetailModel, IDictDetailService>
implements DictDetailApi {
@ -56,7 +56,7 @@ public class DictDetailRestController extends BaseRestController<SysDictDetailMo
public ResultVo<?> findPage(Integer pageNo, Integer pageSize, HttpServletRequest request) {
QueryBuilder<SysDictDetail> queryBuilder = new WebQueryBuilder<>(SysDictDetail.class, request.getParameterMap());
Page<SysDictDetailModel, SysDictDetail> page = new Page<>(pageNo, pageSize);
Page<SysDictDetail, SysDictDetailModel> page = new Page<>(pageNo, pageSize);
page.setQueryWrapper(queryBuilder.build());
page = IService.findPage(page);

@ -28,7 +28,7 @@ import javax.servlet.http.HttpServletResponse;
*/
@Slf4j
@ApiRestController("/dict")
public class DictRestController extends BaseRestController<SysDictModel, SysDict, IDictService>
public class DictRestController extends BaseRestController<SysDict, SysDictModel, IDictService>
implements DictApi {
@ -55,7 +55,7 @@ public class DictRestController extends BaseRestController<SysDictModel, SysDict
public ResultVo<?> findPage(Integer pageNo, Integer pageSize, HttpServletRequest request) {
QueryBuilder<SysDict> queryBuilder = new WebQueryBuilder<>(SysDict.class, request.getParameterMap());
Page<SysDictModel, SysDict> page = new Page<>(pageNo, pageSize);
Page<SysDict, SysDictModel> page = new Page<>(pageNo, pageSize);
page.setQueryWrapper(queryBuilder.build());
page = IService.findPage(page);

@ -18,6 +18,9 @@ public class TestEntity extends BaseEntity {
/** 名称 */
private String name;
/** 分类 */
private String type;
/** 备注 */
private String remark;

@ -6,6 +6,7 @@
<sql id="TestEntityColumns">
a.id as id,
a.name as name,
a.type as type,
a.remark as remark,
a.create_by as createBy,
a.create_time as createTime,

@ -11,7 +11,7 @@ import org.opsli.modulars.test.entity.TestEntity;
* @CreateTime: 2020-09-17 13:07
* @Description:
*/
public interface ITestService extends CrudServiceInterface<TestModel,TestEntity> {
public interface ITestService extends CrudServiceInterface<TestEntity,TestModel> {
/**
*

@ -31,7 +31,7 @@ import java.util.Collection;
@Service
// 开启热数据标示 不加不生效
@EnableHotData
public class TestServiceImpl extends CrudServiceImpl<TestMapper, TestModel, TestEntity> implements ITestService {
public class TestServiceImpl extends CrudServiceImpl<TestMapper, TestEntity,TestModel > implements ITestService {
@Autowired(required = false)
private TestMapper mapper;

@ -1,6 +1,7 @@
package org.opsli.modulars.test.web;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.RandomUtil;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
@ -49,7 +50,7 @@ import java.util.concurrent.TimeUnit;
*/
@Slf4j
@ApiRestController("/test")
public class TestRestRestController extends BaseRestController<TestModel, TestEntity, ITestService>
public class TestRestRestController extends BaseRestController<TestEntity, TestModel, ITestService>
implements TestApi {
@ -267,7 +268,7 @@ public class TestRestRestController extends BaseRestController<TestModel, TestEn
public ResultVo<?> findPage(Integer pageNo, Integer pageSize, HttpServletRequest request) {
QueryBuilder<TestEntity> queryBuilder = new WebQueryBuilder<>(TestEntity.class, request.getParameterMap());
Page<TestModel, TestEntity> page = new Page<>(pageNo, pageSize);
Page<TestEntity,TestModel> page = new Page<>(pageNo, pageSize);
page.setQueryWrapper(queryBuilder.build());
page = IService.findPage(page);
@ -294,6 +295,31 @@ public class TestRestRestController extends BaseRestController<TestModel, TestEn
}
/**
*
* @return
*/
@ApiOperation(value = "批量插入1000个随机新增数据", notes = "批量插入1000个随机新增数据")
@GetMapping("/insertAll")
public ResultVo<Boolean> insertAll(){
List<DictModel> testType = DictUtil.getDictList("testType");
List<TestModel> datas = new ArrayList<>();
// 转化对象 处理 ApiModel 与 本地对象
for (int i = 0; i < 1000; i++) {
int randomNum = RandomUtil.randomInt(0, testType.size());
TestModel model = new TestModel();
model.setName("测试名称"+random.nextInt());
model.setType(testType.get(randomNum).getDictValue());
model.setRemark("测试备注"+random.nextInt());
datas.add(model);
}
// 调用新增方法
boolean b = IService.insertBatch(datas);
return ResultVo.success("新增成功",b);
}
@ApiOperation(value = "获得字典 - By Name", notes = "获得字典 - By Name")
@GetMapping("/getDictByName")

@ -30,11 +30,7 @@ import java.util.List;
* @author parker
*/
@Slf4j
public final class ExcelUtil {
/**
*
*/
private ExcelUtil(){}
public class ExcelPlugin {
/**
* Excel( sheet)
@ -45,7 +41,7 @@ public final class ExcelUtil {
* @param rowModel BaseRowModel
* @return Excel list
*/
public static <T> List<T> readExcel(MultipartFile excel,Class<T> rowModel)
public <T> List<T> readExcel(MultipartFile excel,Class<T> rowModel)
throws ExcelPluginException {
return readExcel(excel, rowModel, null, 1);
}
@ -57,7 +53,7 @@ public final class ExcelUtil {
* @param sheetName sheet 1
* @return Excel list
*/
public static <T> List<T> readExcel(MultipartFile excel, Class<T> rowModel, String sheetName)
public <T> List<T> readExcel(MultipartFile excel, Class<T> rowModel, String sheetName)
throws ExcelPluginException{
return readExcel(excel, rowModel, sheetName, 1);
}
@ -70,7 +66,7 @@ public final class ExcelUtil {
* @param headLineNum 1
* @return Excel list
*/
public static <T> List<T> readExcel(MultipartFile excel, Class<T> rowModel, String sheetName,
public <T> List<T> readExcel(MultipartFile excel, Class<T> rowModel, String sheetName,
int headLineNum) throws ExcelPluginException {
ExcelListener excelListener = new ExcelListener();
InputStream inputStream = null;
@ -111,7 +107,7 @@ public final class ExcelUtil {
* @param fileName
* @param sheetName sheet
*/
public static <T> void writeExcel(HttpServletResponse response, List<T> list,
public <T> void writeExcel(HttpServletResponse response, List<T> list,
String fileName, String sheetName,
Class<T> classType, ExcelTypeEnum excelTypeEnum)
throws ExcelPluginException{
@ -141,7 +137,7 @@ public final class ExcelUtil {
/**
* WriterOutputStream
*/
private static OutputStream getOutputStream(String fileName, HttpServletResponse response,
private OutputStream getOutputStream(String fileName, HttpServletResponse response,
ExcelTypeEnum excelTypeEnum)
throws ExcelPluginException{
//创建本地文件
@ -161,7 +157,7 @@ public final class ExcelUtil {
* @param excel Excel
* @param excelListener new ExcelListener()
*/
private static ExcelReader getReader(MultipartFile excel,
private ExcelReader getReader(MultipartFile excel,
ExcelListener excelListener) throws ExcelPluginException{
String fileName = excel.getOriginalFilename();
if (fileName == null ) {
@ -187,7 +183,7 @@ public final class ExcelUtil {
/**
* BeanCopylist
*/
public static <T> List<T> getExtendsBeanList(List<?> list,Class<T> typeClazz){
public <T> List<T> getExtendsBeanList(List<?> list,Class<T> typeClazz){
return WrapperUtil.cloneTransformInstance(list,typeClazz);
}
}
Loading…
Cancel
Save