提交 b57f723d authored 作者: 000516's avatar 000516 提交者: Coding

Dto转DO通用方法添加到Beanutils,添加PageInfo分页对象,创建自定义异常

Dto转DO通用方法添加到Beanutils,添加PageInfo分页对象,创建自定义异常
...@@ -114,6 +114,11 @@ ...@@ -114,6 +114,11 @@
<artifactId>javax.servlet-api</artifactId> <artifactId>javax.servlet-api</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-core</artifactId>
</dependency>
</dependencies> </dependencies>
</project> </project>
...@@ -16,9 +16,16 @@ public enum ECode implements StatusCode { ...@@ -16,9 +16,16 @@ public enum ECode implements StatusCode {
DB_TABLE_UNIQUE_EXIST(700, "%s已存在,不可重复;"), DB_TABLE_UNIQUE_EXIST(700, "%s已存在,不可重复;"),
DB_TABLE_FLAG_NOTEXIST(701, "当前数据异常或已被他人修改,请刷新重试;"), DB_TABLE_FLAG_NOTEXIST(701, "当前数据异常或已被他人修改,请刷新重试;"),
DB_TABLE_UPDATE_ERROR(702, "数据保存异常,请刷新重试;"), DB_TABLE_UPDATE_ERROR(702, "数据保存异常,请刷新重试;"),
;
/**
* 800+ :Param_参数_统一异常(不分模块)
*/
PARAM_ID_ISNULL_ERROR(800, "缺失 - %sID;"),
PARAM_CODE_ISNULL_ERROR(801, "缺失 - %s编码;"),
;
private int code; private int code;
private String msg; private String msg;
......
...@@ -18,6 +18,11 @@ public class CheckedException extends RuntimeException { ...@@ -18,6 +18,11 @@ public class CheckedException extends RuntimeException {
this.msg = String.format(statusCode.getMsg(), strs); this.msg = String.format(statusCode.getMsg(), strs);
} }
public CheckedException(StatusCode statusCode) {
this.code = statusCode.getCode();
this.msg = statusCode.getMsg();
}
public int getCode() { public int getCode() {
return code; return code;
} }
......
...@@ -9,50 +9,63 @@ public class GlobalException extends RuntimeException ...@@ -9,50 +9,63 @@ public class GlobalException extends RuntimeException
{ {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
/** private int code;
* 错误提示 private String msg;
*/
private String message;
/** public GlobalException(int code) {
* 错误明细,内部调试错误 this.code = code;
* this.msg = "请求失败";
* 和 {@link CommonResult#getDetailMessage()} 一致的设计
*/
private String detailMessage;
/**
* 空构造方法,避免反序列化问题
*/
public GlobalException()
{
} }
public GlobalException(String message) public int getCode() {
{ return code;
this.message = message;
} }
public String getDetailMessage() public String getMsg() {
{ return msg;
return detailMessage;
} }
public GlobalException setDetailMessage(String detailMessage)
{
this.detailMessage = detailMessage;
return this;
}
@Override //
public String getMessage() // /**
{ // * 错误明细,内部调试错误
return message; // *
} // * 和 {@link CommonResult#getDetailMessage()} 一致的设计
// */
public GlobalException setMessage(String message) // private String detailMessage;
{ //
this.message = message; // /**
return this; // * 空构造方法,避免反序列化问题
} // */
// public GlobalException()
// {
// }
//
// public GlobalException(String message)
// {
// this.message = message;
// }
//
// public String getDetailMessage()
// {
// return detailMessage;
// }
//
// public GlobalException setDetailMessage(String detailMessage)
// {
// this.detailMessage = detailMessage;
// return this;
// }
//
// @Override
// public String getMessage()
// {
// return message;
// }
//
// public GlobalException setMessage(String message)
// {
// this.message = message;
// return this;
// }
} }
\ No newline at end of file
package com.sfa.common.core.utils.bean;
import java.util.ArrayList;
import java.util.List;
/**
* @author : liqiulin
* @date : 2024-10-31 17
* @describe : 对象转换
*/
public class BeanTransitions<T> extends org.springframework.beans.BeanUtils {
//
// /**
// * @param tos DO对象List
// * @return DTO对象
// */
// public <P> List<P> transitionDtos(List<T> tos, Class<P> p) {
// if (tos == null || tos.isEmpty()) {
// return new ArrayList<>();
// }
//
// List<P> pos = new ArrayList<>(tos.size() * 2);
// for (T to : tos) {
// pos.add(transitionDto(to, p));
// }
// return pos;
// }
//
// public <P> P transitionDto(T to, Class<P> p) {
// if (to == null) {
// return null;
// }
// P po = null;
// try {
// po = p.newInstance();
// BeanUtils.copyProperties(to, po);
// } catch (InstantiationException e) {
// throw new RuntimeException(e);
// } catch (IllegalAccessException e) {
// throw new RuntimeException(e);
// }
// return po;
// }
}
...@@ -107,4 +107,41 @@ public class BeanUtils extends org.springframework.beans.BeanUtils ...@@ -107,4 +107,41 @@ public class BeanUtils extends org.springframework.beans.BeanUtils
{ {
return m1.substring(BEAN_METHOD_PROP_INDEX).equals(m2.substring(BEAN_METHOD_PROP_INDEX)); return m1.substring(BEAN_METHOD_PROP_INDEX).equals(m2.substring(BEAN_METHOD_PROP_INDEX));
} }
public static <T> List<T> transitionDtos(Object sourceList, Class<?> beanClass) {
if (sourceList == null) {
return new ArrayList<>();
}
List<Object> sList = (List<Object>) sourceList;
if (sList.isEmpty()) {
return new ArrayList<>();
}
List<Object> btoList = new ArrayList<>(sList.size() * 2);
try {
for (Object so : sList) {
Object bto = beanClass.newInstance();
BeanUtils.copyProperties(so, bto);
btoList.add(bto);
}
return (List<T>) btoList;
} catch (Exception e) {
return new ArrayList<>();
}
}
public static <T> T transitionDto(Object source, Class<?> beanClass) {
if (source == null) {
return null;
}
try {
Object bto = beanClass.newInstance();
BeanUtils.copyProperties(source, bto);
return (T)bto;
} catch (Exception e) {
return null;
}
}
} }
package com.sfa.common.core.web.domain;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sfa.common.core.constant.HttpStatus;
import com.sfa.common.core.exception.GlobalException;
import com.sfa.common.core.utils.bean.BeanUtils;
import org.springframework.util.CollectionUtils;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* @author : liqiulin
* @date : 2024-04-19 13
* @describe : 分页配置,默认每页10条记录
*/
public class PageInfo<T> implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 当前页数,第1页开始
*/
private int pageNum = 1;
/**
* 每页条数
*/
private int pageSize = 1;
/**
* 总条数
*/
private int total;
/**
* 总页数
*/
private int totalPage;
private List<T> rows;
private Map<String,Object> queryParams;
/**
*
* current:当前页数,必须大于等于 1,默认值为 1。
* size:每页条数,默认值为 10。
* total:总条数,默认值为 0。
* records:当前页数据集合,默认值为空集合。
* searchCount:是否进行 count 查询,默认值为 true,表示会统计总条数。
* pages:总页数,通过计算得出。
* optimizeCountSql:是否优化 count 查询,默认值为 true。
* hitCount:是否对 count 进行 limit 优化,默认值为 false。
* countId:count 查询的列名,默认为 null,表示所有列。
* maxLimit:设置最大的 limit 查询限制,默认值为 -1,表示不限制。
* @param iPage 查询出的分页对象
* @param t 需要转换的对象(用于返回前端,不能直接返回DO)
* @param <P> 分页对象中的类型
*/
public <P> void pageCovert(IPage<P> iPage, Class<T> t) {
this.total = (int) iPage.getTotal();
this.totalPage = (int) iPage.getPages();
List<P> iPageRecords = iPage.getRecords();
if (CollectionUtils.isEmpty(iPageRecords)) {
this.rows = new ArrayList<>();
return;
}
try {
List<T> tList = new ArrayList<>(iPageRecords.size() * 2);
for (P pr : iPageRecords) {
T newT = t.newInstance();
// 把原对象数据拷贝到新的对象
BeanUtils.copyProperties(pr, newT);
tList.add(newT);
}
this.rows = tList;
} catch (Exception e) {
throw new GlobalException(HttpStatus.ERROR);
}
}
public <P> void pageCovert(IPage<P> iPage) {
this.total = (int) iPage.getTotal();
this.totalPage = (int) iPage.getPages();
}
public int getSkipNum(){
return this.pageSize * (this.pageNum - 1);
}
public int getPageNum() {
return pageNum;
}
public void setPageNum(int pageNum) {
this.pageNum = pageNum;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public List<T> getRows() {
return rows;
}
public void setRows(List<T> rows) {
this.rows = rows;
}
public Map<String, Object> getQueryParams() {
return queryParams;
}
public void setQueryParams(Map<String, Object> queryParams) {
this.queryParams = queryParams;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论