This commit is contained in:
dftre 2024-10-20 22:52:32 +08:00
parent 73281b8a7f
commit f7c329389d
2 changed files with 95 additions and 2 deletions

View File

@ -1,5 +1,6 @@
package com.ruoyi.mybatis.utils;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
@ -89,4 +90,14 @@ public class QueryUtil {
};
}
public static String listToInSQL(Collection<Serializable> oList) {
StringBuilder sb = new StringBuilder();
sb.append("(");
for (Serializable o : oList) {
sb.append(o).append(",");
}
sb.deleteCharAt(sb.length() - 1);
sb.append(")");
return sb.toString();
}
}

View File

@ -1,6 +1,8 @@
package com.ruoyi.mybatis.utils;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Collection;
import org.apache.ibatis.jdbc.SQL;
@ -18,6 +20,10 @@ public class SQLGenerator {
public final static String UPDATE = "update";
public final static String SELECT_BY_ID = "selectById";
public final static String DELETE_BY_ID = "deleteById";
public final static String SELECT_BY_IDS = "selectByIds";
public final static String DELETE_BY_IDS = "deleteByIds";
public final static String SELECT_BY_ID_WITH_ENTITY = "selectByIdWithEntity";
public final static String DELETE_BY_ID_WITH_ENTITY = "deleteByIdWithEntity";
}
public static <T extends BaseEntity> String list(T entity) {
@ -73,6 +79,9 @@ public class SQLGenerator {
tableInfo.getPrimaryKeys().stream()
.map(column -> column.getColumnName() + " = " + column.getTemplate())
.forEach(sql::WHERE);
if (tableInfo.hasDataScope()) {
sql.WHERE("1=1 ${params.dataScope}");
}
tableInfo.getNotNullColumns(entity).stream()
.filter(column -> !column.isPrimaryKey())
.map(column -> column.getColumnName() + " = " + column.getTemplate())
@ -80,17 +89,21 @@ public class SQLGenerator {
return sql.toString();
}
public static <T extends BaseEntity> String deleteById(T entity) {
public static <T extends BaseEntity> String deleteByIdWithEntity(T entity) {
SQL sql = new SQL();
TableInfo tableInfo = TableContainer.getTableInfo(entity);
sql.DELETE_FROM(tableInfo.getTableName());
tableInfo.getPrimaryKeys().stream()
.map(column -> column.getColumnName() + " = " + column.getTemplate())
.forEach(sql::WHERE);
if (tableInfo.hasDataScope()) {
sql.WHERE("1=1 ${params.dataScope}");
}
return sql.toString();
}
public static <T extends BaseEntity> String selectById(T entity) {
public static <T extends BaseEntity> String selectByIdWithEntity(T entity) {
SQL sql = new SQL();
TableInfo tableInfo = TableContainer.getTableInfo(entity);
sql.SELECT(String.join(",", tableInfo.getColumnNames()))
@ -108,6 +121,75 @@ public class SQLGenerator {
.map(column -> column.getColumnName() + " = " + column.getTemplate())
.forEach(sql::WHERE);
}
if (tableInfo.hasDataScope()) {
sql.WHERE("1=1 ${params.dataScope}");
}
return sql.toString();
}
public static <T extends BaseEntity> String selectById(Serializable id, Class<T> clz) {
SQL sql = new SQL();
TableInfo tableInfo = TableContainer.getTableInfo(clz);
sql.SELECT(String.join(",", tableInfo.getColumnNames()))
.FROM(tableInfo.getTableNameFrom());
if (tableInfo.isEnbleMap()) {
tableInfo.getJoinSql().stream().forEach(sql::LEFT_OUTER_JOIN);
ColumnInfo columnInfo = tableInfo.getPrimaryKeys().get(0);
String columnName = tableInfo.getTableNameT() + "." + columnInfo.getColumnName();
sql.WHERE(columnName + " = " + id);
} else {
ColumnInfo columnInfo = tableInfo.getPrimaryKeys().get(0);
sql.WHERE(columnInfo.getColumnName() + " = " + id);
}
if (tableInfo.hasDataScope()) {
sql.WHERE("1=1 ${params.dataScope}");
}
return sql.toString();
}
public static <T extends BaseEntity> String selectByIds(Collection<Serializable> ids, Class<T> clz) {
SQL sql = new SQL();
TableInfo tableInfo = TableContainer.getTableInfo(clz);
sql.SELECT(String.join(",", tableInfo.getColumnNames()))
.FROM(tableInfo.getTableNameFrom());
if (tableInfo.isEnbleMap()) {
tableInfo.getJoinSql().stream()
.forEach(sql::LEFT_OUTER_JOIN);
ColumnInfo columnInfo = tableInfo.getPrimaryKeys().get(0);
String columnName = tableInfo.getTableNameT() + "." + columnInfo.getColumnName();
sql.WHERE(columnName + " in " + QueryUtil.listToInSQL(ids));
} else {
ColumnInfo columnInfo = tableInfo.getPrimaryKeys().get(0);
sql.WHERE(columnInfo.getColumnName() + " in " + QueryUtil.listToInSQL(ids));
}
if (tableInfo.hasDataScope()) {
sql.WHERE("1=1 ${params.dataScope}");
}
return sql.toString();
}
public static <T extends BaseEntity> String deleteById(Serializable id, Class<T> clz) {
SQL sql = new SQL();
TableInfo tableInfo = TableContainer.getTableInfo(clz);
sql.DELETE_FROM(tableInfo.getTableName());
ColumnInfo columnInfo = tableInfo.getPrimaryKeys().get(0);
sql.WHERE(columnInfo.getColumnName() + " = " + id);
if (tableInfo.hasDataScope()) {
sql.WHERE("1=1 ${params.dataScope}");
}
return sql.toString();
}
public static <T extends BaseEntity> String deleteByIds(Collection<Serializable> ids, Class<T> clz) {
SQL sql = new SQL();
TableInfo tableInfo = TableContainer.getTableInfo(clz);
sql.DELETE_FROM(tableInfo.getTableName());
ColumnInfo columnInfo = tableInfo.getPrimaryKeys().get(0);
sql.WHERE(columnInfo.getColumnName() + " in " + QueryUtil.listToInSQL(ids));
if (tableInfo.hasDataScope()) {
sql.WHERE("1=1 ${params.dataScope}");
}
return sql.toString();
}