diff --git a/pom.xml b/pom.xml index db79a31..4c02f53 100644 --- a/pom.xml +++ b/pom.xml @@ -1,7 +1,6 @@ + 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"> 4.0.0 com.ruoyi @@ -240,6 +239,13 @@ ruoyi-online ${ruoyi.version} + + + + com.ruoyi + ruoyi-mybatis-jpa + ${ruoyi.version} + @@ -253,6 +259,7 @@ ruoyi-oauth ruoyi-pay ruoyi-online + ruoyi-mybatis-jpa pom diff --git a/ruoyi-mybatis-jpa/pom.xml b/ruoyi-mybatis-jpa/pom.xml new file mode 100644 index 0000000..51dd070 --- /dev/null +++ b/ruoyi-mybatis-jpa/pom.xml @@ -0,0 +1,28 @@ + + + + ruoyi + com.ruoyi + 3.8.7.3.2 + + 4.0.0 + + ruoyi-mybatis-jpa + + + ruoyi-mybatis-plus模块 + + + + + + + com.ruoyi + ruoyi-common + + + + + diff --git a/ruoyi-mybatis-jpa/src/main/java/com/ruoyi/mybatis/annotation/Column.java b/ruoyi-mybatis-jpa/src/main/java/com/ruoyi/mybatis/annotation/Column.java new file mode 100644 index 0000000..33efc61 --- /dev/null +++ b/ruoyi-mybatis-jpa/src/main/java/com/ruoyi/mybatis/annotation/Column.java @@ -0,0 +1,13 @@ +package com.ruoyi.mybatis.annotation; + +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 Column { + String name(); // 对应数据库字段名 + boolean primaryKey() default false; // 是否是主键 +} diff --git a/ruoyi-mybatis-jpa/src/main/java/com/ruoyi/mybatis/annotation/ColumnMap.java b/ruoyi-mybatis-jpa/src/main/java/com/ruoyi/mybatis/annotation/ColumnMap.java new file mode 100644 index 0000000..65e4194 --- /dev/null +++ b/ruoyi-mybatis-jpa/src/main/java/com/ruoyi/mybatis/annotation/ColumnMap.java @@ -0,0 +1,14 @@ +package com.ruoyi.mybatis.annotation; + +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 ColumnMap { + String name(); // 对应数据库字段 + String target(); // 映射表来源 + String targetColumn(); // 映射表字段 +} diff --git a/ruoyi-mybatis-jpa/src/main/java/com/ruoyi/mybatis/annotation/EnableTableMap.java b/ruoyi-mybatis-jpa/src/main/java/com/ruoyi/mybatis/annotation/EnableTableMap.java new file mode 100644 index 0000000..3f30357 --- /dev/null +++ b/ruoyi-mybatis-jpa/src/main/java/com/ruoyi/mybatis/annotation/EnableTableMap.java @@ -0,0 +1,12 @@ +package com.ruoyi.mybatis.annotation; + +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.TYPE) +public @interface EnableTableMap { + String name() default "t"; +} diff --git a/ruoyi-mybatis-jpa/src/main/java/com/ruoyi/mybatis/annotation/ParamsMap.java b/ruoyi-mybatis-jpa/src/main/java/com/ruoyi/mybatis/annotation/ParamsMap.java new file mode 100644 index 0000000..71a7b0f --- /dev/null +++ b/ruoyi-mybatis-jpa/src/main/java/com/ruoyi/mybatis/annotation/ParamsMap.java @@ -0,0 +1,13 @@ +package com.ruoyi.mybatis.annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target(ElementType.METHOD) +@Retention(RetentionPolicy.RUNTIME) +public @interface ParamsMap { + Class value(); + String[] paramToProperty(); +} diff --git a/ruoyi-mybatis-jpa/src/main/java/com/ruoyi/mybatis/annotation/Query.java b/ruoyi-mybatis-jpa/src/main/java/com/ruoyi/mybatis/annotation/Query.java new file mode 100644 index 0000000..e5b9ad0 --- /dev/null +++ b/ruoyi-mybatis-jpa/src/main/java/com/ruoyi/mybatis/annotation/Query.java @@ -0,0 +1,20 @@ +package com.ruoyi.mybatis.annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import com.ruoyi.mybatis.enums.QueryEnum; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.FIELD) +public @interface Query { + QueryEnum operation() default QueryEnum.eq; // 操作符,如 eq, like, gt 等 + + String[] section() default { "begin", "end" }; + + boolean params() default false; + + String sql() default ""; +} diff --git a/ruoyi-mybatis-jpa/src/main/java/com/ruoyi/mybatis/annotation/Table.java b/ruoyi-mybatis-jpa/src/main/java/com/ruoyi/mybatis/annotation/Table.java new file mode 100644 index 0000000..3f3392d --- /dev/null +++ b/ruoyi-mybatis-jpa/src/main/java/com/ruoyi/mybatis/annotation/Table.java @@ -0,0 +1,13 @@ +package com.ruoyi.mybatis.annotation; + +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.TYPE) +public @interface Table { + String name(); + String[] columns() default {}; +} diff --git a/ruoyi-mybatis-jpa/src/main/java/com/ruoyi/mybatis/domain/ColumnInfo.java b/ruoyi-mybatis-jpa/src/main/java/com/ruoyi/mybatis/domain/ColumnInfo.java new file mode 100644 index 0000000..7eeca0c --- /dev/null +++ b/ruoyi-mybatis-jpa/src/main/java/com/ruoyi/mybatis/domain/ColumnInfo.java @@ -0,0 +1,93 @@ +package com.ruoyi.mybatis.domain; + +import java.lang.reflect.Field; + +import org.springframework.core.annotation.AnnotationUtils; + +import com.ruoyi.mybatis.annotation.Column; +import com.ruoyi.mybatis.annotation.Query; + +public class ColumnInfo { + private String columnName; + private String fieldName; + private Field field; + private Column column; + private Query query; + private String querySql; + + public ColumnInfo(Field field) { + this.field = field; + this.column = AnnotationUtils.findAnnotation(this.field, Column.class); + this.query = AnnotationUtils.findAnnotation(this.field, Query.class); + this.columnName = this.column.name(); + this.fieldName = this.field.getName(); + this.field.setAccessible(true); + this.querySql = this.getQuerySql(this.query); + } + + public String getColumnName() { + return columnName; + } + + public String getTemplate() { + return getTemplate(false); + } + + public String getTemplate(boolean params) { + if (params) { + return "#{params." + fieldName + "}"; + } else { + return "#{" + fieldName + "}"; + } + } + + public boolean isPrimaryKey() { + return this.column.primaryKey(); + } + + public boolean fieldIsNotNull(Object entity) { + try { + return this.field.get(entity) != null; + } catch (IllegalAccessException e) { + throw new RuntimeException("Failed to access field for building query conditions.", e); + } + } + + public String getQuerySql(Query query) { + if (query == null) + return ""; + String column = this.getColumnName(); + String iField = getTemplate(); + String begin = "#{params." + query.section()[0] + "}"; + String end = "#{params." + query.section()[1] + "}"; + return switch (query.operation()) { + case eq -> column + " = " + iField; + case ne -> column + " <> " + iField; + case gt -> column + " > " + iField; + case ge -> column + " >= " + iField; + case le -> column + " < " + iField; + case lt -> column + " <= " + iField; + case between -> column + " between " + begin + " and " + end; + case notBetween -> column + " not between " + begin + " and " + end; + case like -> column + " like concat('%'," + iField + ",'%')"; + case notLike -> column + "not like concat('%'," + iField + ",'%')"; + case likeLeft -> column + "like concat('%'," + iField + ")"; + case likeRight -> column + "like concat(" + iField + ",'%')"; + case notLikeLeft -> column + "not like concat('%'," + iField + ")"; + case notLikeRight -> column + "not like concat(" + iField + ",'%')"; + case isNull -> column + " is null"; + case isNotNull -> column + " is not null"; + case in -> column + " in (" + iField + ""; + case notIn -> column + " not in (" + iField + ""; + case inSql -> column + " in (" + query.sql() + ")"; + case notInSql -> column + " not in (" + query.sql() + ")"; + default -> throw new IllegalArgumentException( + "Unsupported operation: " + query.operation()); + }; + + } + + public String getQuerySql() { + return this.querySql; + } +} diff --git a/ruoyi-mybatis-jpa/src/main/java/com/ruoyi/mybatis/domain/MapColumnInfo.java b/ruoyi-mybatis-jpa/src/main/java/com/ruoyi/mybatis/domain/MapColumnInfo.java new file mode 100644 index 0000000..5234775 --- /dev/null +++ b/ruoyi-mybatis-jpa/src/main/java/com/ruoyi/mybatis/domain/MapColumnInfo.java @@ -0,0 +1,93 @@ +package com.ruoyi.mybatis.domain; + +import java.lang.reflect.Field; + +import org.springframework.core.annotation.AnnotationUtils; + +import com.ruoyi.mybatis.annotation.ColumnMap; +import com.ruoyi.mybatis.annotation.Query; + +public class MapColumnInfo { + private String columnName; + private String fieldName; + private Field field; + private Query query; + private ColumnMap columnMap; + private String querySql; + + public MapColumnInfo(Field field) { + this.field = field; + this.columnMap = AnnotationUtils.findAnnotation(this.field, ColumnMap.class); + this.columnName = this.columnMap.name(); + this.query = AnnotationUtils.findAnnotation(this.field, Query.class); + this.fieldName = this.field.getName(); + this.field.setAccessible(true); + this.querySql = this.getQuerySql(this.query); + } + + public ColumnMap getJoin() { + return this.columnMap; + } + + public String getColumnName() { + return columnName; + } + + public String getTemplate() { + return getTemplate(false); + } + + public String getTemplate(boolean params) { + if (params) { + return "#{params." + fieldName + "}"; + } else { + return "#{" + fieldName + "}"; + } + } + + public boolean fieldIsNotNull(Object entity) { + try { + return this.field.get(entity) != null; + } catch (IllegalAccessException e) { + throw new RuntimeException("Failed to access field for building query conditions.", e); + } + } + + public String getQuerySql(Query query) { + if (query == null) + return ""; + String column = this.getColumnName(); + String iField = getTemplate(); + String begin = "#{params." + query.section()[0] + "}"; + String end = "#{params." + query.section()[1] + "}"; + return switch (query.operation()) { + case eq -> column + " = " + iField; + case ne -> column + " <> " + iField; + case gt -> column + " > " + iField; + case ge -> column + " >= " + iField; + case le -> column + " < " + iField; + case lt -> column + " <= " + iField; + case between -> column + " between " + begin + " and " + end; + case notBetween -> column + " not between " + begin + " and " + end; + case like -> column + " like concat('%'," + iField + ",'%')"; + case notLike -> column + "not like concat('%'," + iField + ",'%')"; + case likeLeft -> column + "like concat('%'," + iField + ")"; + case likeRight -> column + "like concat(" + iField + ",'%')"; + case notLikeLeft -> column + "not like concat('%'," + iField + ")"; + case notLikeRight -> column + "not like concat(" + iField + ",'%')"; + case isNull -> column + " is null"; + case isNotNull -> column + " is not null"; + case in -> column + " in (" + iField + ""; + case notIn -> column + " not in (" + iField + ""; + case inSql -> column + " in (" + query.sql() + ")"; + case notInSql -> column + " not in (" + query.sql() + ")"; + default -> throw new IllegalArgumentException( + "Unsupported operation: " + query.operation()); + }; + } + + public String getQuerySql() { + return this.columnMap.target() + "." + this.querySql; + } + +} diff --git a/ruoyi-mybatis-jpa/src/main/java/com/ruoyi/mybatis/domain/TableInfo.java b/ruoyi-mybatis-jpa/src/main/java/com/ruoyi/mybatis/domain/TableInfo.java new file mode 100644 index 0000000..9745b2e --- /dev/null +++ b/ruoyi-mybatis-jpa/src/main/java/com/ruoyi/mybatis/domain/TableInfo.java @@ -0,0 +1,124 @@ +package com.ruoyi.mybatis.domain; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; + +import org.springframework.core.annotation.AnnotationUtils; + +import com.ruoyi.mybatis.annotation.Column; +import com.ruoyi.mybatis.annotation.ColumnMap; +import com.ruoyi.mybatis.annotation.EnableTableMap; +import com.ruoyi.mybatis.annotation.Table; + +public class TableInfo { + private String tableName; + private EnableTableMap enableTableMap; + private Table table; + private List columns = new ArrayList<>(); // 所有标记column注解的字段 + private List primaryKeys = new ArrayList<>(); + private List mapColumns = new ArrayList<>(); + private Set joinSql = new HashSet<>(); + + public TableInfo(Class cls) { + this.table = AnnotationUtils.findAnnotation(cls, Table.class); + if (this.table == null) + throw new RuntimeException("error , not find tableName"); + this.tableName = this.table.name(); + // 获取所有标记Column注解的字段 + this.enableTableMap = AnnotationUtils.findAnnotation(cls, EnableTableMap.class); + + Arrays.stream(cls.getDeclaredFields()) + .filter(field -> AnnotationUtils.findAnnotation(field, Column.class) != null) + .map(field -> new ColumnInfo(field)) + .forEach(this.columns::add); + + Arrays.stream(cls.getDeclaredFields()) + .filter(field -> AnnotationUtils.findAnnotation(field, ColumnMap.class) != null) + .map(field -> new MapColumnInfo(field)) + .forEach(this.mapColumns::add); + + this.getColumns().stream() + .filter(ColumnInfo::isPrimaryKey) + .forEach(this.primaryKeys::add); + + this.getMapColumns().stream() + .map(MapColumnInfo::getJoin) + .map(join -> join.target() + " on " + + join.target() + "." + join.targetColumn() + " = " + + this.getTableNameT() + "." + join.targetColumn()) + .forEach(joinSql::add); + + } + + public Set getJoinSql() { + return this.joinSql; + } + + public Boolean isEnbleMap() { + return this.enableTableMap != null; + } + + public String getTableNameT() { + return this.enableTableMap.name(); + } + + public String getTableNameFrom() { + if (this.isEnbleMap()) + return this.tableName + " " + this.enableTableMap.name(); + else + return this.tableName; + } + + public List getQueryColumns() { + List columns = Arrays.asList(this.table.columns()); + if (columns.size() <= 0) { + columns = this.columns.stream() + .map(ColumnInfo::getColumnName) + .collect(Collectors.toList()); + } + if (this.isEnbleMap()) { + columns = columns.stream() + .map(column -> this.getTableNameT() + "." + column) + .collect(Collectors.toList()); + this.mapColumns.stream() + .map(column -> column.getJoin().target() + "." + column.getColumnName()) + .forEach(columns::add); + } + + return columns; + + } + + public List getColumns() { + return columns; + } + + public List getMapColumns() { + return mapColumns; + } + + public List getNotNullColumns(T entity) { + return this.columns.stream() + .filter(column -> column.fieldIsNotNull(entity)) + .collect(Collectors.toList()); + } + + public List getNotNullMapColumns(T entity) { + return this.mapColumns.stream() + .filter(column -> column.fieldIsNotNull(entity)) + .collect(Collectors.toList()); + } + + public List getPrimaryKeys() { + return primaryKeys; + } + + public String getTableName() { + return tableName; + } + +} diff --git a/ruoyi-mybatis-jpa/src/main/java/com/ruoyi/mybatis/enums/QueryEnum.java b/ruoyi-mybatis-jpa/src/main/java/com/ruoyi/mybatis/enums/QueryEnum.java new file mode 100644 index 0000000..5eecd68 --- /dev/null +++ b/ruoyi-mybatis-jpa/src/main/java/com/ruoyi/mybatis/enums/QueryEnum.java @@ -0,0 +1,24 @@ +package com.ruoyi.mybatis.enums; + +public enum QueryEnum { + eq, // 等于 = + ne, // 不等于 <> + gt, // 大于 > + ge, // 大于等于 >= + lt, // 小于 < + le, // 小于等于 <= + like, // 模糊查询 + notLike, + likeLeft, // 左模糊查询 + likeRight, // 右模糊查询 + notLikeLeft, + notLikeRight, + in, // in + notIn, // not in + between, // between and + notBetween, // not between and + isNull, //is null + isNotNull, + inSql, + notInSql; +} diff --git a/ruoyi-mybatis-jpa/src/main/java/com/ruoyi/mybatis/utils/QueryWrapperUtil.java b/ruoyi-mybatis-jpa/src/main/java/com/ruoyi/mybatis/utils/QueryWrapperUtil.java new file mode 100644 index 0000000..4a10d17 --- /dev/null +++ b/ruoyi-mybatis-jpa/src/main/java/com/ruoyi/mybatis/utils/QueryWrapperUtil.java @@ -0,0 +1,69 @@ +// package com.ruoyi.mybatis.utils; + +// import java.util.Arrays; +// import java.util.Map; + +// import org.springframework.core.annotation.AnnotationUtils; + +// import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +// import com.ruoyi.common.core.domain.BaseEntity; +// import com.ruoyi.mybatis.annotation.Query; + +// public class QueryWrapperUtil { +// public static QueryWrapper initQueryWrapper(T entity) { +// QueryWrapper queryWrapper = new QueryWrapper<>(); + +// Class clazz = entity.getClass(); +// Map params = entity.getParams(); +// Arrays.stream(clazz.getDeclaredFields()) +// .filter(field -> AnnotationUtils.findAnnotation(field, Query.class) != null) +// .forEach(field -> { +// field.setAccessible(true); +// Query queryAnnotation = field.getAnnotation(Query.class); + +// try { +// Object fieldValue = field.get(entity); +// if (fieldValue != null) { // 判断属性值是否非空 +// switch (queryAnnotation.operation()) { +// case eq -> queryWrapper.eq(queryAnnotation.column(), fieldValue); +// case ne -> queryWrapper.ne(queryAnnotation.column(), fieldValue); +// case gt -> queryWrapper.gt(queryAnnotation.column(), fieldValue); +// case ge -> queryWrapper.ge(queryAnnotation.column(), fieldValue); +// case le -> queryWrapper.le(queryAnnotation.column(), fieldValue); +// case lt -> queryWrapper.lt(queryAnnotation.column(), fieldValue); +// case between -> { +// String begin = queryAnnotation.section()[0]; +// String end = queryAnnotation.section()[1]; +// queryWrapper.between(queryAnnotation.column(), params.get(begin), params.get(end)); +// } +// case notBetween -> { +// String begin = queryAnnotation.section()[0]; +// String end = queryAnnotation.section()[1]; +// queryWrapper.notBetween(queryAnnotation.column(), params.get(begin), +// params.get(end)); +// } +// case like -> queryWrapper.like(queryAnnotation.column(), fieldValue); +// case notLike -> queryWrapper.notLike(queryAnnotation.column(), fieldValue); +// case likeLeft -> queryWrapper.likeLeft(queryAnnotation.column(), fieldValue); +// case likeRight -> queryWrapper.likeRight(queryAnnotation.column(), fieldValue); +// case notLikeLeft -> queryWrapper.notLikeLeft(queryAnnotation.column(), fieldValue); +// case notLikeRight -> queryWrapper.notLikeRight(queryAnnotation.column(), fieldValue); +// case isNull -> queryWrapper.isNull(queryAnnotation.column()); +// case isNotNull -> queryWrapper.isNotNull(queryAnnotation.column()); +// case in -> queryWrapper.in(queryAnnotation.column(), (Object[]) fieldValue); +// case notIn -> queryWrapper.notIn(queryAnnotation.column(), (Object[]) fieldValue); +// case inSql -> queryWrapper.inSql(queryAnnotation.column(), queryAnnotation.sql()); +// case notInSql -> queryWrapper.notInSql(queryAnnotation.column(), queryAnnotation.sql()); +// default -> +// throw new IllegalArgumentException( +// "Unsupported operation: " + queryAnnotation.operation()); +// } +// } +// } catch (IllegalAccessException e) { +// throw new RuntimeException("Failed to access field for building query conditions.", e); +// } +// }); + +// return queryWrapper; +// } +// } diff --git a/ruoyi-mybatis-jpa/src/main/java/com/ruoyi/mybatis/utils/SQLUtil.java b/ruoyi-mybatis-jpa/src/main/java/com/ruoyi/mybatis/utils/SQLUtil.java new file mode 100644 index 0000000..fd5f835 --- /dev/null +++ b/ruoyi-mybatis-jpa/src/main/java/com/ruoyi/mybatis/utils/SQLUtil.java @@ -0,0 +1,115 @@ +package com.ruoyi.mybatis.utils; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.ibatis.jdbc.SQL; + +import com.ruoyi.common.core.domain.BaseEntity; +import com.ruoyi.mybatis.domain.ColumnInfo; +import com.ruoyi.mybatis.domain.MapColumnInfo; +import com.ruoyi.mybatis.domain.TableInfo; + +import lombok.extern.slf4j.Slf4j; + +@Slf4j +public class SQLUtil { + + private static final Map, TableInfo> tableInfoMap = new HashMap<>(); + + private static TableInfo getTableInfo(T entity) { + Class clazz = entity.getClass(); + TableInfo tableInfo = tableInfoMap.get(clazz); + if (tableInfo == null) + tableInfo = new TableInfo(clazz); + return tableInfo; + } + + public static String list(T entity) { + SQL sql = new SQL(); + TableInfo tableInfo = getTableInfo(entity); + sql.SELECT(String.join(",", tableInfo.getQueryColumns())) + .FROM(tableInfo.getTableNameFrom()); + + if (tableInfo.isEnbleMap()) { + tableInfo.getJoinSql().stream() + .forEach(sql::LEFT_OUTER_JOIN); + tableInfo.getNotNullMapColumns(entity).stream() + .map(MapColumnInfo::getQuerySql) + .forEach(sql::WHERE); + tableInfo.getNotNullColumns(entity).stream() + .map(ColumnInfo::getQuerySql) + .map(query -> tableInfo.getTableNameT() + "." + query) + .forEach(sql::WHERE); + } else { + tableInfo.getNotNullColumns(entity).stream() + .map(ColumnInfo::getQuerySql) + .forEach(sql::WHERE); + } + log.debug(sql.toString()); + return sql.toString(); + } + + public static String insert(T entity) { + SQL sql = new SQL(); + TableInfo tableInfo = getTableInfo(entity); + sql.INSERT_INTO(tableInfo.getTableName()); + tableInfo.getNotNullColumns(entity).stream() + .forEach(column -> sql.VALUES(column.getColumnName(), column.getTemplate())); + + return sql.toString(); + } + + public static String update(T entity) { + SQL sql = new SQL(); + TableInfo tableInfo = getTableInfo(entity); + sql.UPDATE(tableInfo.getTableName()); + List sets = new ArrayList(); + tableInfo.getColumns().stream() + .forEach(column -> { + if (column.isPrimaryKey()) { + sql.WHERE(column.getColumnName() + " = " + column.getTemplate()); + } else { + sets.add(column.getColumnName() + " = " + column.getTemplate()); + } + }); + sql.SET(sets.toArray(new String[0])); + return sql.toString(); + } + + public static String deleteById(T entity) { + SQL sql = new SQL(); + TableInfo tableInfo = getTableInfo(entity); + sql.DELETE_FROM(tableInfo.getTableName()); + tableInfo.getPrimaryKeys().stream() + .map(column -> column.getColumnName() + " = " + column.getTemplate()) + .forEach(sql::WHERE); + + return sql.toString(); + } + + public static String selectById(T entity) { + SQL sql = new SQL(); + TableInfo tableInfo = getTableInfo(entity); + sql.SELECT(String.join(",", tableInfo.getQueryColumns())) + .FROM(tableInfo.getTableNameFrom()); + + if (tableInfo.isEnbleMap()) { + tableInfo.getJoinSql().stream() + .forEach(sql::LEFT_OUTER_JOIN); + + tableInfo.getPrimaryKeys().stream() + .map(column -> tableInfo.getTableNameT() + "." + column.getColumnName() + " = " + + column.getTemplate()) + .forEach(sql::WHERE); + } else { + tableInfo.getPrimaryKeys().stream() + .map(column -> column.getColumnName() + " = " + column.getTemplate()) + .forEach(sql::WHERE); + } + + return sql.toString(); + } +}