1
This commit is contained in:
Binary file not shown.
@@ -0,0 +1 @@
|
|||||||
|
/target/
|
||||||
@@ -0,0 +1,56 @@
|
|||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<artifactId>jpaHelper</artifactId>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
<version>release</version>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
|
<version>3.3.3</version>
|
||||||
|
<relativePath /> <!-- lookup parent from repository -->
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<java.version>21</java.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>cn.hutool</groupId>
|
||||||
|
<artifactId>hutool-all</artifactId>
|
||||||
|
<version>5.8.25</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.mysql</groupId>
|
||||||
|
<artifactId>mysql-connector-j</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.swagger.core.v3</groupId>
|
||||||
|
<artifactId>swagger-annotations-jakarta</artifactId>
|
||||||
|
<version>2.2.19</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<source>${java.version}</source>
|
||||||
|
<target>${java.version}</target>
|
||||||
|
<encoding>${project.build.sourceEncoding}</encoding>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</project>
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
/target/
|
||||||
@@ -0,0 +1,66 @@
|
|||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<artifactId>sqlHelper</artifactId>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
<version>release</version>
|
||||||
|
<parent>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
|
<version>3.3.3</version>
|
||||||
|
<relativePath /> <!-- lookup parent from repository -->
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<java.version>21</java.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-jdbc</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.xerial</groupId>
|
||||||
|
<artifactId>sqlite-jdbc</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>cn.hutool</groupId>
|
||||||
|
<artifactId>hutool-all</artifactId>
|
||||||
|
<version>5.8.25</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.swagger.core.v3</groupId>
|
||||||
|
<artifactId>swagger-annotations-jakarta</artifactId>
|
||||||
|
<version>2.2.19</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<source>${java.version}</source>
|
||||||
|
<target>${java.version}</target>
|
||||||
|
<encoding>${project.build.sourceEncoding}</encoding>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</project>
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
package com.cym.sqlHelper.bean;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
import io.swagger.v3.oas.annotations.media.Schema;
|
||||||
|
|
||||||
|
public class BaseModel implements Serializable {
|
||||||
|
@Schema(description = "主键id")
|
||||||
|
String id;
|
||||||
|
@Schema(hidden = true)
|
||||||
|
Long createTime;
|
||||||
|
@Schema(hidden = true)
|
||||||
|
Long updateTime;
|
||||||
|
|
||||||
|
public String getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(String id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getCreateTime() {
|
||||||
|
return createTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCreateTime(Long createTime) {
|
||||||
|
this.createTime = createTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getUpdateTime() {
|
||||||
|
return updateTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUpdateTime(Long updateTime) {
|
||||||
|
this.updateTime = updateTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
package com.cym.sqlHelper.bean;
|
||||||
|
|
||||||
|
import com.cym.sqlHelper.bean.Sort.Direction;
|
||||||
|
|
||||||
|
public class Order {
|
||||||
|
Direction direction;
|
||||||
|
String column;
|
||||||
|
|
||||||
|
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,60 @@
|
|||||||
|
package com.cym.sqlHelper.bean;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import io.swagger.v3.oas.annotations.media.Schema;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 分页类
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class Page<T> {
|
||||||
|
@Schema(description ="总记录数")
|
||||||
|
Long count = 0l;
|
||||||
|
@Schema(description ="起始页(从1开始)")
|
||||||
|
Integer curr = 1;
|
||||||
|
|
||||||
|
@Schema(description ="每页记录数")
|
||||||
|
Integer limit = 10;
|
||||||
|
|
||||||
|
@Schema(description ="列表内容")
|
||||||
|
List records = Collections.emptyList();
|
||||||
|
|
||||||
|
// public <T> List<T> getRecords(Class<T> clazz) {
|
||||||
|
// return (List<T>) records;
|
||||||
|
// }
|
||||||
|
|
||||||
|
public List getRecords() {
|
||||||
|
return records;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRecords(List records) {
|
||||||
|
this.records = records;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getCount() {
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCount(Long count) {
|
||||||
|
this.count = count;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getCurr() {
|
||||||
|
return curr;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCurr(Integer curr) {
|
||||||
|
this.curr = curr;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getLimit() {
|
||||||
|
return limit;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLimit(Integer limit) {
|
||||||
|
this.limit = limit;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,83 @@
|
|||||||
|
package com.cym.sqlHelper.bean;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import com.cym.sqlHelper.reflection.ReflectionUtil;
|
||||||
|
import com.cym.sqlHelper.reflection.SerializableFunction;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
|
||||||
|
public class Sort {
|
||||||
|
List<Order> orderList = new ArrayList<>();
|
||||||
|
|
||||||
|
public static enum Direction {
|
||||||
|
ASC, DESC;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Sort() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public Sort(String column, Direction direction) {
|
||||||
|
Order order = new Order();
|
||||||
|
order.setColumn(column);
|
||||||
|
order.setDirection(direction);
|
||||||
|
|
||||||
|
orderList.add(order);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Sort(List<Order> orderList) {
|
||||||
|
this.orderList.addAll(orderList);
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T, R> Sort(SerializableFunction<T, R> column, Direction direction) {
|
||||||
|
Order order = new Order();
|
||||||
|
order.setColumn(ReflectionUtil.getFieldName(column));
|
||||||
|
order.setDirection(direction);
|
||||||
|
|
||||||
|
orderList.add(order);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Sort add(String column, Direction direction) {
|
||||||
|
Order order = new Order();
|
||||||
|
order.setColumn(column);
|
||||||
|
order.setDirection(direction);
|
||||||
|
|
||||||
|
orderList.add(order);
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T, R> Sort add(SerializableFunction<T, R> column, Direction direction) {
|
||||||
|
Order order = new Order();
|
||||||
|
order.setColumn(ReflectionUtil.getFieldName(column));
|
||||||
|
order.setDirection(direction);
|
||||||
|
|
||||||
|
orderList.add(order);
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String toString() {
|
||||||
|
List<String> sqlList = new ArrayList<>();
|
||||||
|
for (Order order : orderList) {
|
||||||
|
|
||||||
|
String sql = StrUtil.toUnderlineCase(order.getColumn());
|
||||||
|
|
||||||
|
if (order.getDirection() == Direction.ASC) {
|
||||||
|
sql += " ASC";
|
||||||
|
}
|
||||||
|
if (order.getDirection() == Direction.DESC) {
|
||||||
|
sql += " DESC";
|
||||||
|
}
|
||||||
|
|
||||||
|
sqlList.add(sql);
|
||||||
|
}
|
||||||
|
|
||||||
|
return " ORDER BY " + StrUtil.join(",", sqlList);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
package com.cym.sqlHelper.bean;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
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 Map<String, Object> getSets() {
|
||||||
|
return sets;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSets(Map<String, Object> sets) {
|
||||||
|
this.sets = sets;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package com.cym.sqlHelper.config;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.core.task.AsyncTaskExecutor;
|
||||||
|
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class AsyncConfig {
|
||||||
|
|
||||||
|
private static final int MAX_POOL_SIZE = 50;
|
||||||
|
|
||||||
|
private static final int CORE_POOL_SIZE = 20;
|
||||||
|
|
||||||
|
@Bean("sqlThreadPool")
|
||||||
|
public AsyncTaskExecutor sqlThreadPool() {
|
||||||
|
ThreadPoolTaskExecutor asyncTaskExecutor = new ThreadPoolTaskExecutor();
|
||||||
|
asyncTaskExecutor.setMaxPoolSize(MAX_POOL_SIZE);
|
||||||
|
asyncTaskExecutor.setCorePoolSize(CORE_POOL_SIZE);
|
||||||
|
asyncTaskExecutor.setThreadNamePrefix("sql-thread-pool-");
|
||||||
|
asyncTaskExecutor.initialize();
|
||||||
|
return asyncTaskExecutor;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
|
||||||
|
package com.cym.sqlHelper.config;
|
||||||
|
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
@Target({ ElementType.TYPE })
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
public @interface CompositeIndex {
|
||||||
|
|
||||||
|
boolean unique() default false;
|
||||||
|
String[] colums();
|
||||||
|
}
|
||||||
@@ -0,0 +1,59 @@
|
|||||||
|
package com.cym.sqlHelper.config;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
import com.zaxxer.hikari.HikariDataSource;
|
||||||
|
|
||||||
|
import cn.hutool.core.io.FileUtil;
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class DataSourceConfig {
|
||||||
|
@Value("${spring.datasource.url:}")
|
||||||
|
String url;
|
||||||
|
@Value("${spring.datasource.username:}")
|
||||||
|
String username;
|
||||||
|
@Value("${spring.datasource.password:}")
|
||||||
|
String password;
|
||||||
|
@Value("${spring.database.type}")
|
||||||
|
String database;
|
||||||
|
|
||||||
|
@Value("${spring.database.sqlite-path:}")
|
||||||
|
String sqlitePath;
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public DataSource dataSource() throws Exception {
|
||||||
|
HikariDataSource dataSource = new HikariDataSource();
|
||||||
|
if (database.toLowerCase().equals("mysql")) {
|
||||||
|
dataSource.setJdbcUrl(url);
|
||||||
|
dataSource.setUsername(username);// 用户名
|
||||||
|
dataSource.setPassword(password);// 密码
|
||||||
|
dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
|
||||||
|
} else if (database.toLowerCase().equals("postgresql")) {
|
||||||
|
dataSource.setJdbcUrl(url);
|
||||||
|
dataSource.setUsername(username);// 用户名
|
||||||
|
dataSource.setPassword(password);// 密码
|
||||||
|
dataSource.setDriverClassName("org.postgresql.Driver");
|
||||||
|
} else if (database.toLowerCase().equals("sqlite")) {
|
||||||
|
if (StrUtil.isEmpty(sqlitePath)) {
|
||||||
|
sqlitePath = FileUtil.getUserHomePath() + File.separator + "sqlite.db";
|
||||||
|
}
|
||||||
|
|
||||||
|
dataSource.setJdbcUrl("jdbc:sqlite:" + sqlitePath);
|
||||||
|
dataSource.setDriverClassName("org.sqlite.JDBC");
|
||||||
|
|
||||||
|
// sqlite使用连接池
|
||||||
|
dataSource.setMaximumPoolSize(5);
|
||||||
|
} else {
|
||||||
|
throw new Exception("数据库类型配置错误");
|
||||||
|
}
|
||||||
|
|
||||||
|
return dataSource;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package com.cym.sqlHelper.config;
|
||||||
|
|
||||||
|
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,15 @@
|
|||||||
|
|
||||||
|
package com.cym.sqlHelper.config;
|
||||||
|
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
@Target({ ElementType.FIELD })
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
public @interface SingleIndex {
|
||||||
|
|
||||||
|
boolean unique() default false;
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package com.cym.sqlHelper.config;
|
||||||
|
|
||||||
|
import java.lang.annotation.Documented;
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
@Documented
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Target({ ElementType.TYPE })
|
||||||
|
public @interface Table {
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,65 @@
|
|||||||
|
package com.cym.sqlHelper.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.sqlHelper.reflection;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface SerializableFunction<T, R> extends Function<T, R>, Serializable {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
package com.cym.sqlHelper.utils;
|
||||||
|
|
||||||
|
public class Condition {
|
||||||
|
public Condition(String column, String operation, Object value) {
|
||||||
|
this.column = column;
|
||||||
|
this.operation = operation;
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
String column;
|
||||||
|
String operation;
|
||||||
|
Object value;
|
||||||
|
|
||||||
|
|
||||||
|
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,339 @@
|
|||||||
|
package com.cym.sqlHelper.utils;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
|
import com.cym.sqlHelper.reflection.ReflectionUtil;
|
||||||
|
import com.cym.sqlHelper.reflection.SerializableFunction;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询语句生成器 AND连接
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class ConditionAndWrapper extends ConditionWrapper {
|
||||||
|
|
||||||
|
public ConditionAndWrapper() {
|
||||||
|
andLink = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ConditionAndWrapper and(ConditionWrapper conditionWrapper) {
|
||||||
|
list.add(conditionWrapper);
|
||||||
|
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,321 @@
|
|||||||
|
package com.cym.sqlHelper.utils;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
|
import com.cym.sqlHelper.reflection.ReflectionUtil;
|
||||||
|
import com.cym.sqlHelper.reflection.SerializableFunction;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询语句生成器 OR连接
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class ConditionOrWrapper extends ConditionWrapper {
|
||||||
|
|
||||||
|
public ConditionOrWrapper() {
|
||||||
|
andLink = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ConditionOrWrapper or(ConditionWrapper conditionWrapper) {
|
||||||
|
list.add(conditionWrapper);
|
||||||
|
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,235 @@
|
|||||||
|
package com.cym.sqlHelper.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;
|
||||||
|
String block = null;
|
||||||
|
|
||||||
|
if (condition.getValue() == null) {
|
||||||
|
if (condition.getOperation().equals("IS NULL") || condition.getOperation().equals("IS NOT NULL")) {
|
||||||
|
block = buildColumn(condition.getColumn(), String.class) + " " + condition.getOperation();
|
||||||
|
} else {
|
||||||
|
block = buildColumn(condition.getColumn(), String.class) + " " + condition.getOperation() + " null";
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (condition.getValue() instanceof List) {
|
||||||
|
block = buildColumn(condition.getColumn(), condition.getValue().getClass()) + " " + condition.getOperation() + " " + buildIn(condition.getValue());
|
||||||
|
for (Object val : (List<Object>) condition.getValue()) {
|
||||||
|
values.add(val);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
block = buildColumn(condition.getColumn(), condition.getValue().getClass()) + " " + 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, Class<?> clazz) {
|
||||||
|
|
||||||
|
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 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,11 @@
|
|||||||
|
package com.cym.sqlHelper.utils;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.IdUtil;
|
||||||
|
|
||||||
|
public class SnowFlakeUtils {
|
||||||
|
|
||||||
|
public static Long nextId() {
|
||||||
|
return IdUtil.getSnowflake(0, 0).nextId();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,944 @@
|
|||||||
|
package com.cym.sqlHelper.utils;
|
||||||
|
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Map.Entry;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
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.sqlHelper.bean.Page;
|
||||||
|
import com.cym.sqlHelper.bean.Sort;
|
||||||
|
import com.cym.sqlHelper.bean.Update;
|
||||||
|
import com.cym.sqlHelper.config.InitValue;
|
||||||
|
import com.cym.sqlHelper.reflection.ReflectionUtil;
|
||||||
|
import com.cym.sqlHelper.reflection.SerializableFunction;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.ClassUtil;
|
||||||
|
import cn.hutool.core.util.ReflectUtil;
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import jakarta.annotation.PostConstruct;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* mongodb操作器
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
public class SqlHelper extends SqlUtils {
|
||||||
|
@Value("${spring.database.type}")
|
||||||
|
String database;
|
||||||
|
@Value("${spring.database.package}")
|
||||||
|
String packageName;
|
||||||
|
@Value("${spring.database.print:false}")
|
||||||
|
Boolean print;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
JdbcTemplate jdbcTemplate;
|
||||||
|
|
||||||
|
Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
TableUtils tableUtils;
|
||||||
|
|
||||||
|
@PostConstruct
|
||||||
|
private void scan() {
|
||||||
|
if (StrUtil.isEmpty(packageName)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Set<Class<?>> set = ClassUtil.scanPackage(packageName);
|
||||||
|
for (Class<?> clazz : set) {
|
||||||
|
tableUtils.initTable(clazz);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 插入或更新
|
||||||
|
*
|
||||||
|
* @param object 对象
|
||||||
|
*/
|
||||||
|
public String insertOrUpdate(Object object) {
|
||||||
|
|
||||||
|
Long time = System.currentTimeMillis();
|
||||||
|
String id = (String) ReflectUtil.getFieldValue(object, "id");
|
||||||
|
// Object objectOrg = StrUtil.isNotEmpty(id) ? findById(id, object.getClass()) : null;
|
||||||
|
try {
|
||||||
|
if (StrUtil.isEmpty(id)) {
|
||||||
|
// 插入
|
||||||
|
// 设置插入时间
|
||||||
|
if (ReflectUtil.getField(object.getClass(), "createTime") != null) {
|
||||||
|
ReflectUtil.setFieldValue(object, "createTime", time);
|
||||||
|
}
|
||||||
|
if (ReflectUtil.getField(object.getClass(), "updateTime") != null) {
|
||||||
|
ReflectUtil.setFieldValue(object, "updateTime", time);
|
||||||
|
}
|
||||||
|
// 设置默认值
|
||||||
|
setDefaultVaule(object);
|
||||||
|
|
||||||
|
// 没有id设置id
|
||||||
|
if (StrUtil.isEmpty(id)) {
|
||||||
|
ReflectUtil.setFieldValue(object, "id", SnowFlakeUtils.nextId());
|
||||||
|
}
|
||||||
|
|
||||||
|
String sql = "";
|
||||||
|
List<String> fieldsPart = new ArrayList<String>();
|
||||||
|
List<String> placeHolder = new ArrayList<String>();
|
||||||
|
List<Object> paramValues = new ArrayList<Object>();
|
||||||
|
|
||||||
|
Field[] fields = ReflectUtil.getFields(object.getClass());
|
||||||
|
for (Field field : fields) {
|
||||||
|
fieldsPart.add("`" + StrUtil.toUnderlineCase(field.getName()) + "`");
|
||||||
|
placeHolder.add("?");
|
||||||
|
paramValues.add(ReflectUtil.getFieldValue(object, field));
|
||||||
|
}
|
||||||
|
|
||||||
|
sql = "INSERT INTO `" + StrUtil.toUnderlineCase(object.getClass().getSimpleName()) + "` (" + StrUtil.join(",", fieldsPart) + ") VALUES (" + StrUtil.join(",", placeHolder) + ")";
|
||||||
|
|
||||||
|
logQuery(formatSql(sql), paramValues.toArray());
|
||||||
|
jdbcTemplate.update(formatSql(sql), paramValues.toArray());
|
||||||
|
|
||||||
|
} else {
|
||||||
|
// 更新
|
||||||
|
Field[] fields = ReflectUtil.getFields(object.getClass());
|
||||||
|
|
||||||
|
// 设置更新时间
|
||||||
|
if (ReflectUtil.getField(object.getClass(), "updateTime") != null) {
|
||||||
|
ReflectUtil.setFieldValue(object, "updateTime", time);
|
||||||
|
}
|
||||||
|
|
||||||
|
List<String> fieldsPart = new ArrayList<String>();
|
||||||
|
List<Object> paramValues = new ArrayList<Object>();
|
||||||
|
|
||||||
|
for (Field field : fields) {
|
||||||
|
if (!field.getName().equals("id") && ReflectUtil.getFieldValue(object, field) != null) {
|
||||||
|
fieldsPart.add("`" + StrUtil.toUnderlineCase(field.getName()) + "`=?");
|
||||||
|
paramValues.add(ReflectUtil.getFieldValue(object, field));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
paramValues.add(id);
|
||||||
|
|
||||||
|
String sql = "UPDATE `" + StrUtil.toUnderlineCase(object.getClass().getSimpleName()) + "` SET " + StrUtil.join(",", fieldsPart) + " WHERE id = ?";
|
||||||
|
|
||||||
|
logQuery(formatSql(sql), paramValues.toArray());
|
||||||
|
jdbcTemplate.update(formatSql(sql), paramValues.toArray());
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (String) ReflectUtil.getFieldValue(object, "id");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 插入
|
||||||
|
*
|
||||||
|
* @param object 对象
|
||||||
|
*/
|
||||||
|
public String insert(Object object) {
|
||||||
|
// String id = (String) ReflectUtil.getFieldValue(object, "id");
|
||||||
|
// Object objectOrg = StrUtil.isNotEmpty(id) ? findById(id, object.getClass()) : null;
|
||||||
|
// if (objectOrg != null) {
|
||||||
|
// // 数据库里已有相同id, 使用新id以便插入
|
||||||
|
// ReflectUtil.setFieldValue(object, "id", snowFlake.nextId());
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // 没有id生成id
|
||||||
|
// if (ReflectUtil.getFieldValue(object, "id") == null) {
|
||||||
|
// ReflectUtil.setFieldValue(object, "id", snowFlake.nextId());
|
||||||
|
// }
|
||||||
|
|
||||||
|
// 去除主键id
|
||||||
|
ReflectUtil.setFieldValue(object, "id", null);
|
||||||
|
|
||||||
|
insertOrUpdate(object);
|
||||||
|
|
||||||
|
return (String) ReflectUtil.getFieldValue(object, "id");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 插入全部字段
|
||||||
|
*
|
||||||
|
* @param object 对象
|
||||||
|
*/
|
||||||
|
public String insertAllColumn(Object object) {
|
||||||
|
// 没有id生成id
|
||||||
|
if (ReflectUtil.getFieldValue(object, "id") == null) {
|
||||||
|
ReflectUtil.setFieldValue(object, "id", SnowFlakeUtils.nextId());
|
||||||
|
}
|
||||||
|
|
||||||
|
String sql = "";
|
||||||
|
List<String> fieldsPart = new ArrayList<String>();
|
||||||
|
List<String> placeHolder = new ArrayList<String>();
|
||||||
|
List<Object> paramValues = new ArrayList<Object>();
|
||||||
|
|
||||||
|
Field[] fields = ReflectUtil.getFields(object.getClass());
|
||||||
|
for (Field field : fields) {
|
||||||
|
fieldsPart.add("`" + StrUtil.toUnderlineCase(field.getName()) + "`");
|
||||||
|
placeHolder.add("?");
|
||||||
|
paramValues.add(ReflectUtil.getFieldValue(object, field));
|
||||||
|
}
|
||||||
|
|
||||||
|
sql = "INSERT INTO `" + StrUtil.toUnderlineCase(object.getClass().getSimpleName()) + "` (" + StrUtil.join(",", fieldsPart) + ") VALUES (" + StrUtil.join(",", placeHolder) + ")";
|
||||||
|
logQuery(formatSql(sql), paramValues.toArray());
|
||||||
|
jdbcTemplate.update(formatSql(sql), paramValues.toArray());
|
||||||
|
|
||||||
|
// 返回id
|
||||||
|
return (String) ReflectUtil.getFieldValue(object, "id");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 批量插入
|
||||||
|
*
|
||||||
|
* @param <T>
|
||||||
|
*
|
||||||
|
* @param object 对象
|
||||||
|
*/
|
||||||
|
public <T> void insertBatch(List<T> list) {
|
||||||
|
Long time = System.currentTimeMillis();
|
||||||
|
|
||||||
|
// Map<String, Object> idMap = new HashMap<String, Object>();
|
||||||
|
// for (Object object : list) {
|
||||||
|
// if (ReflectUtil.getFieldValue(object, "id") != null) {
|
||||||
|
// String id = (String) ReflectUtil.getFieldValue(object, "id");
|
||||||
|
// Object objectOrg = StrUtil.isNotEmpty(id) ? findById(id, object.getClass()) : null;
|
||||||
|
// idMap.put((String) ReflectUtil.getFieldValue(object, "id"), objectOrg);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
for (Object object : list) {
|
||||||
|
// if (ReflectUtil.getFieldValue(object, "id") != null && idMap.get((String) ReflectUtil.getFieldValue(object, "id")) != null) {
|
||||||
|
// // 数据库里已有相同id, 使用新id以便插入
|
||||||
|
// ReflectUtil.setFieldValue(object, "id", snowFlake.nextId());
|
||||||
|
// }
|
||||||
|
|
||||||
|
// 没有id生成id
|
||||||
|
if (ReflectUtil.getFieldValue(object, "id") == null) {
|
||||||
|
ReflectUtil.setFieldValue(object, "id", SnowFlakeUtils.nextId());
|
||||||
|
}
|
||||||
|
|
||||||
|
// 设置插入时间
|
||||||
|
if (ReflectUtil.getField(object.getClass(), "createTime") != null) {
|
||||||
|
ReflectUtil.setFieldValue(object, "createTime", time);
|
||||||
|
}
|
||||||
|
if (ReflectUtil.getField(object.getClass(), "updateTime") != null) {
|
||||||
|
ReflectUtil.setFieldValue(object, "updateTime", time);
|
||||||
|
}
|
||||||
|
// 设置默认值
|
||||||
|
setDefaultVaule(object);
|
||||||
|
}
|
||||||
|
|
||||||
|
List<Object[]> paramValues = new ArrayList<Object[]>();
|
||||||
|
String sqls = null;
|
||||||
|
for (Object object : list) {
|
||||||
|
Field[] fields = ReflectUtil.getFields(object.getClass());
|
||||||
|
|
||||||
|
List<String> fieldsPart = new ArrayList<String>();
|
||||||
|
List<String> placeHolder = new ArrayList<String>();
|
||||||
|
|
||||||
|
List<Object> params = new ArrayList<Object>();
|
||||||
|
for (Field field : fields) {
|
||||||
|
fieldsPart.add("`" + StrUtil.toUnderlineCase(field.getName()) + "`");
|
||||||
|
placeHolder.add("?");
|
||||||
|
params.add(ReflectUtil.getFieldValue(object, field));
|
||||||
|
}
|
||||||
|
|
||||||
|
paramValues.add(params.toArray());
|
||||||
|
|
||||||
|
if (sqls == null) {
|
||||||
|
sqls = "INSERT INTO `" + StrUtil.toUnderlineCase(object.getClass().getSimpleName()) + "` (" + StrUtil.join(",", fieldsPart) + ") VALUES (" + StrUtil.join(",", placeHolder) + ")";
|
||||||
|
}
|
||||||
|
logQuery(formatSql(sqls), params.toArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
//logQueryBatch(formatSql(sqls), paramValues);
|
||||||
|
jdbcTemplate.batchUpdate(formatSql(sqls), paramValues);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据id更新
|
||||||
|
*
|
||||||
|
* @param object 对象
|
||||||
|
*/
|
||||||
|
public void updateById(Object object) {
|
||||||
|
if (StrUtil.isEmpty((String) ReflectUtil.getFieldValue(object, "id"))) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
insertOrUpdate(object);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 批量更新
|
||||||
|
*
|
||||||
|
* @param conditionAndWrapper
|
||||||
|
* @param update
|
||||||
|
* @param clazz
|
||||||
|
*/
|
||||||
|
public void updateMulti(ConditionWrapper conditionWrapper, Update update, Class<?> clazz) {
|
||||||
|
if (update == null || update.getSets().size() == 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
List<String> fieldsPart = new ArrayList<String>();
|
||||||
|
List<Object> paramValues = new ArrayList<Object>();
|
||||||
|
for (Entry<String, Object> entry : update.getSets().entrySet()) {
|
||||||
|
if (entry.getKey() != null && entry.getValue() != null) {
|
||||||
|
fieldsPart.add("`" + StrUtil.toUnderlineCase(entry.getKey()) + "`=?");
|
||||||
|
paramValues.add(entry.getValue().toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
String sql = "UPDATE `" + StrUtil.toUnderlineCase(clazz.getSimpleName()) + "` SET " + StrUtil.join(",", fieldsPart);
|
||||||
|
if (conditionWrapper != null && conditionWrapper.notEmpty()) {
|
||||||
|
sql += " WHERE " + conditionWrapper.build(paramValues);
|
||||||
|
}
|
||||||
|
|
||||||
|
logQuery(formatSql(sql), paramValues.toArray());
|
||||||
|
jdbcTemplate.update(formatSql(sql), paramValues.toArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 累加某一个字段的数量,原子操作
|
||||||
|
*
|
||||||
|
* @param object
|
||||||
|
*/
|
||||||
|
public void addCountById(String id, String property, Long count, Class<?> clazz) {
|
||||||
|
String sql = "UPDATE `" + StrUtil.toUnderlineCase(clazz.getSimpleName()) + "` SET `" + property + "` = CAST(`" + property + "` AS DECIMAL(30,10)) + ? WHERE `id` = ?";
|
||||||
|
Object[] params = new Object[] { count, id };
|
||||||
|
logQuery(formatSql(sql), params);
|
||||||
|
jdbcTemplate.update(formatSql(sql), params);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 累加某一个字段的数量,原子操作
|
||||||
|
*
|
||||||
|
* @param object
|
||||||
|
*/
|
||||||
|
public <T, R> void addCountById(String id, SerializableFunction<T, R> property, Long count, Class<?> clazz) {
|
||||||
|
addCountById(id, ReflectionUtil.getFieldName(property), count, clazz);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据id更新
|
||||||
|
*
|
||||||
|
* @param object 对象
|
||||||
|
*/
|
||||||
|
public void updateAllColumnById(Object object) {
|
||||||
|
if (StrUtil.isEmpty((String) ReflectUtil.getFieldValue(object, "id"))) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Field[] fields = ReflectUtil.getFields(object.getClass());
|
||||||
|
|
||||||
|
List<String> fieldsPart = new ArrayList<String>();
|
||||||
|
List<Object> paramValues = new ArrayList<Object>();
|
||||||
|
|
||||||
|
for (Field field : fields) {
|
||||||
|
if (!field.getName().equals("id")) {
|
||||||
|
fieldsPart.add("`" + StrUtil.toUnderlineCase(field.getName()) + "`=?");
|
||||||
|
paramValues.add(ReflectUtil.getFieldValue(object, field));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
paramValues.add((String) ReflectUtil.getFieldValue(object, "id"));
|
||||||
|
|
||||||
|
String sql = "UPDATE `" + StrUtil.toUnderlineCase(object.getClass().getSimpleName()) + "` SET " + StrUtil.join(",", fieldsPart) + " WHERE id = ?";
|
||||||
|
|
||||||
|
logQuery(formatSql(sql), paramValues.toArray());
|
||||||
|
jdbcTemplate.update(formatSql(sql), paramValues.toArray());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据id删除
|
||||||
|
*
|
||||||
|
* @param id 对象
|
||||||
|
* @param clazz 类
|
||||||
|
*/
|
||||||
|
public void deleteById(String id, Class<?> clazz) {
|
||||||
|
|
||||||
|
if (StrUtil.isEmpty(id)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
deleteByQuery(new ConditionAndWrapper().eq("id", id), clazz);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据id删除
|
||||||
|
*
|
||||||
|
* @param id 对象
|
||||||
|
* @param clazz 类
|
||||||
|
*/
|
||||||
|
public void deleteByIds(Collection<String> ids, Class<?> clazz) {
|
||||||
|
if (ids == null || ids.size() == 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
deleteByQuery(new ConditionAndWrapper().in("id", ids), clazz);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据id删除
|
||||||
|
*
|
||||||
|
* @param id 对象
|
||||||
|
* @param clazz 类
|
||||||
|
*/
|
||||||
|
public void deleteByIds(String[] ids, Class<?> clazz) {
|
||||||
|
deleteByIds(Arrays.asList(ids), clazz);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据条件删除
|
||||||
|
*
|
||||||
|
* @param query 查询
|
||||||
|
* @param clazz 类
|
||||||
|
*/
|
||||||
|
public void deleteByQuery(ConditionWrapper conditionWrapper, Class<?> clazz) {
|
||||||
|
List<Object> values = new ArrayList<Object>();
|
||||||
|
String sql = "DELETE FROM `" + StrUtil.toUnderlineCase(clazz.getSimpleName()) + "`";
|
||||||
|
if (conditionWrapper != null && conditionWrapper.notEmpty()) {
|
||||||
|
sql += " WHERE " + conditionWrapper.build(values);
|
||||||
|
}
|
||||||
|
logQuery(formatSql(sql), values.toArray());
|
||||||
|
jdbcTemplate.update(formatSql(sql), values.toArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置默认值
|
||||||
|
*
|
||||||
|
* @param object 对象
|
||||||
|
*/
|
||||||
|
private void setDefaultVaule(Object object) {
|
||||||
|
Field[] fields = ReflectUtil.getFields(object.getClass());
|
||||||
|
for (Field field : fields) {
|
||||||
|
// 获取注解
|
||||||
|
if (field.isAnnotationPresent(InitValue.class)) {
|
||||||
|
InitValue defaultValue = field.getAnnotation(InitValue.class);
|
||||||
|
|
||||||
|
String value = defaultValue.value();
|
||||||
|
|
||||||
|
if (ReflectUtil.getFieldValue(object, field) == null) {
|
||||||
|
// 获取字段类型
|
||||||
|
Class<?> type = field.getType();
|
||||||
|
if (type.equals(String.class)) {
|
||||||
|
ReflectUtil.setFieldValue(object, field, value);
|
||||||
|
}
|
||||||
|
if (type.equals(Short.class)) {
|
||||||
|
ReflectUtil.setFieldValue(object, field, Short.parseShort(value));
|
||||||
|
}
|
||||||
|
if (type.equals(Integer.class)) {
|
||||||
|
ReflectUtil.setFieldValue(object, field, Integer.parseInt(value));
|
||||||
|
}
|
||||||
|
if (type.equals(Long.class)) {
|
||||||
|
ReflectUtil.setFieldValue(object, field, Long.parseLong(value));
|
||||||
|
}
|
||||||
|
if (type.equals(Float.class)) {
|
||||||
|
ReflectUtil.setFieldValue(object, field, Float.parseFloat(value));
|
||||||
|
}
|
||||||
|
if (type.equals(Double.class)) {
|
||||||
|
ReflectUtil.setFieldValue(object, field, Double.parseDouble(value));
|
||||||
|
}
|
||||||
|
if (type.equals(Boolean.class)) {
|
||||||
|
ReflectUtil.setFieldValue(object, field, Boolean.parseBoolean(value));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按查询条件获取Page
|
||||||
|
*
|
||||||
|
* @param query 查询
|
||||||
|
* @param page 分页
|
||||||
|
* @param clazz 类
|
||||||
|
* @return Page 分页
|
||||||
|
*/
|
||||||
|
public Page findPage(ConditionWrapper conditionWrapper, Sort sort, Page page, Class<?> clazz) {
|
||||||
|
List<Object> values = new ArrayList<Object>();
|
||||||
|
// 查询出一共的条数
|
||||||
|
Long count = findCountByQuery(conditionWrapper, clazz);
|
||||||
|
|
||||||
|
String sql = "SELECT * FROM `" + StrUtil.toUnderlineCase(clazz.getSimpleName()) + "`";
|
||||||
|
if (conditionWrapper != null && conditionWrapper.notEmpty()) {
|
||||||
|
sql += " WHERE " + conditionWrapper.build(values);
|
||||||
|
}
|
||||||
|
if (sort != null) {
|
||||||
|
sql += " " + sort.toString();
|
||||||
|
} else {
|
||||||
|
sql += " ORDER BY id DESC";
|
||||||
|
}
|
||||||
|
if (database.equalsIgnoreCase("mysql") || database.equalsIgnoreCase("sqlite")) {
|
||||||
|
sql += " LIMIT " + (page.getCurr() - 1) * page.getLimit() + "," + page.getLimit();
|
||||||
|
} else {
|
||||||
|
sql += " LIMIT " + page.getLimit() + " OFFSET " + (page.getCurr() - 1) * page.getLimit();
|
||||||
|
}
|
||||||
|
|
||||||
|
page.setCount(count);
|
||||||
|
|
||||||
|
logQuery(formatSql(sql), values.toArray());
|
||||||
|
page.setRecords(buildObjects(jdbcTemplate.queryForList(formatSql(sql), values.toArray()), clazz));
|
||||||
|
|
||||||
|
return page;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按查询条件获取Page
|
||||||
|
*
|
||||||
|
* @param query 查询
|
||||||
|
* @param page 分页
|
||||||
|
* @param clazz 类
|
||||||
|
* @return Page 分页
|
||||||
|
*/
|
||||||
|
public Page findPage(Sort sort, Page page, Class<?> clazz) {
|
||||||
|
return findPage(null, sort, page, clazz);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按查询条件获取Page
|
||||||
|
*
|
||||||
|
* @param query 查询
|
||||||
|
* @param page 分页
|
||||||
|
* @param clazz 类
|
||||||
|
* @return Page 分页
|
||||||
|
*/
|
||||||
|
public Page findPage(ConditionWrapper conditionWrapper, Page page, Class<?> clazz) {
|
||||||
|
return findPage(conditionWrapper, null, page, clazz);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按查询条件获取Page
|
||||||
|
*
|
||||||
|
* @param query 查询
|
||||||
|
* @param page 分页
|
||||||
|
* @param clazz 类
|
||||||
|
* @return Page 分页
|
||||||
|
*/
|
||||||
|
public Page findPage(Page page, Class<?> clazz) {
|
||||||
|
return findPage(null, null, page, clazz);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据id查找
|
||||||
|
*
|
||||||
|
* @param id id
|
||||||
|
* @param clazz 类
|
||||||
|
* @return T 对象
|
||||||
|
*/
|
||||||
|
public <T> T findById(String id, Class<T> clazz) {
|
||||||
|
if (StrUtil.isEmpty(id)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return findOneByQuery(new ConditionAndWrapper().eq("id", id), clazz);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据条件查找单个
|
||||||
|
*
|
||||||
|
* @param query 查询
|
||||||
|
* @param clazz 类
|
||||||
|
* @return T 对象
|
||||||
|
*/
|
||||||
|
public <T> T findOneByQuery(ConditionWrapper conditionWrapper, Sort sort, Class<T> clazz) {
|
||||||
|
List<Object> values = new ArrayList<Object>();
|
||||||
|
List<T> list = new ArrayList<T>();
|
||||||
|
String sql = "SELECT * FROM `" + StrUtil.toUnderlineCase(clazz.getSimpleName()) + "`";
|
||||||
|
if (conditionWrapper != null && conditionWrapper.notEmpty()) {
|
||||||
|
sql += " WHERE " + conditionWrapper.build(values);
|
||||||
|
}
|
||||||
|
if (sort != null) {
|
||||||
|
sql += " " + sort.toString();
|
||||||
|
} else {
|
||||||
|
sql += " ORDER BY id DESC";
|
||||||
|
}
|
||||||
|
sql += " limit 1";
|
||||||
|
|
||||||
|
logQuery(formatSql(sql), values.toArray());
|
||||||
|
list = buildObjects(jdbcTemplate.queryForList(formatSql(sql), values.toArray()), clazz);
|
||||||
|
return list.size() > 0 ? list.get(0) : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据条件查找单个
|
||||||
|
*
|
||||||
|
* @param query 查询
|
||||||
|
* @param clazz 类
|
||||||
|
* @return T 对象
|
||||||
|
*/
|
||||||
|
public <T> T findOneByQuery(Sort sort, Class<T> clazz) {
|
||||||
|
return findOneByQuery(null, sort, clazz);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据条件查找单个
|
||||||
|
*
|
||||||
|
* @param <T> 类型
|
||||||
|
* @param condition
|
||||||
|
* @param clazz 类
|
||||||
|
* @return T 对象
|
||||||
|
*/
|
||||||
|
public <T> T findOneByQuery(ConditionWrapper conditionWrapper, Class<T> clazz) {
|
||||||
|
return findOneByQuery(conditionWrapper, null, clazz);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据条件查找List
|
||||||
|
*
|
||||||
|
* @param <T> 类型
|
||||||
|
* @param query 查询
|
||||||
|
* @param clazz 类
|
||||||
|
* @return List 列表
|
||||||
|
*/
|
||||||
|
public <T> List<T> findListByQuery(ConditionWrapper conditionWrapper, Sort sort, Class<T> clazz) {
|
||||||
|
List<Object> values = new ArrayList<Object>();
|
||||||
|
|
||||||
|
String sql = "SELECT * FROM `" + StrUtil.toUnderlineCase(clazz.getSimpleName()) + "`";
|
||||||
|
if (conditionWrapper != null && conditionWrapper.notEmpty()) {
|
||||||
|
sql += " WHERE " + conditionWrapper.build(values);
|
||||||
|
}
|
||||||
|
if (sort != null) {
|
||||||
|
sql += " " + sort.toString();
|
||||||
|
} else {
|
||||||
|
sql += " ORDER BY id DESC";
|
||||||
|
}
|
||||||
|
|
||||||
|
logQuery(formatSql(sql), values.toArray());
|
||||||
|
return buildObjects(jdbcTemplate.queryForList(formatSql(sql), values.toArray()), clazz);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据条件查找List
|
||||||
|
*
|
||||||
|
* @param <T> 类型
|
||||||
|
* @param condition 查询
|
||||||
|
* @param clazz 类
|
||||||
|
* @return List 列表
|
||||||
|
*/
|
||||||
|
public <T> List<T> findListByQuery(ConditionWrapper conditionWrapper, Class<T> clazz) {
|
||||||
|
return (List<T>) findListByQuery(conditionWrapper, null, clazz);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据条件查找List
|
||||||
|
*
|
||||||
|
* @param <T> 类型
|
||||||
|
* @param condition 查询
|
||||||
|
* @param clazz 类
|
||||||
|
* @return List 列表
|
||||||
|
*/
|
||||||
|
public <T> List<T> findListByQuery(Sort sort, Class<T> clazz) {
|
||||||
|
return (List<T>) findListByQuery(null, sort, clazz);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据条件查找某个属性
|
||||||
|
*
|
||||||
|
* @param <T> 类型
|
||||||
|
* @param query 查询
|
||||||
|
* @param documentClass 类
|
||||||
|
* @param property 属性
|
||||||
|
* @param propertyClass 属性类
|
||||||
|
* @return List 列表
|
||||||
|
*/
|
||||||
|
public <T> List<T> findPropertiesByQuery(ConditionWrapper conditionWrapper, Class<?> documentClass, String property, Class<T> propertyClass) {
|
||||||
|
List<?> list = findListByQuery(conditionWrapper, documentClass);
|
||||||
|
List<T> propertyList = extractProperty(list, property, propertyClass);
|
||||||
|
|
||||||
|
return propertyList;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据条件查找某个属性
|
||||||
|
*
|
||||||
|
* @param <T> 类型
|
||||||
|
* @param query 查询
|
||||||
|
* @param documentClass 类
|
||||||
|
* @param property 属性
|
||||||
|
* @param propertyClass 属性类
|
||||||
|
* @return List 列表
|
||||||
|
*/
|
||||||
|
public <T, R> List<T> findPropertiesByQuery(ConditionWrapper conditionWrapper, Class<?> documentClass, SerializableFunction<T, R> property, Class<T> propertyClass) {
|
||||||
|
return findPropertiesByQuery(conditionWrapper, documentClass, ReflectionUtil.getFieldName(property), propertyClass);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据条件查找某个属性
|
||||||
|
*
|
||||||
|
* @param <T> 类型
|
||||||
|
* @param condition 查询
|
||||||
|
* @param documentClass 类
|
||||||
|
* @param property 属性
|
||||||
|
* @return List 列表
|
||||||
|
*/
|
||||||
|
public List<String> findPropertiesByQuery(ConditionWrapper conditionWrapper, Class<?> documentClass, String property) {
|
||||||
|
return findPropertiesByQuery(conditionWrapper, documentClass, property, String.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据条件查找某个属性
|
||||||
|
*
|
||||||
|
* @param <T> 类型
|
||||||
|
* @param condition 查询
|
||||||
|
* @param documentClass 类
|
||||||
|
* @param property 属性
|
||||||
|
* @return List 列表
|
||||||
|
*/
|
||||||
|
public <T, R> List<String> findPropertiesByQuery(ConditionWrapper conditionWrapper, Class<?> documentClass, SerializableFunction<T, R> property) {
|
||||||
|
return findPropertiesByQuery(conditionWrapper, documentClass, ReflectionUtil.getFieldName(property), String.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据id查找某个属性
|
||||||
|
*
|
||||||
|
* @param <T> 类型
|
||||||
|
* @param condition 查询
|
||||||
|
* @param documentClass 类
|
||||||
|
* @param property 属性
|
||||||
|
* @return List 列表
|
||||||
|
*/
|
||||||
|
public List<String> findPropertiesByIds(Collection<String> ids, Class<?> documentClass, String property) {
|
||||||
|
if (ids == null || ids.size() == 0) {
|
||||||
|
return new ArrayList<String>();
|
||||||
|
}
|
||||||
|
|
||||||
|
ConditionAndWrapper ConditionAndWrapper = new ConditionAndWrapper();
|
||||||
|
ConditionAndWrapper.in("id", ids);
|
||||||
|
|
||||||
|
return findPropertiesByQuery(ConditionAndWrapper, documentClass, property, String.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据id查找某个属性
|
||||||
|
*
|
||||||
|
* @param <T> 类型
|
||||||
|
* @param condition 查询
|
||||||
|
* @param documentClass 类
|
||||||
|
* @param property 属性
|
||||||
|
* @return List 列表
|
||||||
|
*/
|
||||||
|
public <T, R> List<String> findPropertiesByIds(Collection<String> ids, Class<?> documentClass, SerializableFunction<T, R> property) {
|
||||||
|
return findPropertiesByIds(ids, documentClass, ReflectionUtil.getFieldName(property));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据id查找某个属性
|
||||||
|
*
|
||||||
|
* @param <T> 类型
|
||||||
|
* @param condition 查询
|
||||||
|
* @param documentClass 类
|
||||||
|
* @param property 属性
|
||||||
|
* @return List 列表
|
||||||
|
*/
|
||||||
|
public List<String> findPropertiesByIds(String[] ids, Class<?> documentClass, String property) {
|
||||||
|
return findPropertiesByIds(Arrays.asList(ids), documentClass, property);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据id查找某个属性
|
||||||
|
*
|
||||||
|
* @param <T> 类型
|
||||||
|
* @param condition 查询
|
||||||
|
* @param documentClass 类
|
||||||
|
* @param property 属性
|
||||||
|
* @return List 列表
|
||||||
|
*/
|
||||||
|
public <T, R> List<String> findPropertiesByIds(String[] ids, Class<?> documentClass, SerializableFunction<T, R> property) {
|
||||||
|
return findPropertiesByIds(Arrays.asList(ids), documentClass, ReflectionUtil.getFieldName(property));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据条件查找id
|
||||||
|
*
|
||||||
|
* @param query 查询
|
||||||
|
* @param clazz 类
|
||||||
|
* @return List 列表
|
||||||
|
*/
|
||||||
|
public List<String> findIdsByQuery(ConditionWrapper conditionWrapper, Class<?> clazz) {
|
||||||
|
|
||||||
|
return findPropertiesByQuery(conditionWrapper, clazz, "id");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据id集合查找
|
||||||
|
*
|
||||||
|
* @param List ids id集合
|
||||||
|
* @param clazz 类
|
||||||
|
* @return List 列表
|
||||||
|
*/
|
||||||
|
public <T> List<T> findListByIds(Collection<String> ids, Class<T> clazz) {
|
||||||
|
return findListByIds(ids, null, clazz);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据id集合查找
|
||||||
|
*
|
||||||
|
* @param List ids id集合
|
||||||
|
* @param clazz 类
|
||||||
|
* @return List 列表
|
||||||
|
*/
|
||||||
|
public <T> List<T> findListByIds(String[] ids, Class<T> clazz) {
|
||||||
|
return findListByIds(Arrays.asList(ids), null, clazz);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据id集合查找
|
||||||
|
*
|
||||||
|
* @param List ids id集合
|
||||||
|
* @param clazz 类
|
||||||
|
* @return List 列表
|
||||||
|
*/
|
||||||
|
public <T> List<T> findListByIds(Collection<String> ids, Sort sort, Class<T> clazz) {
|
||||||
|
if (ids == null || ids.size() == 0) {
|
||||||
|
return new ArrayList<T>();
|
||||||
|
}
|
||||||
|
|
||||||
|
ConditionAndWrapper ConditionAndWrapper = new ConditionAndWrapper();
|
||||||
|
ConditionAndWrapper.in("id", ids);
|
||||||
|
|
||||||
|
return findListByQuery(ConditionAndWrapper, sort, clazz);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据id集合查找
|
||||||
|
*
|
||||||
|
* @param List ids id集合
|
||||||
|
* @param clazz 类
|
||||||
|
* @return List 列表
|
||||||
|
*/
|
||||||
|
public <T> List<T> findListByIds(String[] ids, Sort sort, Class<T> clazz) {
|
||||||
|
return findListByIds(Arrays.asList(ids), sort, clazz);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询全部
|
||||||
|
*
|
||||||
|
* @param <T> 类型
|
||||||
|
* @param clazz 类
|
||||||
|
* @return List 列表
|
||||||
|
*/
|
||||||
|
public <T> List<T> findAll(Class<T> clazz) {
|
||||||
|
return findAll(null, clazz);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询全部
|
||||||
|
*
|
||||||
|
* @param <T> 类型
|
||||||
|
* @param clazz 类
|
||||||
|
* @return List 列表
|
||||||
|
*/
|
||||||
|
public <T> List<T> findAll(Sort sort, Class<T> clazz) {
|
||||||
|
return findListByQuery(null, sort, clazz);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查找全部的id
|
||||||
|
*
|
||||||
|
* @param clazz 类
|
||||||
|
* @return List 列表
|
||||||
|
*/
|
||||||
|
public List<String> findAllIds(Class<?> clazz) {
|
||||||
|
return findIdsByQuery(null, clazz);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查找数量
|
||||||
|
*
|
||||||
|
* @param condition 查询
|
||||||
|
* @param clazz 类
|
||||||
|
* @return Long 数量
|
||||||
|
*/
|
||||||
|
public Long findCountByQuery(ConditionWrapper conditionWrapper, Class<?> clazz) {
|
||||||
|
List<Object> values = new ArrayList<Object>();
|
||||||
|
String sql = "SELECT COUNT(*) FROM `" + StrUtil.toUnderlineCase(clazz.getSimpleName()) + "`";
|
||||||
|
if (conditionWrapper != null && conditionWrapper.notEmpty()) {
|
||||||
|
sql += " WHERE " + conditionWrapper.build(values);
|
||||||
|
}
|
||||||
|
|
||||||
|
logQuery(formatSql(sql), values.toArray());
|
||||||
|
return jdbcTemplate.queryForObject(formatSql(sql), values.toArray(), Long.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查找全部数量
|
||||||
|
*
|
||||||
|
* @param clazz 类
|
||||||
|
* @return Long 数量
|
||||||
|
*/
|
||||||
|
public Long findAllCount(Class<?> clazz) {
|
||||||
|
return findCountByQuery(null, clazz);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取list中对象某个属性,组成新的list
|
||||||
|
*
|
||||||
|
* @param list 列表
|
||||||
|
* @param clazz 类
|
||||||
|
* @param property 属性
|
||||||
|
* @return List<T> 列表
|
||||||
|
*/
|
||||||
|
private <T> List<T> extractProperty(List<?> list, String property, Class<T> clazz) {
|
||||||
|
Set<T> rs = new HashSet<T>();
|
||||||
|
for (Object object : list) {
|
||||||
|
Object value = ReflectUtil.getFieldValue(object, property);
|
||||||
|
if (value != null && value.getClass().equals(clazz)) {
|
||||||
|
rs.add((T) value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return new ArrayList<T>(rs);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Map转Bean
|
||||||
|
*
|
||||||
|
* @param <T>
|
||||||
|
* @param queryForList
|
||||||
|
* @param clazz
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private <T> List<T> buildObjects(List<Map<String, Object>> queryForList, Class<T> clazz) {
|
||||||
|
List<T> list = new ArrayList<T>();
|
||||||
|
try {
|
||||||
|
|
||||||
|
Field[] fields = ReflectUtil.getFields(clazz);
|
||||||
|
|
||||||
|
for (Map<String, Object> map : queryForList) {
|
||||||
|
Object obj = clazz.getDeclaredConstructor().newInstance();
|
||||||
|
|
||||||
|
for (Map.Entry<String, Object> entry : map.entrySet()) {
|
||||||
|
String mapKey = entry.getKey();
|
||||||
|
Object mapValue = entry.getValue();
|
||||||
|
|
||||||
|
for (Field field : fields) {
|
||||||
|
if (StrUtil.toUnderlineCase(field.getName()).equals(mapKey)) {
|
||||||
|
ReflectUtil.setFieldValue(obj, field.getName(), mapValue);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
list.add((T) obj);
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,151 @@
|
|||||||
|
package com.cym.sqlHelper.utils;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.jdbc.core.JdbcTemplate;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class SqlUtils {
|
||||||
|
Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||||
|
|
||||||
|
@Value("${spring.database.type}")
|
||||||
|
String database;
|
||||||
|
@Value("${spring.database.package}")
|
||||||
|
String packageName;
|
||||||
|
@Value("${spring.database.print:false}")
|
||||||
|
Boolean print;
|
||||||
|
@Autowired
|
||||||
|
JdbcTemplate jdbcTemplate;
|
||||||
|
|
||||||
|
String separator = System.getProperty("line.separator");
|
||||||
|
|
||||||
|
|
||||||
|
public String formatSql(String sql) {
|
||||||
|
if (StrUtil.isEmpty(sql)) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!database.equalsIgnoreCase("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;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void checkOrCreateTable(Class<?> clazz) {
|
||||||
|
String sql = "CREATE TABLE IF NOT EXISTS `" + StrUtil.toUnderlineCase(clazz.getSimpleName()) + "` (id VARCHAR(32) NOT NULL PRIMARY KEY)";
|
||||||
|
logQuery(formatSql(sql));
|
||||||
|
jdbcTemplate.execute(formatSql(sql));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void logQuery(String sql) {
|
||||||
|
logQuery(sql, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void logQuery(String sql, Object[] params) {
|
||||||
|
if (print) {
|
||||||
|
try {
|
||||||
|
if (params != null) {
|
||||||
|
for (Object object : params) {
|
||||||
|
|
||||||
|
if (object instanceof String) {
|
||||||
|
object = object.toString().replace("$", "RDS_CHAR_DOLLAR");
|
||||||
|
sql = sql.replaceFirst("\\?", "'" + object + "'").replace("RDS_CHAR_DOLLAR", "$");
|
||||||
|
} else {
|
||||||
|
sql = sql.replaceFirst("\\?", String.valueOf(object));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
logger.info(separator + sql);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public void checkOrCreateIndex(Class<?> clazz, String name, boolean unique, List<Map<String, Object>> indexs) {
|
||||||
|
checkOrCreateIndex(clazz, new String[] { name }, unique, indexs);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void checkOrCreateIndex(Class<?> clazz, String[] colums, boolean unique, List<Map<String, Object>> indexs) {
|
||||||
|
List<String> columList = new ArrayList<String>();
|
||||||
|
for (String colum : colums) {
|
||||||
|
columList.add(StrUtil.toUnderlineCase(colum));
|
||||||
|
}
|
||||||
|
String name = StrUtil.join("&", columList) + "@" + StrUtil.toUnderlineCase(clazz.getSimpleName());
|
||||||
|
|
||||||
|
Boolean hasIndex = false;
|
||||||
|
for (Map<String, Object> map : indexs) {
|
||||||
|
if (StrUtil.toUnderlineCase(name).equalsIgnoreCase((String) map.get("name")) || StrUtil.toUnderlineCase(name).equalsIgnoreCase((String) map.get("Key_name"))) {
|
||||||
|
hasIndex = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!hasIndex) {
|
||||||
|
String type = unique ? "UNIQUE INDEX" : "INDEX";
|
||||||
|
String length = "";
|
||||||
|
if (database.equals("mysql")) {
|
||||||
|
length = "(128)";
|
||||||
|
}
|
||||||
|
|
||||||
|
columList = new ArrayList<String>();
|
||||||
|
for (String colum : colums) {
|
||||||
|
columList.add(StrUtil.toUnderlineCase("`" + colum + "`" + length));
|
||||||
|
}
|
||||||
|
|
||||||
|
String sql = "CREATE " + type + " `" + StrUtil.toUnderlineCase(name) + "` ON `" + StrUtil.toUnderlineCase(clazz.getSimpleName()) + "`(" + StrUtil.join(",", columList) + ")";
|
||||||
|
logQuery(formatSql(sql));
|
||||||
|
jdbcTemplate.execute(formatSql(sql));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void checkOrCreateColumn(Class<?> clazz, String name, List<Map<String, Object>> columns) {
|
||||||
|
Boolean hasColumn = false;
|
||||||
|
for (Map<String, Object> map : columns) {
|
||||||
|
if (StrUtil.toUnderlineCase(name).equalsIgnoreCase((String) map.get("name")) || StrUtil.toUnderlineCase(name).equalsIgnoreCase((String) map.get("Field"))) {
|
||||||
|
hasColumn = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!hasColumn) {
|
||||||
|
String sql = "ALTER TABLE `" + StrUtil.toUnderlineCase(clazz.getSimpleName()) + "` ADD COLUMN `" + StrUtil.toUnderlineCase(name) + "` TEXT";
|
||||||
|
logQuery(formatSql(sql));
|
||||||
|
jdbcTemplate.execute(formatSql(sql));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void updateDefaultValue(Class<?> clazz, String column, String value) {
|
||||||
|
String sql = "SELECT COUNT(*) FROM " + StrUtil.toUnderlineCase(clazz.getSimpleName()) + " WHERE `" + StrUtil.toUnderlineCase(column) + "` IS NULL";
|
||||||
|
logQuery(formatSql(sql));
|
||||||
|
Long count = jdbcTemplate.queryForObject(formatSql(sql), Long.class);
|
||||||
|
if (count > 0) {
|
||||||
|
sql = "UPDATE " + StrUtil.toUnderlineCase(clazz.getSimpleName()) + " SET `" + StrUtil.toUnderlineCase(column) + "` = ? WHERE `" + StrUtil.toUnderlineCase(column) + "` IS NULL";
|
||||||
|
logQuery(formatSql(sql));
|
||||||
|
jdbcTemplate.update(formatSql(sql), value);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,107 @@
|
|||||||
|
package com.cym.sqlHelper.utils;
|
||||||
|
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.jdbc.core.JdbcTemplate;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import com.cym.sqlHelper.config.CompositeIndex;
|
||||||
|
import com.cym.sqlHelper.config.InitValue;
|
||||||
|
import com.cym.sqlHelper.config.SingleIndex;
|
||||||
|
import com.cym.sqlHelper.config.Table;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.ReflectUtil;
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class TableUtils {
|
||||||
|
Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||||
|
|
||||||
|
@Value("${spring.database.type}")
|
||||||
|
String database;
|
||||||
|
@Value("${spring.database.package}")
|
||||||
|
String packageName;
|
||||||
|
@Autowired
|
||||||
|
JdbcTemplate jdbcTemplate;
|
||||||
|
@Autowired
|
||||||
|
SqlUtils sqlUtils;
|
||||||
|
|
||||||
|
|
||||||
|
public void initTable(Class<?> clazz) {
|
||||||
|
Table table = clazz.getAnnotation(Table.class);
|
||||||
|
if (table != null) {
|
||||||
|
// 创建表
|
||||||
|
sqlUtils.checkOrCreateTable(clazz);
|
||||||
|
|
||||||
|
// 获取表所有字段
|
||||||
|
String sql = "";
|
||||||
|
if (database.equals("sqlite")) {
|
||||||
|
sql = "PRAGMA TABLE_INFO(`" + StrUtil.toUnderlineCase(clazz.getSimpleName()) + "`)";
|
||||||
|
} else if (database.equals("mysql")) {
|
||||||
|
sql = "SHOW COLUMNS FROM `" + StrUtil.toUnderlineCase(clazz.getSimpleName()) + "`";
|
||||||
|
} else if (database.equals("postgresql")) {
|
||||||
|
sql = "SELECT column_name as name FROM information_schema.columns " + //
|
||||||
|
"WHERE table_schema='public' AND table_name='" + StrUtil.toUnderlineCase(clazz.getSimpleName()) + "'";
|
||||||
|
}
|
||||||
|
sqlUtils.logQuery(sqlUtils.formatSql(sql));
|
||||||
|
List<Map<String, Object>> columns = jdbcTemplate.queryForList(sqlUtils.formatSql(sql));
|
||||||
|
|
||||||
|
// 获取表所有索引
|
||||||
|
if (database.equals("sqlite")) {
|
||||||
|
sql = "PRAGMA INDEX_LIST(`" + StrUtil.toUnderlineCase(clazz.getSimpleName()) + "`)";
|
||||||
|
} else if (database.equals("mysql")) {
|
||||||
|
sql = "SHOW INDEX FROM `" + StrUtil.toUnderlineCase(clazz.getSimpleName()) + "`";
|
||||||
|
} else if (database.equals("postgresql")) {
|
||||||
|
sql = "SELECT " + //
|
||||||
|
"A.INDEXNAME as name " + //
|
||||||
|
"FROM PG_AM B " + //
|
||||||
|
"LEFT JOIN PG_CLASS F ON B.OID = F.RELAM " + //
|
||||||
|
"LEFT JOIN PG_STAT_ALL_INDEXES E ON F.OID = E.INDEXRELID " + //
|
||||||
|
"LEFT JOIN PG_INDEX C ON E.INDEXRELID = C.INDEXRELID " + //
|
||||||
|
"LEFT OUTER JOIN PG_DESCRIPTION D ON C.INDEXRELID = D.OBJOID, " + //
|
||||||
|
"PG_INDEXES A " + //
|
||||||
|
"WHERE " + //
|
||||||
|
"A.SCHEMANAME = E.SCHEMANAME AND A.TABLENAME = E.RELNAME AND A.INDEXNAME = E.INDEXRELNAME " + //
|
||||||
|
"AND E.SCHEMANAME = 'public' AND E.RELNAME = '" + StrUtil.toUnderlineCase(clazz.getSimpleName()) + "' ";//
|
||||||
|
}
|
||||||
|
sqlUtils.logQuery(sqlUtils.formatSql(sql));
|
||||||
|
List<Map<String, Object>> indexs = jdbcTemplate.queryForList(sqlUtils.formatSql(sql));
|
||||||
|
|
||||||
|
// 建立字段
|
||||||
|
Field[] fields = ReflectUtil.getFields(clazz);
|
||||||
|
for (Field field : fields) {
|
||||||
|
// 创建字段
|
||||||
|
if (!field.getName().equals("id")) {
|
||||||
|
sqlUtils.checkOrCreateColumn(clazz, field.getName(), columns);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 创建索引
|
||||||
|
if (field.isAnnotationPresent(SingleIndex.class)) {
|
||||||
|
SingleIndex singleIndex = field.getAnnotation(SingleIndex.class);
|
||||||
|
sqlUtils.checkOrCreateIndex(clazz, field.getName(), singleIndex.unique(), indexs);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 更新表默认值
|
||||||
|
if (field.isAnnotationPresent(InitValue.class)) {
|
||||||
|
InitValue defaultValue = field.getAnnotation(InitValue.class);
|
||||||
|
if (defaultValue.value() != null) {
|
||||||
|
sqlUtils.updateDefaultValue(clazz, field.getName(), defaultValue.value());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取组合索引
|
||||||
|
if (clazz.isAnnotationPresent(CompositeIndex.class)) {
|
||||||
|
CompositeIndex compositeIndex = clazz.getAnnotation(CompositeIndex.class);
|
||||||
|
sqlUtils.checkOrCreateIndex(clazz, compositeIndex.colums(), compositeIndex.unique(), indexs);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
package cn.craccd.test;
|
||||||
Reference in New Issue
Block a user