diff --git a/README.md b/README.md index f3e23e5..2b84db2 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ veasion-db 是一个轻量级持久层db框架,除slf4j-api外不依赖任何 cn.veasion veasion-db - 1.1.6 + 1.1.7 ``` 支持sql解析生成veasion-db代码 diff --git a/pom.xml b/pom.xml index ec69a89..982829f 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ cn.veasion veasion-db - 1.1.6 + 1.1.7 veasion-db https://github.com/veasion/veasion-db diff --git a/src/main/java/cn/veasion/db/jdbc/JdbcDao.java b/src/main/java/cn/veasion/db/jdbc/JdbcDao.java index f5fb14d..a6bec36 100644 --- a/src/main/java/cn/veasion/db/jdbc/JdbcDao.java +++ b/src/main/java/cn/veasion/db/jdbc/JdbcDao.java @@ -75,6 +75,24 @@ public static Object[] executeInsert(Connection connection, String sql, Object.. return keys.toArray(); } + /** + * 执行新增 + */ + public static int executeInsertNoKeys(Connection connection, String sql, Object... params) { + int count; + PreparedStatement ps = null; + try { + ps = prepareStatement(connection, sql, params); + count = ps.executeUpdate(); + LOGGER.info("<== Updates: {}", count); + } catch (SQLException e) { + throw new DbException("新增异常", e); + } finally { + closeAll(ps, null); + } + return count; + } + /** * 列表查询 * @@ -272,11 +290,12 @@ public static Object queryOnly(Connection connection, String sql, Object... para } private static PreparedStatement prepareStatement(Connection connection, String sql, Object... params) throws SQLException { - return prepareStatement(connection, Statement.NO_GENERATED_KEYS, sql, params); + // Statement.NO_GENERATED_KEYS + return prepareStatement(connection, null, sql, params); } - private static PreparedStatement prepareStatement(Connection connection, int autoGeneratedKeys, String sql, Object... params) throws SQLException { - PreparedStatement ps = connection.prepareStatement(sql, autoGeneratedKeys); + private static PreparedStatement prepareStatement(Connection connection, Integer autoGeneratedKeys, String sql, Object... params) throws SQLException { + PreparedStatement ps = autoGeneratedKeys == null ? connection.prepareStatement(sql) : connection.prepareStatement(sql, autoGeneratedKeys); if (params != null && params.length > 0) { for (int i = 0; i < params.length; i++) { ps.setObject(i + 1, params[i]); diff --git a/src/main/java/cn/veasion/db/jdbc/JdbcEntityDao.java b/src/main/java/cn/veasion/db/jdbc/JdbcEntityDao.java index 4e6d12a..44d784a 100644 --- a/src/main/java/cn/veasion/db/jdbc/JdbcEntityDao.java +++ b/src/main/java/cn/veasion/db/jdbc/JdbcEntityDao.java @@ -54,7 +54,13 @@ public ID add(EntityInsert entityInsert) { return InterceptorUtils.intercept(new EntityDaoInvocation<>(this, "add", new Object[]{entityInsert}, () -> { InsertSQL insertSQL = entityInsert.sqlValue(); return executeJdbc(JdbcTypeEnum.INSERT, connection -> { - Object[] objects = JdbcDao.executeInsert(connection, insertSQL.getSQL(), insertSQL.getValues()); + Object[] objects; + if (entityInsert.isUseGeneratedKeys()) { + objects = JdbcDao.executeInsert(connection, insertSQL.getSQL(), insertSQL.getValues()); + } else { + JdbcDao.executeInsertNoKeys(connection, insertSQL.getSQL(), insertSQL.getValues()); + objects = new Object[0]; + } if (objects.length > 0) { ID id = (ID) TypeUtils.convert(objects[0], idField.getType()); if (entity instanceof IBaseId) { @@ -80,7 +86,13 @@ public ID[] batchAdd(BatchEntityInsert batchEntityInsert) { Field idField = FieldUtils.getIdField(getEntityClass()); List entityList = batchEntityInsert.getEntityList(); return executeJdbc(JdbcTypeEnum.INSERT, connection -> { - Object[] objects = JdbcDao.executeInsert(connection, insertSQL.getSQL(), insertSQL.getValues()); + Object[] objects; + if (batchEntityInsert.isUseGeneratedKeys()) { + objects = JdbcDao.executeInsert(connection, insertSQL.getSQL(), insertSQL.getValues()); + } else { + JdbcDao.executeInsertNoKeys(connection, insertSQL.getSQL(), insertSQL.getValues()); + objects = new Object[0]; + } if (idField == null) { return null; } diff --git a/src/main/java/cn/veasion/db/update/BatchEntityInsert.java b/src/main/java/cn/veasion/db/update/BatchEntityInsert.java index 5d35e9b..9240fac 100644 --- a/src/main/java/cn/veasion/db/update/BatchEntityInsert.java +++ b/src/main/java/cn/veasion/db/update/BatchEntityInsert.java @@ -25,6 +25,7 @@ public class BatchEntityInsert { private List entityList; private Class entityClass; private Set skipFields; + private boolean useGeneratedKeys = true; private AbstractQuery insertSelectQuery; private List> fieldValueMapList; @@ -43,6 +44,15 @@ public BatchEntityInsert(AbstractQuery insertSelectQuery) { this.insertSelectQuery = Objects.requireNonNull(insertSelectQuery); } + public BatchEntityInsert setUseGeneratedKeys(boolean useGeneratedKeys) { + this.useGeneratedKeys = useGeneratedKeys; + return this; + } + + public boolean isUseGeneratedKeys() { + return useGeneratedKeys; + } + public List getEntityList() { return entityList; } diff --git a/src/main/java/cn/veasion/db/update/EntityInsert.java b/src/main/java/cn/veasion/db/update/EntityInsert.java index 818eaff..7760f2f 100644 --- a/src/main/java/cn/veasion/db/update/EntityInsert.java +++ b/src/main/java/cn/veasion/db/update/EntityInsert.java @@ -21,6 +21,7 @@ public class EntityInsert { private Object entity; private Class entityClass; private Set skipFields; + private boolean useGeneratedKeys = true; private Map fieldValueMap; public EntityInsert(Object entity) { @@ -42,6 +43,15 @@ public Map getFieldValueMap() { return fieldValueMap; } + public EntityInsert setUseGeneratedKeys(boolean useGeneratedKeys) { + this.useGeneratedKeys = useGeneratedKeys; + return this; + } + + public boolean isUseGeneratedKeys() { + return useGeneratedKeys; + } + public Class getEntityClass() { return entityClass; }