From e446d9e574d778f6f4be316e2f89398075fa5ca1 Mon Sep 17 00:00:00 2001 From: mxlkjnsodihn Date: Wed, 21 Aug 2024 15:30:45 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E5=A2=9E=E5=BC=BACondition?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2=EF=BC=8C=E4=BC=98=E5=8C=96=E6=9F=A5=E8=AF=A2?= =?UTF-8?q?=E6=95=88=E7=8E=87=EF=BC=8C=E5=A2=9E=E5=8A=A0=E6=89=B9=E5=A4=84?= =?UTF-8?q?=E7=90=86=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hcframe-parent/hcframe-base/pom.xml | 2 +- .../base/module/data/dao/TableMapper.java | 2 + .../base/module/data/module/BaseMapper.java | 8 +- .../module/data/module/BaseMapperImpl.java | 265 +++++++++++++----- .../base/module/data/module/Condition.java | 89 ++++-- .../config/DataSourceConfiguration.java | 9 +- .../resources/mapping/data/TableMapper.xml | 74 +++-- 7 files changed, 315 insertions(+), 134 deletions(-) diff --git a/hcframe-parent/hcframe-base/pom.xml b/hcframe-parent/hcframe-base/pom.xml index bf28d6e..3daea12 100644 --- a/hcframe-parent/hcframe-base/pom.xml +++ b/hcframe-parent/hcframe-base/pom.xml @@ -10,7 +10,7 @@ 4.0.0 hcframe-base - 1.2.2-SNAPSHOT + 1.2.3-SNAPSHOT 8 diff --git a/hcframe-parent/hcframe-base/src/main/java/com/taixingyiji/base/module/data/dao/TableMapper.java b/hcframe-parent/hcframe-base/src/main/java/com/taixingyiji/base/module/data/dao/TableMapper.java index 07a6be3..1803a20 100644 --- a/hcframe-parent/hcframe-base/src/main/java/com/taixingyiji/base/module/data/dao/TableMapper.java +++ b/hcframe-parent/hcframe-base/src/main/java/com/taixingyiji/base/module/data/dao/TableMapper.java @@ -48,4 +48,6 @@ public interface TableMapper { Long count(@Param("tableName")String tableName,@Param("sql")String sql); Long countBySql(@Param("sql") String sql); + int insertBatch(@Param("list") List> list, @Param("tableName") String tableName); + int updateBatchByPk(@Param("sql")String sql); } diff --git a/hcframe-parent/hcframe-base/src/main/java/com/taixingyiji/base/module/data/module/BaseMapper.java b/hcframe-parent/hcframe-base/src/main/java/com/taixingyiji/base/module/data/module/BaseMapper.java index ba2b6fd..e7d74df 100644 --- a/hcframe-parent/hcframe-base/src/main/java/com/taixingyiji/base/module/data/module/BaseMapper.java +++ b/hcframe-parent/hcframe-base/src/main/java/com/taixingyiji/base/module/data/module/BaseMapper.java @@ -119,11 +119,17 @@ public interface BaseMapper { List> selectSql(String sql); PageInfo> selectSqlByPage(String sql, WebPageInfo webPageInfo); + List> selectSql(String sql,Map params); + + PageInfo> selectSqlByPage(String sql,Map params, WebPageInfo webPageInfo); Map selectOneSql(String sql); - + Map selectOneSql(String sql,Map params); Long count(String tableName, Condition condition); Long count(Condition condition); + int saveBatch(String tableName,String pkName, List> list); + + int updateBatchByPk(String tableName,String pkName,List> list); } diff --git a/hcframe-parent/hcframe-base/src/main/java/com/taixingyiji/base/module/data/module/BaseMapperImpl.java b/hcframe-parent/hcframe-base/src/main/java/com/taixingyiji/base/module/data/module/BaseMapperImpl.java index 2e232e1..7dcf9cb 100644 --- a/hcframe-parent/hcframe-base/src/main/java/com/taixingyiji/base/module/data/module/BaseMapperImpl.java +++ b/hcframe-parent/hcframe-base/src/main/java/com/taixingyiji/base/module/data/module/BaseMapperImpl.java @@ -1,6 +1,7 @@ package com.taixingyiji.base.module.data.module; import com.github.pagehelper.PageInfo; +import com.taixingyiji.base.common.ServiceException; import com.taixingyiji.base.common.WebPageInfo; import com.taixingyiji.base.common.utils.MyPageHelper; import com.taixingyiji.base.common.utils.StringUtils; @@ -12,9 +13,12 @@ import com.taixingyiji.base.module.datasource.entity.DatasourceConfig; import com.taixingyiji.base.module.datasource.utils.DataSourceUtil; import com.taixingyiji.base.module.datasource.utils.DataUnit; import com.taixingyiji.base.module.tableconfig.entity.OsSysTable; +import org.mybatis.spring.SqlSessionTemplate; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; +import java.util.Collections; +import java.util.HashMap; import java.util.List; import java.util.Map; @@ -22,18 +26,20 @@ import java.util.Map; public class BaseMapperImpl implements BaseMapper { public static final String BASE = "base"; - + public static final String TABLE_MAPPER_PACKAGE = "com.taixingyiji.base.module.data.dao.TableMapper."; final TableMapper tableMapper; @Value("${spring.datasource.druid.driver-class-name}") public String dataType; + final SqlSessionTemplate sqlSessionTemplate; - public BaseMapperImpl(TableMapper tableMapper) { + public BaseMapperImpl(TableMapper tableMapper, SqlSessionTemplate sqlSessionTemplate) { this.tableMapper = tableMapper; + this.sqlSessionTemplate = sqlSessionTemplate; } @Override - public int save(DataMap dataMap) { + public int save(DataMap dataMap) { String key; DatasourceConfig datasourceConfig = new DatasourceConfig(); try { @@ -103,7 +109,7 @@ public class BaseMapperImpl implements BaseMapper { pkName = "ID"; } int i; - if (datasourceConfig.getCommonType().equals(DataUnit.ORACLE)||datasourceConfig.getCommonType().equals(DataUnit.DAMENG)) { + if (datasourceConfig.getCommonType().equals(DataUnit.ORACLE) || datasourceConfig.getCommonType().equals(DataUnit.DAMENG)) { if (org.springframework.util.StringUtils.isEmpty(data.get(pkName))) { data.put(pkName, getSequence(tableName, pkName)); } @@ -120,10 +126,19 @@ public class BaseMapperImpl implements BaseMapper { public int save(E e) { DataMap dataMap = DataMap.builder().obj(e).build(); int i = save(dataMap); - dataMap.set(StringUtils.toCamelCase(dataMap.getPkName()),dataMap.getPkValue()); + dataMap.set(StringUtils.toCamelCase(dataMap.getPkName()), dataMap.getPkValue()); return i; } + private int updateByWhere(Condition condition, String tableName, Map data) { + Map params = condition.getParamMap(); + params.put("tableName", tableName); + params.put("info", data); + params.put("sql", condition.getSql()); + int i = sqlSessionTemplate.update(TABLE_MAPPER_PACKAGE + "updateByWhere", params); + SqlException.base(i, "更新失败"); + return i; + } @Override public int updateByPk(DataMap dataMap) { @@ -134,13 +149,11 @@ public class BaseMapperImpl implements BaseMapper { Condition condition = Condition.creatCriteria() .andEqual(dataMap.getPkName(), dataMap.getPkValue()) .build(); - int i = tableMapper.updateByWhere(dataMap.getData(), dataMap.getTableName(), condition.getSql()); - SqlException.base(i, "更新失败"); - return i; + return updateByWhere(condition, dataMap.getTableName(), dataMap.getData()); } @Override - public int updateByPk(String tableName, String pkName, Map data) { + public int updateByPk(String tableName, String pkName, Map data) { JudgesNull(data, "data can not be null!"); JudgesNull(tableName, "tableName can not be null!"); JudgesNull(pkName, "pkName can not be null!"); @@ -149,9 +162,7 @@ public class BaseMapperImpl implements BaseMapper { .andEqual(pkName, data.get(pkName)) .build(); data.remove(pkName); - int i = tableMapper.updateByWhere(data, tableName, condition.getSql()); - SqlException.base(i, "更新失败"); - return i; + return updateByWhere(condition, tableName, data); } @Override @@ -163,9 +174,7 @@ public class BaseMapperImpl implements BaseMapper { Condition condition = Condition.creatCriteria() .andEqual(osSysTable.getTablePk(), data.get(osSysTable.getTablePk())) .build(); - int i = tableMapper.updateByWhere(data, osSysTable.getTableName(), condition.getSql()); - SqlException.base(i, "更新失败"); - return i; + return updateByWhere(condition, osSysTable.getTableName(), data); } @Override @@ -179,9 +188,7 @@ public class BaseMapperImpl implements BaseMapper { Condition condition = Condition.creatCriteria() .andIn(dataMap.getPkName(), ids) .build(); - int i = tableMapper.updateByWhere(dataMap.getData(), dataMap.getTableName(), condition.getSql()); - SqlException.base(i, "更新失败"); - return i; + return updateByWhere(condition, dataMap.getTableName(), dataMap.getData()); } @@ -194,7 +201,7 @@ public class BaseMapperImpl implements BaseMapper { Condition condition = Condition.creatCriteria() .andIn(dataMap.getPkName(), dataMap.getIdList()) .build(); - return tableMapper.updateByWhere(dataMap.getData(), dataMap.getTableName(), condition.getSql()); + return updateByWhere(condition, dataMap.getTableName(), dataMap.getData()); } @Override @@ -206,9 +213,7 @@ public class BaseMapperImpl implements BaseMapper { Condition condition = Condition.creatCriteria() .andIn(pkName, ids) .build(); - int i = tableMapper.updateByWhere(data, tableName, condition.getSql()); - SqlException.base(i, "更新失败"); - return i; + return updateByWhere(condition, tableName, data); } @Override @@ -220,9 +225,7 @@ public class BaseMapperImpl implements BaseMapper { Condition condition = Condition.creatCriteria() .andIn(osSysTable.getTablePk(), ids) .build(); - int i = tableMapper.updateByWhere(data, osSysTable.getTableName(), condition.getSql()); - SqlException.base(i, "更新失败"); - return i; + return updateByWhere(condition, osSysTable.getTableName(), data); } @@ -230,16 +233,21 @@ public class BaseMapperImpl implements BaseMapper { public int updateByCondition(DataMap dataMap, Condition condition) { JudgesNull(dataMap.getData(), "data can not be null!"); JudgesNull(dataMap.getTableName(), "tableName can not be null!"); - int i = tableMapper.updateByWhere(dataMap.getData(), dataMap.getTableName(), condition.getSql()); - SqlException.base(i, "更新失败"); - return i; + return updateByWhere(condition, dataMap.getTableName(), dataMap.getData()); } @Override public int updateByCondition(String tableName, Map data, Condition condition) { JudgesNull(data, "data can not be null!"); JudgesNull(tableName, "tableName can not be null!"); - int i = tableMapper.updateByWhere(data, tableName, condition.getSql()); + return updateByWhere(condition, tableName, data); + } + + private int deleteByWhere(Condition condition, String tableName) { + Map params = condition.getParamMap(); + params.put("tableName", tableName); + params.put("sql", condition.getSql()); + int i = sqlSessionTemplate.delete(TABLE_MAPPER_PACKAGE + "deleteByWhere", params); SqlException.base(i, "更新失败"); return i; } @@ -252,8 +260,7 @@ public class BaseMapperImpl implements BaseMapper { Condition condition = Condition.creatCriteria() .andEqual(dataMap.getPkName(), dataMap.getPkValue()) .build(); - int i = tableMapper.deleteByWhere(dataMap.getTableName(), condition.getSql()); - return i; + return deleteByWhere(condition, dataMap.getTableName()); } @Override @@ -264,8 +271,7 @@ public class BaseMapperImpl implements BaseMapper { Condition condition = Condition.creatCriteria() .andEqual(pkName, pkValue) .build(); - int i = tableMapper.deleteByWhere(tableName, condition.getSql()); - return i; + return deleteByWhere(condition, tableName); } @Override @@ -276,8 +282,7 @@ public class BaseMapperImpl implements BaseMapper { Condition condition = Condition.creatCriteria() .andEqual(osSysTable.getTablePk(), pkValue) .build(); - int i = tableMapper.deleteByWhere(osSysTable.getTableName(), condition.getSql()); - return i; + return deleteByWhere(condition, osSysTable.getTableName()); } @Override @@ -288,8 +293,7 @@ public class BaseMapperImpl implements BaseMapper { Condition condition = Condition.creatCriteria() .andIn(pkName, ids) .build(); - int i = tableMapper.deleteByWhere(tableName, condition.getSql()); - return i; + return deleteByWhere(condition, tableName); } @Override @@ -300,8 +304,7 @@ public class BaseMapperImpl implements BaseMapper { Condition condition = Condition.creatCriteria() .andIn(dataMap.getPkName(), dataMap.getIdList()) .build(); - int i = tableMapper.deleteByWhere(dataMap.getTableName(), condition.getSql()); - return i; + return deleteByWhere(condition, dataMap.getTableName()); } @Override @@ -312,22 +315,19 @@ public class BaseMapperImpl implements BaseMapper { Condition condition = Condition.creatCriteria() .andIn(osSysTable.getTablePk(), ids) .build(); - int i = tableMapper.deleteByWhere(osSysTable.getTableName(), condition.getSql()); - return i; + return deleteByWhere(condition, osSysTable.getTableName()); } @Override public int deleteByCondition(DataMap dataMap, Condition condition) { JudgesNull(dataMap.getTableName(), "tableName can not be null!"); - int i = tableMapper.deleteByWhere(dataMap.getTableName(), condition.getSql()); - return i; + return deleteByWhere(condition, dataMap.getTableName()); } @Override public int deleteByCondition(String tableName, Condition condition) { JudgesNull(tableName, "tableName can not be null!"); - int i = tableMapper.deleteByWhere(tableName, condition.getSql()); - return i; + return deleteByWhere(condition, tableName); } @Override @@ -336,18 +336,30 @@ public class BaseMapperImpl implements BaseMapper { return tableMapper.useSql(SelectCondition.builder().tableName(tableName).build().getSql()); } + private List> selectList(Condition condition) { + Map params = condition.getParamMap(); + params.put("sql", condition.getSql()); + return sqlSessionTemplate.selectList(TABLE_MAPPER_PACKAGE + "useSql", params); + } + + private Map selectOne(Condition condition) { + Map params = condition.getParamMap(); + params.put("sql", condition.getSql()); + return sqlSessionTemplate.selectOne(TABLE_MAPPER_PACKAGE + "userSqlByOne", params); + } + @Override public List> selectByEqual(DataMap dataMap, Map map) { JudgesNull(dataMap.getTableName(), "tableName can not be null!"); Condition condition = equal(dataMap, map); - return tableMapper.useSql(condition.getSql()); + return selectList(condition); } @Override public List> selectByEqual(String tableName, Map map) { JudgesNull(tableName, "tableName can not be null!"); Condition condition = equal(DataMap.builder().tableName(tableName).build(), map); - return tableMapper.useSql(condition.getSql()); + return selectList(condition); } @@ -369,7 +381,7 @@ public class BaseMapperImpl implements BaseMapper { JudgesNull(dataMap.getTableName(), "tableName can not be null!"); MyPageHelper.start(webPageInfo); Condition condition = equal(dataMap, map); - return new PageInfo<>(tableMapper.useSql(condition.getSql())); + return new PageInfo<>(selectList(condition)); } @Override @@ -377,14 +389,14 @@ public class BaseMapperImpl implements BaseMapper { JudgesNull(tableName, "tableName can not be null!"); MyPageHelper.start(webPageInfo); Condition condition = equal(DataMap.builder().tableName(tableName).build(), map); - return new PageInfo<>(tableMapper.useSql(condition.getSql())); + return new PageInfo<>(selectList(condition)); } @Override public Map selectOneByEqual(DataMap dataMap, Map map) { JudgesNull(dataMap.getTableName(), "tableName can not be null!"); Condition condition = equal(dataMap, map); - return tableMapper.userSqlByOne(condition.getSql()); + return selectOne(condition); } @Override @@ -394,7 +406,7 @@ public class BaseMapperImpl implements BaseMapper { JudgesNull(dataMap.getPkValue(), "pkValue can not be null!"); Condition condition = Condition.creatCriteria(dataMap).build(); condition = condition.toCreatCriteria().andEqual(dataMap.getPkName(), dataMap.getPkValue()).build(); - return tableMapper.userSqlByOne(condition.getSql()); + return selectOne(condition); } @Override @@ -404,7 +416,7 @@ public class BaseMapperImpl implements BaseMapper { JudgesNull(pkValue, "pkValue can not be null!"); Condition condition = Condition.creatCriteria(DataMap.builder().sysOsTable(osSysTable).pkValue(pkValue).build()).build(); condition = condition.toCreatCriteria().andEqual(osSysTable.getTablePk(), pkValue).build(); - return tableMapper.userSqlByOne(condition.getSql()); + return selectOne(condition); } @Override @@ -414,48 +426,50 @@ public class BaseMapperImpl implements BaseMapper { JudgesNull(pkValue, "pkValue can not be null!"); Condition condition = Condition.creatCriteria(DataMap.builder().tableName(tableName).pkName(pkName).pkValue(pkValue).build()).build(); condition = condition.toCreatCriteria().andEqual(pkName, pkValue).build(); - return tableMapper.userSqlByOne(condition.getSql()); + return selectOne(condition); } @Override public List> selectByCondition(Condition condition) { - return tableMapper.useSql(condition.getSql()); + return selectList(condition); } @Override public List> selectByCondition(DataMap dataMap, Condition condition) { JudgesNull(dataMap.getTableName(), "tableName can not be null!"); - return tableMapper.useSql(condition.toCreatCriteria(dataMap).build().getSql()); + condition = condition.toCreatCriteria(dataMap).build(); + return selectList(condition); } @Override public List> selectByCondition(String tableName, Condition condition) { JudgesNull(tableName, "tableName can not be null!"); - return tableMapper.useSql(condition.toCreatCriteria(DataMap.builder().tableName(tableName).build()).build().getSql()); + condition = condition.toCreatCriteria(DataMap.builder().tableName(tableName).build()).build(); + return selectList(condition); } @Override public List> selectByCondition(String tableName, List fieldList, Condition condition) { JudgesNull(tableName, "tableName can not be null!"); - return tableMapper.useSql(condition + condition = condition .toCreatCriteria(DataMap.builder().tableName(tableName).fieldList(fieldList).build()) - .build() - .getSql()); + .build(); + return selectList(condition); } @Override public List> selectByCondition(String tableName, String fieldList, Condition condition) { JudgesNull(tableName, "tableName can not be null!"); - return tableMapper.useSql(condition + condition = condition .toCreatCriteria(DataMap.builder().tableName(tableName).fields(fieldList).build()) - .build() - .getSql()); + .build(); + return selectList(condition); } @Override public PageInfo> selectByCondition(Condition condition, WebPageInfo webPageInfo) { MyPageHelper.start(webPageInfo); - return new PageInfo<>(tableMapper.useSql(condition.getSql())); + return new PageInfo<>(selectList(condition)); } @Override @@ -463,7 +477,7 @@ public class BaseMapperImpl implements BaseMapper { JudgesNull(dataMap.getTableName(), "tableName can not be null!"); MyPageHelper.start(webPageInfo); condition = condition.toCreatCriteria(dataMap).build(); - return new PageInfo<>(tableMapper.useSql(condition.getSql())); + return new PageInfo<>(selectList(condition)); } @Override @@ -471,7 +485,7 @@ public class BaseMapperImpl implements BaseMapper { JudgesNull(tableName, "tableName can not be null!"); MyPageHelper.start(webPageInfo); condition = condition.toCreatCriteria(DataMap.builder().tableName(tableName).build()).build(); - return new PageInfo<>(tableMapper.useSql(condition.getSql())); + return new PageInfo<>(selectList(condition)); } @Override @@ -479,7 +493,7 @@ public class BaseMapperImpl implements BaseMapper { JudgesNull(tableName, "tableName can not be null!"); MyPageHelper.start(webPageInfo); condition = condition.toCreatCriteria(DataMap.builder().tableName(tableName).fieldList(fieldList).build()).build(); - return new PageInfo<>(tableMapper.useSql(condition.getSql())); + return new PageInfo<>(selectList(condition)); } @Override @@ -487,36 +501,40 @@ public class BaseMapperImpl implements BaseMapper { JudgesNull(tableName, "tableName can not be null!"); MyPageHelper.start(webPageInfo); condition = condition.toCreatCriteria(DataMap.builder().tableName(tableName).fields(fieldList).build()).build(); - return new PageInfo<>(tableMapper.useSql(condition.getSql())); + return new PageInfo<>(selectList(condition)); } @Override public Map selectOneByCondition(DataMap dataMap, Condition condition) { JudgesNull(dataMap.getTableName(), "tableName can not be null!"); - return tableMapper.userSqlByOne(condition.toCreatCriteria(dataMap).build().getSql()); + condition = condition.toCreatCriteria(dataMap).build(); + return selectOne(condition); } @Override public Map selectOneByCondition(String tableName, Condition condition) { JudgesNull(tableName, "tableName can not be null!"); - return tableMapper.userSqlByOne(condition.toCreatCriteria(DataMap.builder().tableName(tableName).build()).build().getSql()); + condition = condition.toCreatCriteria(DataMap.builder().tableName(tableName).build()).build(); + return selectOne(condition); } @Override public Map selectOneByCondition(String tableName, List fieldList, Condition condition) { JudgesNull(tableName, "tableName can not be null!"); - return tableMapper.userSqlByOne(condition.toCreatCriteria(DataMap.builder().tableName(tableName).fieldList(fieldList).build()).build().getSql()); + condition = condition.toCreatCriteria(DataMap.builder().tableName(tableName).fieldList(fieldList).build()).build(); + return selectOne(condition); } @Override public Map selectOneByCondition(String tableName, String fieldList, Condition condition) { JudgesNull(tableName, "tableName can not be null!"); - return tableMapper.userSqlByOne(condition.toCreatCriteria(DataMap.builder().tableName(tableName).fields(fieldList).build()).build().getSql()); + condition = condition.toCreatCriteria(DataMap.builder().tableName(tableName).fields(fieldList).build()).build(); + return selectOne(condition); } @Override public Map selectOneByCondition(Condition condition) { - return tableMapper.userSqlByOne(condition.getSql()); + return selectOne(condition); } @Override @@ -528,6 +546,8 @@ public class BaseMapperImpl implements BaseMapper { return tableMapper.useSql(condition.toCreatCriteria(selectJoinBuilder).build().getSql()); } + + @Override public List> selectJoinByCondition(String tableName, List joinCondition, Condition condition) { JudgesNull(tableName, "tableName can not be null!"); @@ -589,14 +609,37 @@ public class BaseMapperImpl implements BaseMapper { return new PageInfo<>(tableMapper.useSql(sql)); } + @Override + public List> selectSql(String sql, Map params) { + params.put("sql",sql); + return sqlSessionTemplate.selectList(TABLE_MAPPER_PACKAGE + "useSql", params); + } + + @Override + public PageInfo> selectSqlByPage(String sql, Map params, WebPageInfo webPageInfo) { + MyPageHelper.start(webPageInfo); + params.put("sql",sql); + return new PageInfo<>(sqlSessionTemplate.selectList(TABLE_MAPPER_PACKAGE + "useSql", params)); + } + @Override public Map selectOneSql(String sql) { return tableMapper.userSqlByOne(sql); } + @Override + public Map selectOneSql(String sql, Map params) { + params.put("sql",sql); + return sqlSessionTemplate.selectOne(TABLE_MAPPER_PACKAGE + "userSqlByOne", params); + } + @Override public Long count(String tableName, Condition condition) { - return tableMapper.count(tableName,condition.getSql()); + String sql = condition.getSql(); + sql = "SELECT count(0) as COUNT FROM "+tableName+" " + sql; + condition.setSql(sql); + Map map = selectOne(condition); + return Long.parseLong(String.valueOf(map.get("COUNT"))); } @Override @@ -604,6 +647,80 @@ public class BaseMapperImpl implements BaseMapper { return tableMapper.countBySql(condition.getSql()); } + @Override + public int saveBatch(String tableName, String pkName, List> list) { + JudgesNull(tableName, "tableName can not be null!"); +// JudgesNull(data, "tableName can not be null!"); + String key; + DatasourceConfig datasourceConfig = new DatasourceConfig(); + try { + key = DBContextHolder.getDataSource(); + datasourceConfig = DataSourceUtil.get(key); + } catch (Exception e) { + if (dataType.contains("oracle")) { + datasourceConfig.setCommonType(DataUnit.ORACLE); + } + if (dataType.contains("mysql")) { + datasourceConfig.setCommonType(DataUnit.MYSQL); + } + if (dataType.contains("DmDriver")) { + datasourceConfig.setCommonType(DataUnit.DAMENG); + } + if (dataType.contains("sqlite")) { + datasourceConfig.setCommonType(DataUnit.SQLITE); + } + } + if (StringUtils.isEmpty(pkName)) { + pkName = "ID"; + } + + if (datasourceConfig.getCommonType().equals(DataUnit.ORACLE) || datasourceConfig.getCommonType().equals(DataUnit.DAMENG)) { + for(Map map:list){ + Object id = getSequence(tableName, pkName); + map.put(pkName,id); + } + } + int i = tableMapper.insertBatch(list, tableName); + SqlException.base(i, "保存失败"); + return i; + } + + @Override + public int updateBatchByPk(String tableName, String pkName, List> list) { + StringBuilder sql = new StringBuilder(); + Map paramMap = new HashMap<>(); + int index = 0; + for (Map item : list) { + sql.append("UPDATE ").append(tableName).append(" SET "); + boolean hasSetClause = false; + for (Map.Entry entry : item.entrySet()) { + String key = entry.getKey(); + Object value = entry.getValue(); + if (!key.equals(pkName) && value != null && !(value instanceof String && ((String) value).isEmpty())) { + sql.append(key).append(" = #{item").append(index).append("_").append(key).append("}, "); + paramMap.put("item" + index + "_" + key, value); + hasSetClause = true; + } + } + if (hasSetClause) { + // 移除最后的逗号和空格 + sql.setLength(sql.length() - 2); + } else { + // 如果没有要更新的字段,跳过这条记录 + continue; + } + sql.append(" WHERE ").append(pkName).append(" = #{item").append(index).append("_").append(pkName).append("};"); + paramMap.put("item" + index + "_" + pkName, item.get(pkName)); + index++; + } + if (index == 0) { + // 如果没有任何记录需要更新,直接返回 + return 0; + } + paramMap.put("sql", sql.toString()); + return this.sqlSessionTemplate.update(TABLE_MAPPER_PACKAGE+"updateBatchByPk", paramMap); + } + public void JudgesNull(Object object, String str) { if (object == null) { throw new BaseMapperException(str); @@ -626,6 +743,6 @@ public class BaseMapperImpl implements BaseMapper { } id = tableMapper.getSequence(tableName); } - return Long.parseLong(id.toString())+1L; + return Long.parseLong(id.toString()) + 1L; } } diff --git a/hcframe-parent/hcframe-base/src/main/java/com/taixingyiji/base/module/data/module/Condition.java b/hcframe-parent/hcframe-base/src/main/java/com/taixingyiji/base/module/data/module/Condition.java index 3feae98..6e72b11 100644 --- a/hcframe-parent/hcframe-base/src/main/java/com/taixingyiji/base/module/data/module/Condition.java +++ b/hcframe-parent/hcframe-base/src/main/java/com/taixingyiji/base/module/data/module/Condition.java @@ -1,12 +1,17 @@ package com.taixingyiji.base.module.data.module; +import cn.hutool.core.util.IdUtil; import com.taixingyiji.base.common.ServiceException; import com.taixingyiji.base.common.utils.XssClass; import com.taixingyiji.base.common.utils.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; + import java.io.Serializable; +import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.List; +import java.util.Map; /** * @author lhc @@ -35,6 +40,8 @@ public class Condition implements Serializable { public static String LTE = "<="; public static String GTE = ">="; public static String GROUP_BY = "GROUP BY"; + private Map paramMap = new LinkedHashMap<>(); + private String sql = ""; @@ -47,10 +54,11 @@ public class Condition implements Serializable { public Condition() { } - public Condition(String sql,SelectCondition selectCondition) { + public Condition(String sql, SelectCondition selectCondition, Map paramMap) { this.selectCondition = selectCondition; this.sql = sql; String[] sqlArr; + this.paramMap = paramMap; if (sql.contains(" WHERE ")) { sqlArr = sql.split(WHERE + " " + OneEq); this.selecSql = sqlArr[0]; @@ -64,6 +72,10 @@ public class Condition implements Serializable { } } + public Map getParamMap() { + return paramMap; + } + public SelectCondition getSelectCondition() { return selectCondition; } @@ -107,7 +119,7 @@ public class Condition implements Serializable { } public static ConditionBuilder creatCriteria(SelectCondition selectCondition) { - return new ConditionBuilder(selectCondition); + return new ConditionBuilder(selectCondition,new HashMap<>()); } public static ConditionBuilder creatCriteria(SelectCondition selectCondition, boolean flag) { @@ -116,7 +128,7 @@ public class Condition implements Serializable { public static ConditionBuilder creatCriteria(DataMap dataMap) { JudesNull(dataMap.getTableName(), "tableName can not be null!"); - return new ConditionBuilder(dataMap.getSelectCondition()); + return new ConditionBuilder(dataMap.getSelectCondition(),new HashMap<>()); } public static ConditionBuilder creatCriteria(DataMap dataMap, boolean flag) { @@ -135,14 +147,15 @@ public class Condition implements Serializable { } public ConditionBuilder toCreatCriteria() { - return new ConditionBuilder(this.sql); + return new ConditionBuilder(this.sql,this.paramMap); } + public ConditionBuilder toCreatCriteria(SelectCondition selectCondition) { - return new ConditionBuilder(selectCondition,this.sql); + return new ConditionBuilder(selectCondition, this.sql,this.paramMap); } public ConditionBuilder toCreatCriteria(DataMap dataMap) { - return new ConditionBuilder(dataMap.getSelectCondition(),this.sql); + return new ConditionBuilder(dataMap.getSelectCondition(), this.sql,this.paramMap); } public static class ConditionBuilder { @@ -156,6 +169,7 @@ public class Condition implements Serializable { private int lrn = 0; private int rrn = 0; + private Map paramMap = new LinkedHashMap<>(); // 是否开启防注入,默认开启 private boolean flag = true; @@ -185,9 +199,10 @@ public class Condition implements Serializable { this.flag = flag; } - public ConditionBuilder(SelectCondition selectCondition) { + public ConditionBuilder(SelectCondition selectCondition,Map paramMap) { this.selectCondition = selectCondition; this.selecSql = selectCondition.getSql(); + this.paramMap = paramMap; } public ConditionBuilder(SelectCondition selectCondition, boolean flag) { @@ -196,10 +211,11 @@ public class Condition implements Serializable { this.selecSql = selectCondition.getSql(); } - public ConditionBuilder(SelectCondition selectCondition,String sql) { + public ConditionBuilder(SelectCondition selectCondition, String sql,Map paramMap) { spliteSql(sql); this.selectCondition = selectCondition; this.selecSql = selectCondition.getSql(); + this.paramMap = paramMap; } private void spliteSql(String sql) { @@ -217,15 +233,16 @@ public class Condition implements Serializable { } } - public ConditionBuilder(SelectCondition selectCondition,String sql,boolean flag) { + public ConditionBuilder(SelectCondition selectCondition, String sql, boolean flag) { this.flag = flag; spliteSql(sql); this.selectCondition = selectCondition; this.selecSql = selectCondition.getSql(); } - public ConditionBuilder(String sql) { + public ConditionBuilder(String sql,Map paramMap) { this.sql = sql; + this.paramMap = paramMap; spliteSql(sql); } @@ -252,11 +269,12 @@ public class Condition implements Serializable { } public ConditionBuilder equal(String key, Object value) { - sqlCheck(value); if (value.toString().contains("\"")) { value = value.toString().replaceAll("\"", ""); } - this.conditionSql += " " + key + EQUAL + "'" + value + "'"; + String sqlKey = "item_" + IdUtil.fastUUID(); + this.conditionSql += " " + key + EQUAL + "#{" + sqlKey + "}"; + this.paramMap.put(sqlKey, value); return this; } @@ -291,8 +309,9 @@ public class Condition implements Serializable { StringBuilder inStr = new StringBuilder(L_CURVES); int i = 1; for (Object object : value) { - sqlCheck(object); - inStr.append("'").append(object.toString()).append("'"); + String sqlKey = "item_" + IdUtil.fastUUID(); + this.paramMap.put(sqlKey, value); + inStr.append("#{").append(sqlKey).append("}"); if (i != value.size()) { inStr.append(","); } @@ -314,9 +333,11 @@ public class Condition implements Serializable { } public ConditionBuilder between(String key, Object start, Object end) { - sqlCheck(start); - sqlCheck(end); - this.conditionSql += " " + key + " " + BETWEEN + " '" + start.toString() + "' " + AND + " '" + end.toString() + "' "; + String sqlKey = "item_" + IdUtil.fastUUID(); + String sqlKey2 = "item_" + IdUtil.fastUUID(); + this.paramMap.put(sqlKey, start); + this.paramMap.put(sqlKey2, end); + this.conditionSql += " " + key + " " + BETWEEN + " #{" + sqlKey + "} " + AND + " #{" + sqlKey2 + "} "; return this; } @@ -331,8 +352,9 @@ public class Condition implements Serializable { } public ConditionBuilder lt(String key, Object value) { - sqlCheck(value); - this.conditionSql += " " + key + " " + LT + " '" + value.toString() + "' "; + String sqlKey = "item_" + IdUtil.fastUUID(); + this.paramMap.put(sqlKey, value); + this.conditionSql += " " + key + " " + LT + " #{" + sqlKey + "} "; return this; } @@ -350,9 +372,11 @@ public class Condition implements Serializable { this.conditionSql += " " + NOT + " "; return this; } + public ConditionBuilder notEqual(String key, Object value) { - sqlCheck(value); - this.conditionSql += " " + key + " " + NOT_EQUAL + " '" + value.toString() + "' "; + String sqlKey = "item_" + IdUtil.fastUUID(); + this.paramMap.put(sqlKey, value); + this.conditionSql += " " + key + " " + NOT_EQUAL + " #{" + sqlKey + "} "; return this; } @@ -367,8 +391,9 @@ public class Condition implements Serializable { } public ConditionBuilder gt(String key, Object value) { - sqlCheck(value); - this.conditionSql += " " + key + " " + GT + " '" + value.toString() + "' "; + String sqlKey = "item_" + IdUtil.fastUUID(); + this.paramMap.put(sqlKey, value); + this.conditionSql += " " + key + " " + GT + " #{" + sqlKey + "} "; return this; } @@ -383,8 +408,9 @@ public class Condition implements Serializable { } public ConditionBuilder lte(String key, Object value) { - sqlCheck(value); - this.conditionSql += " " + key + " " + LTE + " '" + value.toString() + "' "; + String sqlKey = "item_" + IdUtil.fastUUID(); + this.paramMap.put(sqlKey, value); + this.conditionSql += " " + key + " " + LTE + " #{" + value.toString() + "} "; return this; } @@ -399,8 +425,9 @@ public class Condition implements Serializable { } public ConditionBuilder gte(String key, Object value) { - sqlCheck(value); - this.conditionSql += " " + key + " " + GTE + " '" + value.toString() + "' "; + String sqlKey = "item_" + IdUtil.fastUUID(); + this.paramMap.put(sqlKey, value); + this.conditionSql += " " + key + " " + GTE + " #{" + value.toString() + "} "; return this; } @@ -414,7 +441,7 @@ public class Condition implements Serializable { return this; } - public ConditionBuilder groupBy(String ...str) { + public ConditionBuilder groupBy(String... str) { int i = 1; StringBuilder stringBuilder = new StringBuilder(" " + GROUP_BY + " "); for (String s : str) { @@ -434,7 +461,7 @@ public class Condition implements Serializable { } if (this.flag) { if (XssClass.sqlInj(obj.toString())) { - logger.error("非法字符:"+obj.toString()); + logger.error("非法字符:" + obj.toString()); throw new ServiceException("value中含有非法字符,有注入风险!"); } } @@ -446,7 +473,7 @@ public class Condition implements Serializable { } if (this.flag) { if (XssClass.sqlInjLike(obj.toString())) { - logger.error("非法字符:"+obj.toString()); + logger.error("非法字符:" + obj.toString()); throw new ServiceException("value中含有非法字符,有注入风险!"); } } @@ -465,7 +492,7 @@ public class Condition implements Serializable { if (StringUtils.isEmpty(this.conditionSql)) { this.sql = this.selecSql; } - return new Condition(this.sql,this.selectCondition); + return new Condition(this.sql, this.selectCondition,this.paramMap); } } diff --git a/hcframe-parent/hcframe-base/src/main/java/com/taixingyiji/base/module/datasource/config/DataSourceConfiguration.java b/hcframe-parent/hcframe-base/src/main/java/com/taixingyiji/base/module/datasource/config/DataSourceConfiguration.java index f4495de..910a58c 100644 --- a/hcframe-parent/hcframe-base/src/main/java/com/taixingyiji/base/module/datasource/config/DataSourceConfiguration.java +++ b/hcframe-parent/hcframe-base/src/main/java/com/taixingyiji/base/module/datasource/config/DataSourceConfiguration.java @@ -3,7 +3,9 @@ package com.taixingyiji.base.module.datasource.config; import com.alibaba.druid.pool.DruidDataSource; import com.taixingyiji.base.common.MyMapper; import com.taixingyiji.base.module.datasource.dynamic.MyDynamicDataSource; +import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; +import org.mybatis.spring.SqlSessionTemplate; import org.mybatis.spring.annotation.MapperScan; import org.mybatis.spring.boot.autoconfigure.MybatisProperties; import org.slf4j.Logger; @@ -74,7 +76,7 @@ public class DataSourceConfiguration { @Bean(value = "sqlSessionFactoryBean222") public SqlSessionFactoryBean sqlSessionFactoryBean() throws IOException { SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); - sqlSessionFactoryBean.setTypeAliasesPackage("com.common.**.entity"); + sqlSessionFactoryBean.setTypeAliasesPackage("com.**.**.entity"); if (!ObjectUtils.isEmpty(this.properties.resolveMapperLocations())) { sqlSessionFactoryBean.setMapperLocations(this.properties.resolveMapperLocations()); } @@ -89,4 +91,9 @@ public class DataSourceConfiguration { public PlatformTransactionManager transactionManager() { return new DataSourceTransactionManager(dynamicDataSource()); } + @Bean + public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) { + return new SqlSessionTemplate(sqlSessionFactory); + } + } diff --git a/hcframe-parent/hcframe-base/src/main/resources/mapping/data/TableMapper.xml b/hcframe-parent/hcframe-base/src/main/resources/mapping/data/TableMapper.xml index 3734857..e8e0982 100644 --- a/hcframe-parent/hcframe-base/src/main/resources/mapping/data/TableMapper.xml +++ b/hcframe-parent/hcframe-base/src/main/resources/mapping/data/TableMapper.xml @@ -5,6 +5,10 @@ select * FROM ${tableName} + + + DELETE + FROM ${tableName} ${sql} + + + + - - - + + + + + CREATE + SEQUENCE ${tableName}_seq + MINVALUE 1 + NOMAXVALUE + START + WITH ${lastId} + INCREMENT BY 1 + NOCACHE + + + INSERT INTO ${tableName} + + + ${column} + + + VALUES + + + + #{value}, + + + + + + ${sql} +