清理无用代码。

This commit is contained in:
mxd
2021-07-15 20:47:34 +08:00
parent 82aa155848
commit 4ad8db09cc
4 changed files with 296 additions and 316 deletions

View File

@@ -2,23 +2,20 @@ package org.ssssssss.magicapi.spring.boot.starter;
/**
* CRUD 配置
*
* @author 冰点
* @date 2021-7-15 09:26:17
* @since 1.3.3
* @since 1.3.4
*/
public class CrudConfig {
/**
* 逻辑删除列
*/
private String logicDeleteColumn="is_valid";
private String logicDeleteColumn = "is_valid";
/**
* 逻辑删除值
*/
private String logicDeleteValue="0";
/**
* 是否控制并发插入
*/
private boolean isLimitParallel=false;
private String logicDeleteValue = "0";
public String getLogicDeleteColumn() {
return logicDeleteColumn;
@@ -36,11 +33,4 @@ public class CrudConfig {
this.logicDeleteValue = logicDeleteValue;
}
public boolean isLimitParallel() {
return isLimitParallel;
}
public void setLimitParallel(boolean limitParallel) {
isLimitParallel = limitParallel;
}
}

View File

@@ -403,7 +403,6 @@ public class MagicAPIAutoConfiguration implements WebMvcConfigurer, WebSocketCon
sqlModule.setDialectAdapter(dialectAdapter);
sqlModule.setLogicDeleteColumn(properties.getCrudConfig().getLogicDeleteColumn());
sqlModule.setLogicDeleteValue(properties.getCrudConfig().getLogicDeleteColumn());
sqlModule.setLimitParallel(properties.getCrudConfig().isLimitParallel());
return sqlModule;
}

View File

@@ -1,7 +1,6 @@
package org.ssssssss.magicapi.modules;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.env.Environment;
import org.springframework.dao.DataRetrievalFailureException;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.jdbc.core.ArgumentPreparedStatementSetter;
@@ -63,9 +62,11 @@ public class SQLModule extends HashMap<String, SQLModule> implements MagicModule
private List<SQLInterceptor> sqlInterceptors;
private long ttl;
private String logicDeleteColumn;
private String logicDeleteValue;
private boolean isLimitParallel;
public SQLModule() {
}
@@ -150,14 +151,6 @@ public class SQLModule extends HashMap<String, SQLModule> implements MagicModule
this.logicDeleteValue = logicDeleteValue;
}
public boolean isLimitParallel() {
return isLimitParallel;
}
public void setLimitParallel(boolean limitParallel) {
isLimitParallel = limitParallel;
}
protected SqlCache getSqlCache() {
return sqlCache;
}
@@ -347,13 +340,14 @@ public class SQLModule extends HashMap<String, SQLModule> implements MagicModule
this.sqlCache.delete(this.cacheName);
}
}
/**
* 插入并返回主键
*/
@Comment("批量执行insert操作返回插入主键数组")
public int[] batchInsert(@Comment("`SQL`语句") String sql,@Comment("参数")List<Object[]> list) {
return dataSourceNode.getJdbcTemplate().batchUpdate(sql,list);
}
/**
* 插入并返回主键
*/
@Comment("批量执行insert操作返回插入主键数组")
public int[] batchInsert(@Comment("`SQL`语句") String sql, @Comment("参数") List<Object[]> list) {
return dataSourceNode.getJdbcTemplate().batchUpdate(sql, list);
}
/**
* 插入并返回主键
@@ -415,8 +409,8 @@ public class SQLModule extends HashMap<String, SQLModule> implements MagicModule
}
@UnableCall
public Integer selectInt(BoundSql boundSql){
return boundSql.getCacheValue(this.sqlInterceptors, () -> dataSourceNode.getJdbcTemplate().query(boundSql.getSql(),new SingleRowResultSetExtractor<>(Integer.class), boundSql.getParameters()));
public Integer selectInt(BoundSql boundSql) {
return boundSql.getCacheValue(this.sqlInterceptors, () -> dataSourceNode.getJdbcTemplate().query(boundSql.getSql(), new SingleRowResultSetExtractor<>(Integer.class), boundSql.getParameters()));
}
/**

View File

@@ -1,16 +1,12 @@
package org.ssssssss.magicapi.modules.table;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import org.ssssssss.magicapi.exception.MagicAPIException;
import org.ssssssss.magicapi.modules.BoundSql;
import org.ssssssss.magicapi.modules.SQLModule;
import org.ssssssss.script.annotation.Comment;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Function;
import java.util.stream.Collectors;
@@ -18,327 +14,328 @@ import java.util.stream.Collectors;
* @author
*/
public class NamedTable {
String tableName;
String tableName;
SQLModule sqlModule;
SQLModule sqlModule;
String primary;
String logicDeleteColumn;
String logicDeleteValue;
boolean isLimitParallel;
Map<String, Object> columns = new HashMap<>();
String primary;
List<String> fields = new ArrayList<>();
String logicDeleteColumn;
List<String> groups = new ArrayList<>();
String logicDeleteValue;
List<String> orders = new ArrayList<>();
Map<String, Object> columns = new HashMap<>();
Function<String, String> rowMapColumnMapper;
List<String> fields = new ArrayList<>();
Object defaultPrimaryValue;
List<String> groups = new ArrayList<>();
Where where = new Where(this);
List<String> orders = new ArrayList<>();
public NamedTable(String tableName, SQLModule sqlModule, Function<String, String> rowMapColumnMapper) {
this.tableName = tableName;
this.sqlModule = sqlModule;
this.rowMapColumnMapper = rowMapColumnMapper;
this.logicDeleteColumn = sqlModule.getLogicDeleteColumn();
this.logicDeleteValue = sqlModule.getLogicDeleteValue();
this.isLimitParallel = sqlModule.isLimitParallel();
}
Function<String, String> rowMapColumnMapper;
@Comment("设置主键名update时使用")
public NamedTable primary(String primary) {
return primary(primary, null);
}
Object defaultPrimaryValue;
@Comment("设置主键名,并设置默认主键值(主要用于insert)")
public NamedTable primary(String primary, Object defaultPrimaryValue) {
this.primary = rowMapColumnMapper.apply(primary);
this.defaultPrimaryValue = defaultPrimaryValue;
return this;
}
Where where = new Where(this);
@Comment("拼接where")
public Where where() {
return where;
}
public NamedTable(String tableName, SQLModule sqlModule, Function<String, String> rowMapColumnMapper) {
this.tableName = tableName;
this.sqlModule = sqlModule;
this.rowMapColumnMapper = rowMapColumnMapper;
this.logicDeleteColumn = sqlModule.getLogicDeleteColumn();
this.logicDeleteValue = sqlModule.getLogicDeleteValue();
}
@Comment("设置单列的值")
public NamedTable column(@Comment("列名") String key, @Comment("") Object value) {
this.columns.put(rowMapColumnMapper.apply(key), value);
return this;
}
@Comment("设置主键名update时使用")
public NamedTable primary(String primary) {
return primary(primary, null);
}
@Comment("设置查询的列,如`columns('a','b','c')`")
public NamedTable columns(@Comment("各项列") String... columns) {
if (columns != null) {
for (String column : columns) {
column(column);
}
}
return this;
}
@Comment("设置主键名,并设置默认主键值(主要用于insert)")
public NamedTable primary(String primary, Object defaultPrimaryValue) {
this.primary = rowMapColumnMapper.apply(primary);
this.defaultPrimaryValue = defaultPrimaryValue;
return this;
}
@Comment("设置查询的列,如`columns(['a','b','c'])`")
public NamedTable columns(Collection<String> columns) {
if (columns != null) {
columns.stream().filter(StringUtils::isNotBlank).map(rowMapColumnMapper).forEach(this.fields::add);
}
return this;
}
@Comment("拼接where")
public Where where() {
return where;
}
@Comment("设置查询的列,如`column('a')`")
public NamedTable column(String column) {
if (StringUtils.isNotBlank(column)) {
this.fields.add(this.rowMapColumnMapper.apply(column));
}
return this;
}
@Comment("设置单列的值")
public NamedTable column(@Comment("列名") String key, @Comment("") Object value) {
this.columns.put(rowMapColumnMapper.apply(key), value);
return this;
}
@Comment("拼接`order by xxx asc/desc`")
public NamedTable orderBy(@Comment("要排序的") String column, @Comment("`asc`或`desc`") String sort) {
this.orders.add(column + " " + sort);
return this;
}
@Comment("设置查询的列,如`columns('a','b','c')`")
public NamedTable columns(@Comment("各项") String... columns) {
if (columns != null) {
for (String column : columns) {
column(column);
}
}
return this;
}
@Comment("拼接`order by xxx asc`")
public NamedTable orderBy(@Comment("要排序的列") String column) {
return orderBy(column, "asc");
}
@Comment("设置查询的列,如`columns(['a','b','c'])`")
public NamedTable columns(Collection<String> columns) {
if (columns != null) {
columns.stream().filter(StringUtils::isNotBlank).map(rowMapColumnMapper).forEach(this.fields::add);
}
return this;
}
@Comment("拼接`order by xxx desc`")
public NamedTable orderByDesc(@Comment("要排序的列") String column) {
return orderBy(column, "desc");
}
@Comment("设置查询的列,如`column('a')`")
public NamedTable column(String column) {
if (StringUtils.isNotBlank(column)) {
this.fields.add(this.rowMapColumnMapper.apply(column));
}
return this;
}
@Comment("拼接`group by`")
public NamedTable groupBy(@Comment("分组的列") String... columns) {
this.groups.addAll(Arrays.asList(columns));
return this;
}
@Comment("拼接`order by xxx asc/desc`")
public NamedTable orderBy(@Comment("排序的列") String column, @Comment("`asc`或`desc`") String sort) {
this.orders.add(column + " " + sort);
return this;
}
private List<Map.Entry<String, Object>> filterNotBlanks() {
return this.columns.entrySet().stream()
.filter(it -> StringUtils.isNotBlank(Objects.toString(it.getValue(), "")))
.collect(Collectors.toList());
}
@Comment("拼接`order by xxx asc`")
public NamedTable orderBy(@Comment("要排序的列") String column) {
return orderBy(column, "asc");
}
@Comment("执行插入,返回主键")
public Object insert() {
return insert(null);
}
@Comment("拼接`order by xxx desc`")
public NamedTable orderByDesc(@Comment("要排序的列") String column) {
return orderBy(column, "desc");
}
@Comment("执行插入,返回主键")
public Object insert(@Comment("各项列和值") Map<String, Object> data) {
if (data != null) {
data.forEach((key, value) -> this.columns.put(rowMapColumnMapper.apply(key), value));
}
if (this.defaultPrimaryValue != null && StringUtils.isBlank(Objects.toString(this.columns.getOrDefault(this.primary, "")))) {
this.columns.put(this.primary, this.defaultPrimaryValue);
}
List<Map.Entry<String, Object>> entries = filterNotBlanks();
if (entries.isEmpty()) {
throw new MagicAPIException("参数不能为空");
}
StringBuilder builder = new StringBuilder();
builder.append("insert into ");
builder.append(tableName);
builder.append("(");
builder.append(StringUtils.join(entries.stream().map(Map.Entry::getKey).toArray(), ","));
builder.append(") values (");
builder.append(StringUtils.join(Collections.nCopies(entries.size(), "?"), ","));
builder.append(")");
return sqlModule.insert(new BoundSql(builder.toString(), entries.stream().map(Map.Entry::getValue).collect(Collectors.toList()), sqlModule), this.primary);
}
@Comment("拼接`group by`")
public NamedTable groupBy(@Comment("要分组的列") String... columns) {
this.groups.addAll(Arrays.asList(columns));
return this;
}
@Comment("执行delete语句(物理删除)")
public int delete() {
if (where.isEmpty()) {
throw new MagicAPIException("delete语句不能没有条件");
}
StringBuilder builder = new StringBuilder();
builder.append("delete from ");
builder.append(tableName);
builder.append(where.getSql());
return sqlModule.update(new BoundSql(builder.toString(), where.getParams(), sqlModule));
}
private List<Map.Entry<String, Object>> filterNotBlanks() {
return this.columns.entrySet().stream()
.filter(it -> StringUtils.isNotBlank(Objects.toString(it.getValue(), "")))
.collect(Collectors.toList());
}
@Comment("执行delete语句")
public int delete(@Comment("是否逻辑删除") boolean isLogicDelete) {
if (where.isEmpty()) {
throw new MagicAPIException("delete语句不能没有条件");
}
if (!isLogicDelete) {
return delete();
} else {
Map<String, Object> dataMap = new HashMap<>();
dataMap.put(logicDeleteColumn, logicDeleteValue);
return update(dataMap);
}
}
@Comment("执行插入,返回主键")
public Object insert() {
return insert(null);
}
@Comment("保存到表中,当主键有值时则修改,否则插入")
public Object save() {
return this.save(null, false);
}
@Comment("执行插入,返回主键")
public Object insert(@Comment("各项列和值") Map<String, Object> data) {
if (data != null) {
data.forEach((key, value) -> this.columns.put(rowMapColumnMapper.apply(key), value));
}
if (this.defaultPrimaryValue != null && StringUtils.isBlank(Objects.toString(this.columns.getOrDefault(this.primary, "")))) {
this.columns.put(this.primary, this.defaultPrimaryValue);
}
List<Map.Entry<String, Object>> entries = filterNotBlanks();
if (entries.isEmpty()) {
throw new MagicAPIException("参数不能为空");
}
StringBuilder builder = new StringBuilder();
builder.append("insert into ");
builder.append(tableName);
builder.append("(");
builder.append(StringUtils.join(entries.stream().map(Map.Entry::getKey).toArray(), ","));
builder.append(") values (");
builder.append(StringUtils.join(Collections.nCopies(entries.size(), "?"), ","));
builder.append(")");
return sqlModule.insert(new BoundSql(builder.toString(), entries.stream().map(Map.Entry::getValue).collect(Collectors.toList()), sqlModule), this.primary);
}
@Comment("保存到表中,当主键有值时则修改,否则插入")
public Object save(@Comment("各项列和值") Map<String, Object> data, @Comment("是否根据id查询有没有数据") boolean beforeQuery) {
if (StringUtils.isBlank(this.primary)) {
throw new MagicAPIException("请设置主键");
}
String primaryValue = Objects.toString(this.columns.get(this.primary), "");
if (StringUtils.isBlank(primaryValue) && data != null) {
primaryValue = Objects.toString(data.get(this.primary), "");
}
if (beforeQuery) {
if (StringUtils.isNotBlank(primaryValue)) {
List<Object> params = new ArrayList<>();
params.add(primaryValue);
Integer count = sqlModule.selectInt(new BoundSql("select count(*) count from " + this.tableName + " where " + this.primary + " = ?", params, sqlModule));
if (count == 0) {
return insert(data);
}
return update(data);
} else {
return insert(data);
}
}
@Comment("执行delete语句(物理删除)")
public int delete() {
if (where.isEmpty()) {
throw new MagicAPIException("delete语句不能没有条件");
}
StringBuilder builder = new StringBuilder();
builder.append("delete from ");
builder.append(tableName);
builder.append(where.getSql());
return sqlModule.update(new BoundSql(builder.toString(), where.getParams(), sqlModule));
}
if (StringUtils.isNotBlank(primaryValue)) {
return update(data);
}
return insert(data);
}
@Comment("执行delete语句")
public int delete(@Comment("是否逻辑删除") boolean isLogicDelete) {
if (where.isEmpty()) {
throw new MagicAPIException("delete语句不能没有条件");
}
if (!isLogicDelete) {
return delete();
} else {
Map<String, Object> dataMap = new HashMap<>();
dataMap.put(logicDeleteColumn, logicDeleteValue);
return update(dataMap);
}
}
@Comment("保存到表中,当主键有值时则修改,否则插入")
public Object save(boolean beforeQuery) {
return this.save(null, beforeQuery);
}
@Comment("保存到表中,当主键有值时则修改,否则插入")
public Object save() {
return this.save(null, false);
}
@Comment("保存到表中,当主键有值时则修改,否则插入")
public Object save(@Comment("各项列和值") Map<String, Object> data) {
return this.save(data, false);
}
@Comment("保存到表中,当主键有值时则修改,否则插入")
public Object save(@Comment("各项列和值") Map<String, Object> data, @Comment("是否根据id查询有没有数据") boolean beforeQuery) {
if (StringUtils.isBlank(this.primary)) {
throw new MagicAPIException("请设置主键");
}
String primaryValue = Objects.toString(this.columns.get(this.primary), "");
if (StringUtils.isBlank(primaryValue) && data != null) {
primaryValue = Objects.toString(data.get(this.primary), "");
}
if (beforeQuery) {
if (StringUtils.isNotBlank(primaryValue)) {
List<Object> params = new ArrayList<>();
params.add(primaryValue);
Integer count = sqlModule.selectInt(new BoundSql("select count(*) count from " + this.tableName + " where " + this.primary + " = ?", params, sqlModule));
if (count == 0) {
return insert(data);
}
return update(data);
} else {
return insert(data);
}
}
if (StringUtils.isNotBlank(primaryValue)) {
return update(data);
}
return insert(data);
}
@Comment("保存到表中,当主键有值时则修改,否则插入")
public Object save(boolean beforeQuery) {
return this.save(null, beforeQuery);
}
@Comment("保存到表中,当主键有值时则修改,否则插入")
public Object save(@Comment("各项列和值") Map<String, Object> data) {
return this.save(data, false);
}
@Comment("执行`select`查询")
public List<Map<String, Object>> select() {
return sqlModule.select(buildSelect());
}
@Comment("执行`select`查询")
public List<Map<String, Object>> select() {
return sqlModule.select(buildSelect());
}
@Comment("执行`select`查询")
public List<Map<String, Object>> select(@Comment("排除无效数据") boolean excludeInvalid) {
return sqlModule.select(buildSelect(excludeInvalid));
}
@Comment("执行`select`查询")
public List<Map<String, Object>> select(@Comment("排除无效数据") boolean excludeInvalid) {
return sqlModule.select(buildSelect(excludeInvalid));
}
@Comment("执行`selectOne`查询")
public Map<String, Object> selectOne() {
return sqlModule.selectOne(buildSelect());
}
@Comment("执行`selectOne`查询")
public Map<String, Object> selectOne() {
return sqlModule.selectOne(buildSelect());
}
@Comment("执行`selectOne`查询")
public Map<String, Object> selectOne(@Comment("排除无效数据") boolean excludeInvalid) {
return sqlModule.selectOne(buildSelect(excludeInvalid));
}
@Comment("执行`selectOne`查询")
public Map<String, Object> selectOne(@Comment("排除无效数据") boolean excludeInvalid) {
return sqlModule.selectOne(buildSelect(excludeInvalid));
}
private BoundSql buildSelect() {
return buildSelect(false);
}
private BoundSql buildSelect() {
return buildSelect(false);
}
private BoundSql buildSelect(boolean excludeInvalid) {
StringBuilder builder = new StringBuilder();
builder.append("select ");
if (this.fields.isEmpty()) {
builder.append("*");
} else {
builder.append(StringUtils.join(this.fields, ","));
}
builder.append(" from ").append(tableName);
List<Object> params = new ArrayList<>();
where.and(excludeInvalid, it -> where.ne(logicDeleteColumn, logicDeleteValue));
if (!where.isEmpty()) {
where.and();
builder.append(where.getSql());
params.addAll(where.getParams());
}
if (!orders.isEmpty()) {
builder.append(" order by ");
builder.append(String.join(",", orders));
}
if (!groups.isEmpty()) {
builder.append(" group by ");
builder.append(String.join(",", groups));
}
return new BoundSql(builder.toString(), params, sqlModule);
}
private BoundSql buildSelect(boolean excludeInvalid) {
StringBuilder builder = new StringBuilder();
builder.append("select ");
if (this.fields.isEmpty()) {
builder.append("*");
} else {
builder.append(StringUtils.join(this.fields, ","));
}
builder.append(" from ").append(tableName);
List<Object> params = new ArrayList<>();
where.and(excludeInvalid, it -> where.ne(logicDeleteColumn, logicDeleteValue));
if (!where.isEmpty()) {
where.and();
builder.append(where.getSql());
params.addAll(where.getParams());
}
if (!orders.isEmpty()) {
builder.append(" order by ");
builder.append(String.join(",", orders));
}
if (!groups.isEmpty()) {
builder.append(" group by ");
builder.append(String.join(",", groups));
}
return new BoundSql(builder.toString(), params, sqlModule);
}
@Comment("执行分页查询")
public Object page() {
return sqlModule.page(buildSelect());
}
@Comment("执行分页查询")
public Object page() {
return sqlModule.page(buildSelect());
}
@Comment("执行分页查询")
public Object page(@Comment("排除无效数据") boolean excludeInvalid) {
return sqlModule.page(buildSelect(excludeInvalid));
}
@Comment("执行分页查询")
public Object page(@Comment("排除无效数据") boolean excludeInvalid) {
return sqlModule.page(buildSelect(excludeInvalid));
}
@Comment("执行update语句")
public int update() {
return update(null);
}
@Comment("执行update语句")
public int update() {
return update(null);
}
@Comment("执行update语句")
public int update(@Comment("各项列和值") Map<String, Object> data, @Comment("是否更新空值字段") boolean isUpdateBlank) {
if (null != data) {
data.forEach((key, value) -> this.columns.put(rowMapColumnMapper.apply(key), value));
}
Object primaryValue = null;
if (StringUtils.isNotBlank(this.primary)) {
primaryValue = this.columns.remove(this.primary);
}
List<Map.Entry<String, Object>> entries = null;
if (!isUpdateBlank) {
entries = filterNotBlanks();
} else {
entries = new ArrayList<>(this.columns.entrySet());
}
@Comment("执行update语句")
public int update(@Comment("各项列和值") Map<String, Object> data, @Comment("是否更新空值字段") boolean isUpdateBlank) {
if (null != data) {
data.forEach((key, value) -> this.columns.put(rowMapColumnMapper.apply(key), value));
}
Object primaryValue = null;
if (StringUtils.isNotBlank(this.primary)) {
primaryValue = this.columns.remove(this.primary);
}
List<Map.Entry<String, Object>> entries = null;
if (!isUpdateBlank) {
entries = filterNotBlanks();
} else {
entries = new ArrayList<>(this.columns.entrySet());
}
if (entries.isEmpty()) {
throw new MagicAPIException("要修改的列不能为空");
}
StringBuilder builder = new StringBuilder();
builder.append("update ");
builder.append(tableName);
builder.append(" set ");
List<Object> params = new ArrayList<>();
for (int i = 0, size = entries.size(); i < size; i++) {
Map.Entry<String, Object> entry = entries.get(i);
builder.append(entry.getKey()).append(" = ?");
params.add(entry.getValue());
if (i + 1 < size) {
builder.append(",");
}
}
if (!where.isEmpty()) {
builder.append(where.getSql());
params.addAll(where.getParams());
} else if (primaryValue != null) {
builder.append(" where ").append(this.primary).append(" = ?");
params.add(primaryValue);
} else {
throw new MagicAPIException("主键值不能为空");
}
return sqlModule.update(new BoundSql(builder.toString(), params, sqlModule));
}
if (entries.isEmpty()) {
throw new MagicAPIException("要修改的列不能为空");
}
StringBuilder builder = new StringBuilder();
builder.append("update ");
builder.append(tableName);
builder.append(" set ");
List<Object> params = new ArrayList<>();
for (int i = 0, size = entries.size(); i < size; i++) {
Map.Entry<String, Object> entry = entries.get(i);
builder.append(entry.getKey()).append(" = ?");
params.add(entry.getValue());
if (i + 1 < size) {
builder.append(",");
}
}
if (!where.isEmpty()) {
builder.append(where.getSql());
params.addAll(where.getParams());
} else if (primaryValue != null) {
builder.append(" where ").append(this.primary).append(" = ?");
params.add(primaryValue);
} else {
throw new MagicAPIException("主键值不能为空");
}
return sqlModule.update(new BoundSql(builder.toString(), params, sqlModule));
}
@Comment("执行update语句")
public int update(@Comment("各项列和值") Map<String, Object> data) {
return update(data, false);
}
@Comment("执行update语句")
public int update(@Comment("各项列和值") Map<String, Object> data) {
return update(data, false);
}
}