1
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
package com.cym.bean;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ ElementType.FIELD })
|
||||
public @interface CreateTime {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.cym.bean;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ ElementType.FIELD })
|
||||
public @interface InitValue {
|
||||
|
||||
String value();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package com.cym.bean;
|
||||
|
||||
public class OrderBy {
|
||||
Direction direction;
|
||||
String column;
|
||||
String orderByStr;
|
||||
|
||||
|
||||
|
||||
public static enum Direction {
|
||||
ASC, DESC;
|
||||
}
|
||||
|
||||
public OrderBy(Direction direction, String column) {
|
||||
this.direction = direction;
|
||||
this.column = column;
|
||||
}
|
||||
|
||||
public OrderBy() {
|
||||
|
||||
}
|
||||
|
||||
|
||||
public String getOrderByStr() {
|
||||
return orderByStr;
|
||||
}
|
||||
|
||||
public void setOrderByStr(String orderByStr) {
|
||||
this.orderByStr = orderByStr;
|
||||
}
|
||||
|
||||
public Direction getDirection() {
|
||||
return direction;
|
||||
}
|
||||
|
||||
public void setDirection(Direction direction) {
|
||||
this.direction = direction;
|
||||
}
|
||||
|
||||
public String getColumn() {
|
||||
return column;
|
||||
}
|
||||
|
||||
public void setColumn(String column) {
|
||||
this.column = column;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
package com.cym.bean;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
|
||||
@Schema(description = " 分页返回类")
|
||||
public class Page<T> {
|
||||
@Schema(description = "总记录数")
|
||||
Long total = 0l;
|
||||
@Schema(description = "起始页,从1开始")
|
||||
Integer pageNum = 1;
|
||||
@Schema(description = "每页记录数,默认为10")
|
||||
Integer pageSize = 10;
|
||||
|
||||
@Schema(description = "起始编号")
|
||||
Integer startRow;
|
||||
@Schema(description = "结束编号")
|
||||
Integer endRow;
|
||||
@Schema(description = "总页数")
|
||||
Integer pages;
|
||||
|
||||
@Schema(description = "内容列表")
|
||||
List<T> list;
|
||||
|
||||
public Page() {
|
||||
|
||||
}
|
||||
|
||||
public Page(Integer pageNum, Integer pageSize) {
|
||||
this.setPageNum(pageNum);
|
||||
this.setPageSize(pageSize);
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空List
|
||||
*/
|
||||
public void clearList() {
|
||||
list = new ArrayList<>();
|
||||
}
|
||||
|
||||
public Long getTotal() {
|
||||
return total;
|
||||
}
|
||||
|
||||
public void setTotal(Long total) {
|
||||
this.total = total;
|
||||
}
|
||||
|
||||
public Integer getPageNum() {
|
||||
return pageNum;
|
||||
}
|
||||
|
||||
public void setPageNum(Integer pageNum) {
|
||||
this.pageNum = pageNum;
|
||||
}
|
||||
|
||||
public Integer getPageSize() {
|
||||
return pageSize;
|
||||
}
|
||||
|
||||
public void setPageSize(Integer pageSize) {
|
||||
this.pageSize = pageSize;
|
||||
}
|
||||
|
||||
public List<T> getList() {
|
||||
return list;
|
||||
}
|
||||
|
||||
public void setList(List<T> list) {
|
||||
this.list = list;
|
||||
}
|
||||
|
||||
public Integer getStartRow() {
|
||||
return startRow;
|
||||
}
|
||||
|
||||
public void setStartRow(Integer startRow) {
|
||||
this.startRow = startRow;
|
||||
}
|
||||
|
||||
public Integer getEndRow() {
|
||||
return endRow;
|
||||
}
|
||||
|
||||
public void setEndRow(Integer endRow) {
|
||||
this.endRow = endRow;
|
||||
}
|
||||
|
||||
public Integer getPages() {
|
||||
return pages;
|
||||
}
|
||||
|
||||
public void setPages(Integer pages) {
|
||||
this.pages = pages;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
package com.cym.bean;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.cym.bean.OrderBy.Direction;
|
||||
import com.cym.reflection.ReflectionUtil;
|
||||
import com.cym.reflection.SerializableFunction;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
|
||||
public class Sort {
|
||||
List<OrderBy> orderList = new ArrayList<>();
|
||||
|
||||
public Sort() {
|
||||
|
||||
}
|
||||
|
||||
public Sort(String column, Direction direction) {
|
||||
OrderBy order = new OrderBy();
|
||||
order.setColumn(column);
|
||||
order.setDirection(direction);
|
||||
|
||||
orderList.add(order);
|
||||
}
|
||||
|
||||
public Sort(List<OrderBy> orderList) {
|
||||
this.orderList.addAll(orderList);
|
||||
}
|
||||
|
||||
public Sort(String orderByStr) {
|
||||
OrderBy order = new OrderBy();
|
||||
order.setOrderByStr(orderByStr);
|
||||
|
||||
orderList.add(order);
|
||||
}
|
||||
|
||||
public <T, R> Sort(SerializableFunction<T, R> column, Direction direction) {
|
||||
OrderBy order = new OrderBy();
|
||||
order.setColumn(ReflectionUtil.getFieldName(column));
|
||||
order.setDirection(direction);
|
||||
|
||||
orderList.add(order);
|
||||
}
|
||||
|
||||
public Sort add(String column, Direction direction) {
|
||||
OrderBy order = new OrderBy();
|
||||
order.setColumn(column);
|
||||
order.setDirection(direction);
|
||||
|
||||
orderList.add(order);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public <T, R> Sort add(SerializableFunction<T, R> column, Direction direction) {
|
||||
OrderBy order = new OrderBy();
|
||||
order.setColumn(ReflectionUtil.getFieldName(column));
|
||||
order.setDirection(direction);
|
||||
|
||||
orderList.add(order);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public Sort add(String orderByStr) {
|
||||
OrderBy order = new OrderBy();
|
||||
order.setOrderByStr(orderByStr);
|
||||
|
||||
orderList.add(order);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
List<String> sqlList = new ArrayList<>();
|
||||
for (OrderBy order : orderList) {
|
||||
if (StrUtil.isEmpty(order.getOrderByStr())) {
|
||||
String sql = "`" + StrUtil.toUnderlineCase(order.getColumn()) + "`";
|
||||
|
||||
if (order.getDirection() == Direction.ASC) {
|
||||
sql += " ASC";
|
||||
}
|
||||
if (order.getDirection() == Direction.DESC) {
|
||||
sql += " DESC";
|
||||
}
|
||||
|
||||
sqlList.add(sql);
|
||||
} else {
|
||||
sqlList.add(order.getOrderByStr());
|
||||
}
|
||||
}
|
||||
if (sqlList.size() > 0) {
|
||||
return " ORDER BY " + StrUtil.join(",", sqlList);
|
||||
} else {
|
||||
return "";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public List<OrderBy> getOrderList() {
|
||||
return orderList;
|
||||
}
|
||||
|
||||
public void setOrderList(List<OrderBy> orderList) {
|
||||
this.orderList = orderList;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.cym.bean;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import com.cym.reflection.ReflectionUtil;
|
||||
import com.cym.reflection.SerializableFunction;
|
||||
|
||||
public class Update {
|
||||
|
||||
Map<String, Object> sets;
|
||||
|
||||
public Update() {
|
||||
sets = new HashMap<String, Object>();
|
||||
}
|
||||
|
||||
public Update set(String key, Object value) {
|
||||
sets.put(key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public <T, R> Update set(SerializableFunction<T, R> property, Object value) {
|
||||
sets.put(ReflectionUtil.getFieldName(property), value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Map<String, Object> getSets() {
|
||||
return sets;
|
||||
}
|
||||
|
||||
public void setSets(Map<String, Object> sets) {
|
||||
this.sets = sets;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.cym.bean;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ ElementType.FIELD })
|
||||
public @interface UpdateTime {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package com.cym.reflection;
|
||||
|
||||
import java.beans.Introspector;
|
||||
import java.lang.invoke.SerializedLambda;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
public class ReflectionUtil {
|
||||
|
||||
private static Map<SerializableFunction<?, ?>, Field> cache = new ConcurrentHashMap<>();
|
||||
|
||||
public static <T, R> String getFieldName(SerializableFunction<T, R> function) {
|
||||
Field field = ReflectionUtil.getField(function);
|
||||
return field.getName();
|
||||
}
|
||||
|
||||
public static Field getField(SerializableFunction<?, ?> function) {
|
||||
return cache.computeIfAbsent(function, ReflectionUtil::findField);
|
||||
}
|
||||
|
||||
public static Field findField(SerializableFunction<?, ?> function) {
|
||||
Field field = null;
|
||||
String fieldName = null;
|
||||
try {
|
||||
// 第1步 获取SerializedLambda
|
||||
Method method = function.getClass().getDeclaredMethod("writeReplace");
|
||||
method.setAccessible(Boolean.TRUE);
|
||||
SerializedLambda serializedLambda = (SerializedLambda) method.invoke(function);
|
||||
// 第2步 implMethodName 即为Field对应的Getter方法名
|
||||
String implMethodName = serializedLambda.getImplMethodName();
|
||||
if (implMethodName.startsWith("get") && implMethodName.length() > 3) {
|
||||
fieldName = Introspector.decapitalize(implMethodName.substring(3));
|
||||
|
||||
} else if (implMethodName.startsWith("is") && implMethodName.length() > 2) {
|
||||
fieldName = Introspector.decapitalize(implMethodName.substring(2));
|
||||
} else if (implMethodName.startsWith("lambda$")) {
|
||||
throw new IllegalArgumentException("SerializableFunction不能传递lambda表达式,只能使用方法引用");
|
||||
|
||||
} else {
|
||||
throw new IllegalArgumentException(implMethodName + "不是Getter方法引用");
|
||||
}
|
||||
// 第3步 获取的Class是字符串,并且包名是“/”分割,需要替换成“.”,才能获取到对应的Class对象
|
||||
String declaredClass = serializedLambda.getImplClass().replace("/", ".");
|
||||
Class<?> aClass = Class.forName(declaredClass, false, ClassUtils.getDefaultClassLoader());
|
||||
|
||||
// 第4步 Spring 中的反射工具类获取Class中定义的Field
|
||||
field = ReflectionUtils.findField(aClass, fieldName);
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
// 第5步 如果没有找到对应的字段应该抛出异常
|
||||
if (field != null) {
|
||||
return field;
|
||||
}
|
||||
throw new NoSuchFieldError(fieldName);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.cym.reflection;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.function.Function;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface SerializableFunction<T, R> extends Function<T, R>, Serializable {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package com.cym.utils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.cym.bean.Page;
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
|
||||
public class BeanExtUtil {
|
||||
|
||||
/**
|
||||
* 根据List对象属性批量创建对应的Class List对象
|
||||
*
|
||||
* @param list
|
||||
* @param clazz
|
||||
* @return
|
||||
*/
|
||||
public static <T> List<T> copyListByProperties(List<?> list, Class<T> clazz) {
|
||||
if (list == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
List<T> rsList = new ArrayList<>();
|
||||
for (Object source : list) {
|
||||
rsList.add((T) BeanUtil.copyProperties(source, clazz));
|
||||
}
|
||||
|
||||
return rsList;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 根据PageResp对象属性批量创建对应的Class PageResp对象
|
||||
*
|
||||
* @param pageResp
|
||||
* @param clazz
|
||||
* @return
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> Page<T> copyPageByProperties(Page<?> page, Class<T> clazz) {
|
||||
Page<T> pageNew = copyBeanByProperties(page, Page.class);
|
||||
pageNew.setList(copyListByProperties(page.getList(), clazz));
|
||||
return pageNew;
|
||||
}
|
||||
|
||||
/**
|
||||
* 按照Bean对象属性创建对应的Class对象
|
||||
*
|
||||
*/
|
||||
public static <T> T copyBeanByProperties(Object source, Class<T> tClass) {
|
||||
return BeanUtil.copyProperties(source, tClass);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package com.cym.utils;
|
||||
|
||||
public class Condition {
|
||||
public Condition(String column, String operation, Object value) {
|
||||
this.column = column;
|
||||
this.operation = operation;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public Condition(String condition) {
|
||||
this.condition = condition;
|
||||
}
|
||||
|
||||
String column;
|
||||
String operation;
|
||||
Object value;
|
||||
String condition;
|
||||
|
||||
|
||||
|
||||
public String getCondition() {
|
||||
return condition;
|
||||
}
|
||||
|
||||
public void setCondition(String condition) {
|
||||
this.condition = condition;
|
||||
}
|
||||
|
||||
public String getColumn() {
|
||||
return column;
|
||||
}
|
||||
|
||||
public void setColumn(String column) {
|
||||
this.column = column;
|
||||
}
|
||||
|
||||
public String getOperation() {
|
||||
return operation;
|
||||
}
|
||||
|
||||
public void setOperation(String operation) {
|
||||
this.operation = operation;
|
||||
}
|
||||
|
||||
public Object getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(Object value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,351 @@
|
||||
package com.cym.utils;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
|
||||
import com.cym.reflection.ReflectionUtil;
|
||||
import com.cym.reflection.SerializableFunction;
|
||||
|
||||
/**
|
||||
* 查询语句生成器 AND连接
|
||||
*
|
||||
*/
|
||||
public class ConditionAndWrapper extends ConditionWrapper {
|
||||
|
||||
public ConditionAndWrapper() {
|
||||
andLink = true;
|
||||
}
|
||||
|
||||
public ConditionAndWrapper and(ConditionWrapper conditionWrapper) {
|
||||
list.add(conditionWrapper);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 自由撰写条件
|
||||
*
|
||||
* @param condition 条件
|
||||
* @return ConditionWrapper
|
||||
*/
|
||||
public ConditionAndWrapper custom(String condition) {
|
||||
super.custom(condition);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 等于
|
||||
*
|
||||
* @param column 字段
|
||||
* @param params 参数
|
||||
* @return ConditionWrapper
|
||||
*/
|
||||
public ConditionAndWrapper eq(String column, Object params) {
|
||||
super.eq(column, params);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 等于
|
||||
*
|
||||
* @param column 字段
|
||||
* @param params 参数
|
||||
* @return ConditionWrapper
|
||||
*/
|
||||
public <T, R> ConditionAndWrapper eq(SerializableFunction<T, R> column, Object params) {
|
||||
super.eq(ReflectionUtil.getFieldName(column), params);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 不等于
|
||||
*
|
||||
* @param column 字段
|
||||
* @param params 参数
|
||||
* @return ConditionAndWrapper
|
||||
*/
|
||||
public ConditionAndWrapper ne(String column, Object params) {
|
||||
super.ne(column, params);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 不等于
|
||||
*
|
||||
* @param column 字段
|
||||
* @param params 参数
|
||||
* @return ConditionAndWrapper
|
||||
*/
|
||||
public <T, R> ConditionAndWrapper ne(SerializableFunction<T, R> column, Object params) {
|
||||
super.ne(ReflectionUtil.getFieldName(column), params);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 小于
|
||||
*
|
||||
* @param column 字段
|
||||
* @param params 参数
|
||||
* @return ConditionAndWrapper
|
||||
*/
|
||||
public ConditionAndWrapper lt(String column, Object params) {
|
||||
super.lt(column, params);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 小于
|
||||
*
|
||||
* @param column 字段
|
||||
* @param params 参数
|
||||
* @return ConditionAndWrapper
|
||||
*/
|
||||
public <T, R> ConditionAndWrapper lt(SerializableFunction<T, R> column, Object params) {
|
||||
super.lt(ReflectionUtil.getFieldName(column), params);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 小于或等于
|
||||
*
|
||||
* @param column 字段
|
||||
* @param params 参数
|
||||
* @return ConditionAndWrapper
|
||||
*/
|
||||
public ConditionAndWrapper lte(String column, Object params) {
|
||||
super.lte(column, params);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 小于或等于
|
||||
*
|
||||
* @param column 字段
|
||||
* @param params 参数
|
||||
* @return ConditionAndWrapper
|
||||
*/
|
||||
public <T, R> ConditionAndWrapper lte(SerializableFunction<T, R> column, Object params) {
|
||||
super.lte(ReflectionUtil.getFieldName(column), params);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 大于
|
||||
*
|
||||
* @param column 字段
|
||||
* @param params 参数
|
||||
* @return ConditionAndWrapper
|
||||
*/
|
||||
public ConditionAndWrapper gt(String column, Object params) {
|
||||
super.gt(column, params);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 大于
|
||||
*
|
||||
* @param column 字段
|
||||
* @param params 参数
|
||||
* @return ConditionAndWrapper
|
||||
*/
|
||||
public <T, R> ConditionAndWrapper gt(SerializableFunction<T, R> column, Object params) {
|
||||
super.gt(ReflectionUtil.getFieldName(column), params);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 大于或等于
|
||||
*
|
||||
* @param column 字段
|
||||
* @param params 参数
|
||||
* @return ConditionAndWrapper
|
||||
*/
|
||||
public ConditionAndWrapper gte(String column, Object params) {
|
||||
super.gte(column, params);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 大于或等于
|
||||
*
|
||||
* @param column 字段
|
||||
* @param params 参数
|
||||
* @return ConditionAndWrapper
|
||||
*/
|
||||
public <T, R> ConditionAndWrapper gte(SerializableFunction<T, R> column, Object params) {
|
||||
super.gte(ReflectionUtil.getFieldName(column), params);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 相似于
|
||||
*
|
||||
* @param column 字段
|
||||
* @param params 参数
|
||||
* @return ConditionAndWrapper
|
||||
*/
|
||||
public ConditionAndWrapper like(String column, String params) {
|
||||
super.like(column, params);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 相似于
|
||||
*
|
||||
* @param column 字段
|
||||
* @param params 参数
|
||||
* @return ConditionAndWrapper
|
||||
*/
|
||||
public <T, R> ConditionAndWrapper like(SerializableFunction<T, R> column, String params) {
|
||||
super.like(ReflectionUtil.getFieldName(column), params);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 在其中
|
||||
*
|
||||
* @param column 字段
|
||||
* @param params 参数
|
||||
* @return ConditionAndWrapper
|
||||
*/
|
||||
public ConditionAndWrapper in(String column, Collection<?> params) {
|
||||
super.in(column, params);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 在其中
|
||||
*
|
||||
* @param column 字段
|
||||
* @param params 参数
|
||||
* @return ConditionAndWrapper
|
||||
*/
|
||||
public <T, R> ConditionAndWrapper in(SerializableFunction<T, R> column, Collection<?> params) {
|
||||
super.in(ReflectionUtil.getFieldName(column), params);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 在其中
|
||||
*
|
||||
* @param column 字段
|
||||
* @param params 参数
|
||||
* @return ConditionAndWrapper
|
||||
*/
|
||||
public <T, R> ConditionAndWrapper in(String column, Object[] params) {
|
||||
super.in(column, Arrays.asList(params));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 在其中
|
||||
*
|
||||
* @param column 字段
|
||||
* @param params 参数
|
||||
* @return ConditionAndWrapper
|
||||
*/
|
||||
public <T, R> ConditionAndWrapper in(SerializableFunction<T, R> column, Object[] params) {
|
||||
super.in(ReflectionUtil.getFieldName(column), Arrays.asList(params));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 不在其中
|
||||
*
|
||||
* @param column 字段
|
||||
* @param params 参数
|
||||
* @return ConditionAndWrapper
|
||||
*/
|
||||
public ConditionAndWrapper nin(String column, Collection<?> params) {
|
||||
super.nin(column, params);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 不在其中
|
||||
*
|
||||
* @param column 字段
|
||||
* @param params 参数
|
||||
* @return ConditionAndWrapper
|
||||
*/
|
||||
public <T, R> ConditionAndWrapper nin(SerializableFunction<T, R> column, Collection<?> params) {
|
||||
super.nin(ReflectionUtil.getFieldName(column), params);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 不在其中
|
||||
*
|
||||
* @param column 字段
|
||||
* @param params 参数
|
||||
* @return ConditionAndWrapper
|
||||
*/
|
||||
public ConditionAndWrapper nin(String column, Object[] params) {
|
||||
super.nin(column, Arrays.asList(params));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 不在其中
|
||||
*
|
||||
* @param column 字段
|
||||
* @param params 参数
|
||||
* @return ConditionAndWrapper
|
||||
*/
|
||||
public <T, R> ConditionAndWrapper nin(SerializableFunction<T, R> column, Object[] params) {
|
||||
super.nin(ReflectionUtil.getFieldName(column), Arrays.asList(params));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 为空
|
||||
*
|
||||
* @param <T>
|
||||
*
|
||||
* @param column 字段
|
||||
* @return ConditionAndWrapper
|
||||
*/
|
||||
public ConditionAndWrapper isNull(String column) {
|
||||
super.isNull(column);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 为空
|
||||
*
|
||||
* @param <T>
|
||||
*
|
||||
* @param column 字段
|
||||
* @return ConditionAndWrapper
|
||||
*/
|
||||
public <T, R> ConditionAndWrapper isNull(SerializableFunction<T, R> column) {
|
||||
super.isNull(ReflectionUtil.getFieldName(column));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 不为空
|
||||
*
|
||||
* @param <T>
|
||||
*
|
||||
* @param column 字段
|
||||
* @return ConditionAndWrapper
|
||||
*/
|
||||
public ConditionAndWrapper isNotNull(String column) {
|
||||
super.isNotNull(column);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 不为空
|
||||
*
|
||||
* @param <T>
|
||||
*
|
||||
* @param column 字段
|
||||
* @return ConditionAndWrapper
|
||||
*/
|
||||
public <T, R> ConditionAndWrapper isNotNull(SerializableFunction<T, R> column) {
|
||||
super.isNotNull(ReflectionUtil.getFieldName(column));
|
||||
return this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,332 @@
|
||||
package com.cym.utils;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
|
||||
import com.cym.reflection.ReflectionUtil;
|
||||
import com.cym.reflection.SerializableFunction;
|
||||
|
||||
/**
|
||||
* 查询语句生成器 OR连接
|
||||
*
|
||||
*/
|
||||
public class ConditionOrWrapper extends ConditionWrapper {
|
||||
|
||||
public ConditionOrWrapper() {
|
||||
andLink = false;
|
||||
}
|
||||
|
||||
public ConditionOrWrapper or(ConditionWrapper conditionWrapper) {
|
||||
list.add(conditionWrapper);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 自由撰写条件
|
||||
*
|
||||
* @param condition 条件
|
||||
* @return ConditionWrapper
|
||||
*/
|
||||
public ConditionOrWrapper custom(String condition) {
|
||||
super.custom(condition);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 等于
|
||||
*
|
||||
* @param column 字段
|
||||
* @param params 参数
|
||||
* @return ConditionWrapper
|
||||
*/
|
||||
public ConditionOrWrapper eq(String column, Object params) {
|
||||
super.eq(column, params);
|
||||
return this;
|
||||
}
|
||||
/**
|
||||
* 等于
|
||||
*
|
||||
* @param column 字段
|
||||
* @param params 参数
|
||||
* @return ConditionWrapper
|
||||
*/
|
||||
public <T, R> ConditionOrWrapper eq(SerializableFunction<T, R> column, Object params) {
|
||||
super.eq(ReflectionUtil.getFieldName(column), params);
|
||||
return this;
|
||||
}
|
||||
/**
|
||||
* 不等于
|
||||
*
|
||||
* @param column 字段
|
||||
* @param params 参数
|
||||
* @return ConditionOrWrapper
|
||||
*/
|
||||
public ConditionOrWrapper ne(String column, Object params) {
|
||||
super.ne(column, params);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 不等于
|
||||
*
|
||||
* @param column 字段
|
||||
* @param params 参数
|
||||
* @return ConditionOrWrapper
|
||||
*/
|
||||
public <T, R> ConditionOrWrapper ne(SerializableFunction<T, R> column, Object params) {
|
||||
super.ne(ReflectionUtil.getFieldName(column), params);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 小于
|
||||
*
|
||||
* @param column 字段
|
||||
* @param params 参数
|
||||
* @return ConditionOrWrapper
|
||||
*/
|
||||
public ConditionOrWrapper lt(String column, Object params) {
|
||||
super.lt(column, params);
|
||||
return this;
|
||||
}
|
||||
/**
|
||||
* 小于
|
||||
*
|
||||
* @param column 字段
|
||||
* @param params 参数
|
||||
* @return ConditionOrWrapper
|
||||
*/
|
||||
public <T, R> ConditionOrWrapper lt(SerializableFunction<T, R> column, Object params) {
|
||||
super.lt(ReflectionUtil.getFieldName(column), params);
|
||||
return this;
|
||||
}
|
||||
/**
|
||||
* 小于或等于
|
||||
*
|
||||
* @param column 字段
|
||||
* @param params 参数
|
||||
* @return ConditionOrWrapper
|
||||
*/
|
||||
public ConditionOrWrapper lte(String column, Object params) {
|
||||
super.lte(column, params);
|
||||
return this;
|
||||
}
|
||||
/**
|
||||
* 小于或等于
|
||||
*
|
||||
* @param column 字段
|
||||
* @param params 参数
|
||||
* @return ConditionOrWrapper
|
||||
*/
|
||||
public <T, R> ConditionOrWrapper lte(SerializableFunction<T, R> column, Object params) {
|
||||
super.lte(ReflectionUtil.getFieldName(column), params);
|
||||
return this;
|
||||
}
|
||||
/**
|
||||
* 大于
|
||||
*
|
||||
* @param column 字段
|
||||
* @param params 参数
|
||||
* @return ConditionOrWrapper
|
||||
*/
|
||||
public ConditionOrWrapper gt(String column, Object params) {
|
||||
super.gt(column, params);
|
||||
return this;
|
||||
}
|
||||
/**
|
||||
* 大于
|
||||
*
|
||||
* @param column 字段
|
||||
* @param params 参数
|
||||
* @return ConditionOrWrapper
|
||||
*/
|
||||
public <T, R> ConditionOrWrapper gt(SerializableFunction<T, R> column, Object params) {
|
||||
super.gt(ReflectionUtil.getFieldName(column), params);
|
||||
return this;
|
||||
}
|
||||
/**
|
||||
* 大于或等于
|
||||
*
|
||||
* @param column 字段
|
||||
* @param params 参数
|
||||
* @return ConditionOrWrapper
|
||||
*/
|
||||
public ConditionOrWrapper gte(String column, Object params) {
|
||||
super.gte(column, params);
|
||||
return this;
|
||||
}
|
||||
/**
|
||||
* 大于或等于
|
||||
*
|
||||
* @param column 字段
|
||||
* @param params 参数
|
||||
* @return ConditionOrWrapper
|
||||
*/
|
||||
public <T, R> ConditionOrWrapper gte(SerializableFunction<T, R> column, Object params) {
|
||||
super.gte(ReflectionUtil.getFieldName(column), params);
|
||||
return this;
|
||||
}
|
||||
/**
|
||||
* 相似于
|
||||
*
|
||||
* @param column 字段
|
||||
* @param params 参数
|
||||
* @return ConditionOrWrapper
|
||||
*/
|
||||
public ConditionOrWrapper like(String column, String params) {
|
||||
super.like(column, params);
|
||||
return this;
|
||||
}
|
||||
/**
|
||||
* 相似于
|
||||
*
|
||||
* @param column 字段
|
||||
* @param params 参数
|
||||
* @return ConditionOrWrapper
|
||||
*/
|
||||
public <T, R> ConditionOrWrapper like(SerializableFunction<T, R> column, String params) {
|
||||
super.like(ReflectionUtil.getFieldName(column), params);
|
||||
return this;
|
||||
}
|
||||
/**
|
||||
* 在其中
|
||||
*
|
||||
* @param column 字段
|
||||
* @param params 参数
|
||||
* @return ConditionOrWrapper
|
||||
*/
|
||||
public ConditionOrWrapper in(String column, Collection<?> params) {
|
||||
super.in(column, params);
|
||||
return this;
|
||||
}
|
||||
/**
|
||||
* 在其中
|
||||
*
|
||||
* @param column 字段
|
||||
* @param params 参数
|
||||
* @return ConditionOrWrapper
|
||||
*/
|
||||
public <T, R> ConditionOrWrapper in(SerializableFunction<T, R> column, Collection<?> params) {
|
||||
super.in(ReflectionUtil.getFieldName(column), params);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 在其中
|
||||
*
|
||||
* @param column 字段
|
||||
* @param params 参数
|
||||
* @return ConditionOrWrapper
|
||||
*/
|
||||
public ConditionOrWrapper in(String column, Object[] params) {
|
||||
super.in(column, Arrays.asList(params));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 在其中
|
||||
*
|
||||
* @param column 字段
|
||||
* @param params 参数
|
||||
* @return ConditionOrWrapper
|
||||
*/
|
||||
public <T, R> ConditionOrWrapper in(SerializableFunction<T, R> column, Object[] params) {
|
||||
super.in(ReflectionUtil.getFieldName(column), Arrays.asList(params));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 不在其中
|
||||
*
|
||||
* @param column 字段
|
||||
* @param params 参数
|
||||
* @return ConditionOrWrapper
|
||||
*/
|
||||
public ConditionOrWrapper nin(String column, Collection<?> params) {
|
||||
super.nin(column, params);
|
||||
return this;
|
||||
}
|
||||
/**
|
||||
* 不在其中
|
||||
*
|
||||
* @param column 字段
|
||||
* @param params 参数
|
||||
* @return ConditionOrWrapper
|
||||
*/
|
||||
public <T, R> ConditionOrWrapper nin(SerializableFunction<T, R> column, Collection<?> params) {
|
||||
super.nin(ReflectionUtil.getFieldName(column), params);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 不在其中
|
||||
*
|
||||
* @param column 字段
|
||||
* @param params 参数
|
||||
* @return ConditionOrWrapper
|
||||
*/
|
||||
public ConditionOrWrapper nin(String column, Object[] params) {
|
||||
super.nin(column, Arrays.asList(params));
|
||||
return this;
|
||||
}
|
||||
/**
|
||||
* 不在其中
|
||||
*
|
||||
* @param column 字段
|
||||
* @param params 参数
|
||||
* @return ConditionOrWrapper
|
||||
*/
|
||||
public <T, R> ConditionOrWrapper nin(SerializableFunction<T, R> column, Object[] params) {
|
||||
super.nin(ReflectionUtil.getFieldName(column), Arrays.asList(params));
|
||||
return this;
|
||||
}
|
||||
/**
|
||||
* 为空
|
||||
*
|
||||
* @param <T>
|
||||
*
|
||||
* @param column 字段
|
||||
* @return ConditionOrWrapper
|
||||
*/
|
||||
public ConditionOrWrapper isNull(String column) {
|
||||
super.isNull(column);
|
||||
return this;
|
||||
}
|
||||
/**
|
||||
* 为空
|
||||
*
|
||||
* @param <T>
|
||||
*
|
||||
* @param column 字段
|
||||
* @return ConditionOrWrapper
|
||||
*/
|
||||
public <T, R> ConditionOrWrapper isNull(SerializableFunction<T, R> column) {
|
||||
super.isNull(ReflectionUtil.getFieldName(column));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 不为空
|
||||
*
|
||||
* @param <T>
|
||||
*
|
||||
* @param column 字段
|
||||
* @return ConditionOrWrapper
|
||||
*/
|
||||
public ConditionOrWrapper isNotNull(String column) {
|
||||
super.isNotNull(column);
|
||||
return this;
|
||||
}
|
||||
/**
|
||||
* 不为空
|
||||
*
|
||||
* @param <T>
|
||||
*
|
||||
* @param column 字段
|
||||
* @return ConditionOrWrapper
|
||||
*/
|
||||
public <T, R> ConditionOrWrapper isNotNull(SerializableFunction<T, R> column) {
|
||||
super.isNotNull(ReflectionUtil.getFieldName(column));
|
||||
return this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,247 @@
|
||||
package com.cym.utils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
|
||||
/**
|
||||
* 查询语句生成器
|
||||
*
|
||||
* @author CYM
|
||||
*
|
||||
*/
|
||||
public abstract class ConditionWrapper {
|
||||
boolean andLink;
|
||||
|
||||
List<Object> list = new ArrayList<Object>();
|
||||
|
||||
/**
|
||||
* 将Wrapper转化为Condition
|
||||
*
|
||||
* @param params
|
||||
*
|
||||
* @return Condition
|
||||
*/
|
||||
public String build(List<Object> values) {
|
||||
String sql = "";
|
||||
if (list.size() > 0) {
|
||||
List<String> blocks = new ArrayList<String>();
|
||||
for (Object object : list) {
|
||||
if (object instanceof Condition) {
|
||||
Condition condition = (Condition) object;
|
||||
|
||||
if (StrUtil.isNotEmpty(condition.getCondition())) {
|
||||
blocks.add(condition.getCondition());
|
||||
} else {
|
||||
String block = null;
|
||||
if (condition.getValue() == null) {
|
||||
if (condition.getOperation().equals("IS NULL") || condition.getOperation().equals("IS NOT NULL")) {
|
||||
block = buildColumn(condition.getColumn()) + " " + condition.getOperation();
|
||||
} else {
|
||||
block = buildColumn(condition.getColumn()) + " " + condition.getOperation() + " null";
|
||||
}
|
||||
} else {
|
||||
if (condition.getValue() instanceof List) {
|
||||
block = buildColumn(condition.getColumn()) + " " + condition.getOperation() + " " + buildIn(condition.getValue());
|
||||
for (Object val : (List<Object>) condition.getValue()) {
|
||||
values.add(val);
|
||||
}
|
||||
} else {
|
||||
block = buildColumn(condition.getColumn()) + " " + condition.getOperation() + " ?";
|
||||
if (!condition.getOperation().equals("LIKE")) {
|
||||
values.add(condition.getValue());
|
||||
} else {
|
||||
values.add("%" + condition.getValue().toString().replace("%", "\\%") + "%");
|
||||
}
|
||||
}
|
||||
}
|
||||
blocks.add(block);
|
||||
}
|
||||
}
|
||||
|
||||
if (object instanceof ConditionWrapper) {
|
||||
ConditionWrapper conditionWrapper = (ConditionWrapper) object;
|
||||
String block = " (" + conditionWrapper.build(values) + ") ";
|
||||
blocks.add(block);
|
||||
}
|
||||
}
|
||||
|
||||
if (andLink) {
|
||||
sql = StrUtil.join(" AND ", blocks);
|
||||
} else {
|
||||
sql = StrUtil.join(" OR ", blocks);
|
||||
}
|
||||
|
||||
}
|
||||
return sql;
|
||||
}
|
||||
|
||||
public String buildColumn(String column) {
|
||||
return "`" + StrUtil.toUnderlineCase(column) + "`";
|
||||
}
|
||||
|
||||
public String buildIn(Object value) {
|
||||
List<String> ask = new ArrayList<String>();
|
||||
for (Object obj : (Collection<?>) value) {
|
||||
ask.add("?");
|
||||
}
|
||||
|
||||
if (ask.size() > 0) {
|
||||
return " (" + StrUtil.join(",", ask) + ") ";
|
||||
} else {
|
||||
return " (null) ";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 自由撰写条件
|
||||
*
|
||||
* @param condition
|
||||
*/
|
||||
public ConditionWrapper custom(String condition) {
|
||||
list.add(new Condition(condition));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 等于
|
||||
*
|
||||
* @param column 字段
|
||||
* @param params 参数
|
||||
* @return ConditionWrapper
|
||||
*/
|
||||
public ConditionWrapper eq(String column, Object params) {
|
||||
list.add(new Condition(column, "=", params));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 不等于
|
||||
*
|
||||
* @param column 字段
|
||||
* @param params 参数
|
||||
* @return ConditionWrapper
|
||||
*/
|
||||
public ConditionWrapper ne(String column, Object params) {
|
||||
list.add(new Condition(column, "<>", params));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 小于
|
||||
*
|
||||
* @param column 字段
|
||||
* @param params 参数
|
||||
* @return ConditionWrapper
|
||||
*/
|
||||
public ConditionWrapper lt(String column, Object params) {
|
||||
list.add(new Condition(column, "<", params));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 小于或等于
|
||||
*
|
||||
* @param column 字段
|
||||
* @param params 参数
|
||||
* @return ConditionWrapper
|
||||
*/
|
||||
public ConditionWrapper lte(String column, Object params) {
|
||||
list.add(new Condition(column, "<=", params));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 大于
|
||||
*
|
||||
* @param column 字段
|
||||
* @param params 参数
|
||||
* @return ConditionWrapper
|
||||
*/
|
||||
public ConditionWrapper gt(String column, Object params) {
|
||||
list.add(new Condition(column, ">", params));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 大于或等于
|
||||
*
|
||||
* @param column 字段
|
||||
* @param params 参数
|
||||
* @return ConditionWrapper
|
||||
*/
|
||||
public ConditionWrapper gte(String column, Object params) {
|
||||
list.add(new Condition(column, ">=", params));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 相似于
|
||||
*
|
||||
* @param column 字段
|
||||
* @param params 参数
|
||||
* @return ConditionWrapper
|
||||
*/
|
||||
public ConditionWrapper like(String column, String params) {
|
||||
list.add(new Condition(column, "LIKE", params));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 在其中
|
||||
*
|
||||
* @param column 字段
|
||||
* @param params 参数
|
||||
* @return ConditionWrapper
|
||||
*/
|
||||
public ConditionWrapper in(String column, Collection<?> params) {
|
||||
list.add(new Condition(column, "IN", params));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 不在其中
|
||||
*
|
||||
* @param column 字段
|
||||
* @param params 参数
|
||||
* @return ConditionWrapper
|
||||
*/
|
||||
public ConditionWrapper nin(String column, Collection<?> params) {
|
||||
list.add(new Condition(column, "NOT IN", params));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 为空
|
||||
*
|
||||
* @param <T>
|
||||
*
|
||||
* @param column 字段
|
||||
* @return ConditionWrapper
|
||||
*/
|
||||
public ConditionWrapper isNull(String column) {
|
||||
list.add(new Condition(column, "IS NULL", null));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 不为空
|
||||
*
|
||||
* @param <T>
|
||||
*
|
||||
* @param column 字段
|
||||
* @return ConditionWrapper
|
||||
*/
|
||||
public ConditionWrapper isNotNull(String column) {
|
||||
list.add(new Condition(column, "IS NOT NULL", null));
|
||||
return this;
|
||||
}
|
||||
|
||||
public boolean notEmpty() {
|
||||
return list.size() > 0;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
package com.cym.utils;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.cym.bean.InitValue;
|
||||
import com.cym.bean.Update;
|
||||
|
||||
import cn.hutool.core.util.ClassUtil;
|
||||
import cn.hutool.core.util.ReflectUtil;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.transaction.Transactional;
|
||||
|
||||
@Service
|
||||
public class InitTool {
|
||||
@Autowired
|
||||
PackageTools packageTools;
|
||||
|
||||
@Autowired
|
||||
JpaHelper jpaHelper;
|
||||
|
||||
@Autowired
|
||||
JdbcTemplate jdbcTemplate;
|
||||
|
||||
@Value("${spring.datasource.url:}")
|
||||
String url;
|
||||
|
||||
@Transactional
|
||||
public void init() {
|
||||
Set<Class<?>> set = ClassUtil.scanPackage(packageTools.getMainPackage());
|
||||
// 注入默认值
|
||||
for (Class<?> clazz : set) {
|
||||
Entity entity = clazz.getAnnotation(Entity.class);
|
||||
if (entity != null) {
|
||||
Field[] fields = ReflectUtil.getFields(clazz);
|
||||
for (Field field : fields) {
|
||||
if (field.isAnnotationPresent(InitValue.class)) {
|
||||
InitValue defaultValue = field.getAnnotation(InitValue.class);
|
||||
if (defaultValue.value() != null) {
|
||||
// 更新默认值
|
||||
Long count = jpaHelper.findCountByQuery(new ConditionAndWrapper().isNull(field.getName()), clazz);
|
||||
if (count > 0) {
|
||||
String value = defaultValue.value();
|
||||
Object obj = null;
|
||||
// 获取字段类型
|
||||
Class<?> type = field.getType();
|
||||
if (type.equals(String.class)) {
|
||||
obj = value;
|
||||
}
|
||||
if (type.equals(Short.class)) {
|
||||
obj = Short.parseShort(value);
|
||||
}
|
||||
if (type.equals(Integer.class)) {
|
||||
obj = Integer.parseInt(value);
|
||||
}
|
||||
if (type.equals(Long.class)) {
|
||||
obj = Long.parseLong(value);
|
||||
}
|
||||
if (type.equals(Float.class)) {
|
||||
obj = Float.parseFloat(value);
|
||||
}
|
||||
if (type.equals(Double.class)) {
|
||||
obj = Double.parseDouble(value);
|
||||
}
|
||||
if (type.equals(Boolean.class)) {
|
||||
obj = Boolean.parseBoolean(value);
|
||||
}
|
||||
|
||||
jpaHelper.updateByQuery(new ConditionAndWrapper().isNull(field.getName()), new Update().set(field.getName(), obj), clazz);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.cym.utils;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import jakarta.annotation.PostConstruct;
|
||||
|
||||
@Configuration
|
||||
public class InitUtil {
|
||||
@Autowired
|
||||
InitTool initTool;
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
initTool.init();
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,28 @@
|
||||
package com.cym.utils;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class PackageTools {
|
||||
|
||||
@Autowired
|
||||
ApplicationContext context;
|
||||
|
||||
/**
|
||||
* 找到主程序包
|
||||
* @return
|
||||
*/
|
||||
public String getMainPackage() {
|
||||
Map<String, Object> annotatedBeans = context.getBeansWithAnnotation(SpringBootApplication.class);
|
||||
String packageName = annotatedBeans.isEmpty() ? null : annotatedBeans.values().toArray()[0].getClass().getPackage().getName();
|
||||
|
||||
return packageName;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.cym.utils;
|
||||
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
|
||||
public class SnowFlakeUtils {
|
||||
|
||||
public static Long nextId() {
|
||||
return IdUtil.getSnowflake(0, 0).nextId();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package com.cym.utils;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
|
||||
@Component
|
||||
public class SqlUtils {
|
||||
Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||
|
||||
@Value("${spring.datasource.url:}")
|
||||
String url;
|
||||
|
||||
@Value("${spring.datasource.printsql:true}")
|
||||
Boolean printsql;
|
||||
|
||||
String separator = System.getProperty("line.separator");
|
||||
|
||||
public void logQuery(String sql) {
|
||||
logQuery(sql, null);
|
||||
}
|
||||
|
||||
public void logQuery(String sql, Object[] params) {
|
||||
sql = formatSql(sql);
|
||||
|
||||
if (params != null) {
|
||||
for (Object object : params) {
|
||||
|
||||
if (object instanceof String) {
|
||||
sql = sql.replaceFirst("\\?", "'" + object.toString().replace("$", "¥").replace("\\%", "\\\\%") + "'");
|
||||
} else {
|
||||
sql = sql.replaceFirst("\\?", String.valueOf(object));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
if (printsql) {
|
||||
logger.info(separator + sql + separator);
|
||||
}
|
||||
}
|
||||
|
||||
public String formatSql(String sql) {
|
||||
if (StrUtil.isEmpty(sql)) {
|
||||
return "";
|
||||
}
|
||||
|
||||
if (!url.contains("mysql")) {
|
||||
sql = sql.replace("`", "\"");
|
||||
}
|
||||
|
||||
sql = sql.replace("FROM", separator + "FROM")//
|
||||
.replace("WHERE", separator + "WHERE")//
|
||||
.replace("ORDER", separator + "ORDER")//
|
||||
.replace("LIMIT", separator + "LIMIT")//
|
||||
.replace("VALUES", separator + "VALUES");//
|
||||
return sql;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user