diff --git a/plusone-basic/plusone-basic-common/src/main/java/xyz/zhouxy/plusone/util/AssertResult.java b/plusone-basic/plusone-basic-common/src/main/java/xyz/zhouxy/plusone/util/AssertResult.java index 0e66185..298f204 100644 --- a/plusone-basic/plusone-basic-common/src/main/java/xyz/zhouxy/plusone/util/AssertResult.java +++ b/plusone-basic/plusone-basic-common/src/main/java/xyz/zhouxy/plusone/util/AssertResult.java @@ -41,6 +41,14 @@ public final class AssertResult { } } + public static void updateOneRow(int i) { + update(i, 1); + } + + public static void updateOneRow(Object i, String format) { + update(i, 1, format); + } + public static void exist(boolean expression) { if (!expression) { throw new DataNotExistException(); diff --git a/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/jdbc/JdbcEntityDaoSupport.java b/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/jdbc/JdbcEntityDaoSupport.java index 99e0ba9..93b886f 100644 --- a/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/jdbc/JdbcEntityDaoSupport.java +++ b/plusone-basic/plusone-basic-infrastructure/src/main/java/xyz/zhouxy/plusone/jdbc/JdbcEntityDaoSupport.java @@ -10,6 +10,7 @@ import javax.annotation.Nonnull; import org.springframework.jdbc.core.ResultSetExtractor; import org.springframework.jdbc.core.RowMapper; +import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; import org.springframework.jdbc.core.namedparam.SqlParameterSource; @@ -35,6 +36,10 @@ public abstract class JdbcEntityDaoSupport, ID extends Seri return this.jdbc.query(sql, paramSource, this.resultSetExtractor); } + protected final T queryForObject(String sql, String paramName, Object value) { + return this.jdbc.query(sql, new MapSqlParameterSource(paramName, value), this.resultSetExtractor); + } + protected final List queryForList(String sql) { return this.jdbc.query(sql, this.rowMapper); } @@ -43,19 +48,36 @@ public abstract class JdbcEntityDaoSupport, ID extends Seri return this.jdbc.query(sql, parameterSource, this.rowMapper); } + protected final List queryForList(String sql, String paramName, Object value) { + return this.jdbc.query(sql, new MapSqlParameterSource(paramName, value), this.rowMapper); + } + protected final Stream queryForStream(String sql, SqlParameterSource parameterSource) { return this.jdbc.queryForStream(sql, parameterSource, this.rowMapper); } + protected final Stream queryForStream(String sql, String paramName, Object value) { + return this.jdbc.queryForStream(sql, new MapSqlParameterSource(paramName, value), this.rowMapper); + } + protected final Stream queryForStream(String sql, SqlParameterSource parameterSource, Class elementType) { return this.jdbc.queryForList(sql, parameterSource, elementType).stream(); } + protected final Stream queryForStream(String sql, String paramName, Object value, Class elementType) { + return this.jdbc.queryForList(sql, new MapSqlParameterSource(paramName, value), elementType).stream(); + } + protected final boolean queryExists(String sql, SqlParameterSource parameterSource) { Boolean isExists = this.jdbc.query(sql, parameterSource, ResultSet::next); return Boolean.TRUE.equals(isExists); } + protected final boolean queryExists(String sql, String paramName, Object value) { + Boolean isExists = this.jdbc.query(sql, new MapSqlParameterSource(paramName, value), ResultSet::next); + return Boolean.TRUE.equals(isExists); + } + protected final int update(String sql, SqlParameterSource parameterSource) { return this.jdbc.update(sql, parameterSource); } diff --git a/plusone-system/plusone-system-infrastructure/src/main/java/xyz/zhouxy/plusone/system/domain/model/account/AccountRepositoryImpl.java b/plusone-system/plusone-system-infrastructure/src/main/java/xyz/zhouxy/plusone/system/domain/model/account/AccountRepositoryImpl.java index e72e69b..8b39d29 100644 --- a/plusone-system/plusone-system-infrastructure/src/main/java/xyz/zhouxy/plusone/system/domain/model/account/AccountRepositoryImpl.java +++ b/plusone-system/plusone-system-infrastructure/src/main/java/xyz/zhouxy/plusone/system/domain/model/account/AccountRepositoryImpl.java @@ -41,7 +41,7 @@ public class AccountRepositoryImpl extends JdbcRepositorySupport new MapSqlParameterSource() .addValue("id", entity.getId().orElseThrow()) .addValue("version", entity.getVersion())); - AssertResult.update(i, 1); + AssertResult.updateOneRow(i); } @Override @@ -53,7 +53,7 @@ public class AccountRepositoryImpl extends JdbcRepositorySupport FROM sys_account WHERE id = :id AND deleted = 0 """, - new MapSqlParameterSource("id", id)); + "id", id); } @Override @@ -65,7 +65,7 @@ public class AccountRepositoryImpl extends JdbcRepositorySupport FROM sys_account WHERE email = :email AND deleted = 0 """, - new MapSqlParameterSource("email", email.value())); + "email", email.value()); } @Override @@ -77,7 +77,7 @@ public class AccountRepositoryImpl extends JdbcRepositorySupport FROM sys_account WHERE mobile_phone = :mobilePhone AND deleted = 0 """, - new MapSqlParameterSource("mobilePhone", mobilePhone.value())); + "mobilePhone", mobilePhone.value()); } @Override @@ -89,31 +89,31 @@ public class AccountRepositoryImpl extends JdbcRepositorySupport FROM sys_account WHERE username = :username AND deleted = 0 """, - new MapSqlParameterSource("username", username.value())); + "username", username.value()); } @Override public boolean exists(Long id) { return queryExists("SELECT 1 FROM sys_account WHERE id = :id AND deleted = 0 LIMIT 1", - new MapSqlParameterSource("id", id)); + "id", id); } @Override public boolean existsUsername(Username username) { return queryExists("SELECT 1 FROM sys_account WHERE username = :username AND deleted = 0 LIMIT 1", - new MapSqlParameterSource("username", username.value())); + "username", username.value()); } @Override public boolean existsEmail(Email email) { return queryExists("SELECT 1 FROM sys_account WHERE email = :email AND deleted = 0 LIMIT 1", - new MapSqlParameterSource("email", email.value())); + "email", email.value()); } @Override public boolean existsMobilePhone(MobilePhone mobilePhone) { return queryExists("SELECT 1 FROM sys_account WHERE mobile_phone = :mobile_phone AND deleted = 0 LIMIT 1", - new MapSqlParameterSource("mobile_phone", mobilePhone.value())); + "mobile_phone", mobilePhone.value()); } @Override @@ -127,7 +127,7 @@ public class AccountRepositoryImpl extends JdbcRepositorySupport LEFT JOIN sys_account_role ar ON a.id = ar.account_id WHERE ar.role_id = :roleId AND a.deleted = 0 """, - new MapSqlParameterSource("roleId", roleId)); + "roleId", roleId); } @Override @@ -142,7 +142,7 @@ public class AccountRepositoryImpl extends JdbcRepositorySupport :createdBy, :createTime) """, generateParamSource(id, entity)); - AssertResult.update(i, 1); + AssertResult.updateOneRow(i); this.accountRoleDAO.insertAccountRoleRefs(id, entity.getRoleIds()); return entity; } @@ -166,7 +166,7 @@ public class AccountRepositoryImpl extends JdbcRepositorySupport WHERE id = :id AND deleted = 0 AND "version" = :version """, generateParamSource(entity)); - AssertResult.update(i, 1); + AssertResult.updateOneRow(i); this.accountRoleDAO.saveAccountRoleRefs(entity); return entity; } diff --git a/plusone-system/plusone-system-infrastructure/src/main/java/xyz/zhouxy/plusone/system/domain/model/dict/DictRepositoryImpl.java b/plusone-system/plusone-system-infrastructure/src/main/java/xyz/zhouxy/plusone/system/domain/model/dict/DictRepositoryImpl.java index 23a79a6..98e5fe3 100644 --- a/plusone-system/plusone-system-infrastructure/src/main/java/xyz/zhouxy/plusone/system/domain/model/dict/DictRepositoryImpl.java +++ b/plusone-system/plusone-system-infrastructure/src/main/java/xyz/zhouxy/plusone/system/domain/model/dict/DictRepositoryImpl.java @@ -36,7 +36,7 @@ public class DictRepositoryImpl extends JdbcRepositorySupport implem @Override public Dict doFindById(@Nonnull Long id) { return queryForObject("SELECT id, dict_type, dict_label, \"version\" WHERE id = :id AND deleted = 0", - new MapSqlParameterSource("id", id)); + "id", id); } @Override @@ -47,7 +47,7 @@ public class DictRepositoryImpl extends JdbcRepositorySupport implem VALUES (:dictType, :dictLabel, :createTime, :createdBy) """, generateParamSource(id, entity)); - AssertResult.update(i, 1); + AssertResult.updateOneRow(i); this.dictValueDAO.insertDictValues(id, entity); return find(id); } @@ -64,7 +64,7 @@ public class DictRepositoryImpl extends JdbcRepositorySupport implem WHERE id = :id AND deleted = 0 AND "version" = :version """, generateParamSource(entity)); - AssertResult.update(i, 1); + AssertResult.updateOneRow(i); this.dictValueDAO.updateDictValues(entity); return find(entity.getId().orElseThrow()); } @@ -76,7 +76,7 @@ public class DictRepositoryImpl extends JdbcRepositorySupport implem WHERE id = :id AND deleted = 0 AND "version" = :version """, generateParamSource(entity)); - AssertResult.update(i, 1); + AssertResult.updateOneRow(i); } @Override diff --git a/plusone-system/plusone-system-infrastructure/src/main/java/xyz/zhouxy/plusone/system/domain/model/menu/MenuRepositoryImpl.java b/plusone-system/plusone-system-infrastructure/src/main/java/xyz/zhouxy/plusone/system/domain/model/menu/MenuRepositoryImpl.java index a7d6886..895bd49 100644 --- a/plusone-system/plusone-system-infrastructure/src/main/java/xyz/zhouxy/plusone/system/domain/model/menu/MenuRepositoryImpl.java +++ b/plusone-system/plusone-system-infrastructure/src/main/java/xyz/zhouxy/plusone/system/domain/model/menu/MenuRepositoryImpl.java @@ -46,7 +46,7 @@ public class MenuRepositoryImpl extends JdbcRepositorySupport implem FROM sys_menu WHERE id = :id AND deleted = 0 """, - new MapSqlParameterSource("id", id)); + "id", id); } @Override @@ -62,7 +62,7 @@ public class MenuRepositoryImpl extends JdbcRepositorySupport implem """; MapSqlParameterSource paramSource = generateParamSource(id, entity); int i = update(sql, paramSource); - AssertResult.update(i, 1); + AssertResult.updateOneRow(i); this.actionDAO.saveActions(id, entity.getActions()); return entity; } @@ -92,7 +92,7 @@ public class MenuRepositoryImpl extends JdbcRepositorySupport implem // 更新菜单 int i = update(sql, generateParamSource(entity)); - AssertResult.update(i, 1); + AssertResult.updateOneRow(i); // 保存权限 Long id = entity.getId().orElseThrow(); @@ -108,13 +108,13 @@ public class MenuRepositoryImpl extends JdbcRepositorySupport implem """, new MapSqlParameterSource("id", entity.getId().orElseThrow()) .addValue("version", entity.getVersion())); - AssertResult.update(i, 1); + AssertResult.updateOneRow(i); } @Override public boolean exists(Long id) { return queryExists("SELECT 1 FROM sys_menu WHERE id = :id AND deleted = 0 LIMIT 1", - new MapSqlParameterSource("id", id)); + "id", id); } @Override @@ -129,7 +129,7 @@ public class MenuRepositoryImpl extends JdbcRepositorySupport implem FROM sys_menu WHERE id IN (:ids) AND deleted = 0 """, - new MapSqlParameterSource("ids", ids)); + "ids", ids); } @Override @@ -147,7 +147,7 @@ public class MenuRepositoryImpl extends JdbcRepositorySupport implem LEFT JOIN sys_role_menu AS rm ON m.id = rm.menu_id WHERE rm.role_id = :roleId AND r.deleted = 0 """, - new MapSqlParameterSource("roleId", roleId)); + "roleId", roleId); } @Override diff --git a/plusone-system/plusone-system-infrastructure/src/main/java/xyz/zhouxy/plusone/system/domain/model/role/RoleRepositoryImpl.java b/plusone-system/plusone-system-infrastructure/src/main/java/xyz/zhouxy/plusone/system/domain/model/role/RoleRepositoryImpl.java index 9f97cc4..8bf24ac 100644 --- a/plusone-system/plusone-system-infrastructure/src/main/java/xyz/zhouxy/plusone/system/domain/model/role/RoleRepositoryImpl.java +++ b/plusone-system/plusone-system-infrastructure/src/main/java/xyz/zhouxy/plusone/system/domain/model/role/RoleRepositoryImpl.java @@ -41,7 +41,8 @@ public class RoleRepositoryImpl extends JdbcRepositorySupport implem SELECT "id","name","identifier","status","remarks","version" FROM "sys_role" WHERE id = :id AND deleted = 0 - """, new MapSqlParameterSource("id", id)); + """, + "id", id); } @Override @@ -52,13 +53,13 @@ public class RoleRepositoryImpl extends JdbcRepositorySupport implem """, new MapSqlParameterSource("id", entity.getId().orElseThrow()) .addValue("version", entity.getVersion())); - AssertResult.update(i, 1); + AssertResult.updateOneRow(i); } @Override public boolean exists(Long id) { return queryExists("SELECT 1 FROM sys_role WHERE id = :id AND deleted = 0 LIMIT 1", - new MapSqlParameterSource("id", id)); + "id", id); } @Override @@ -70,7 +71,7 @@ public class RoleRepositoryImpl extends JdbcRepositorySupport implem VALUES (:id, :name, :identifier, :status, :remarks, :createTime, :createdBy) """, generateParamSource(id, entity)); - AssertResult.update(i, 1); + AssertResult.updateOneRow(i); this.roleMenuRefDAO.saveRoleMenuRefs(id, entity); this.rolePermissionRefDAO.saveRolePermissionRefs(id, entity); return entity; @@ -89,7 +90,7 @@ public class RoleRepositoryImpl extends JdbcRepositorySupport implem "version" = "version" + 1 WHERE id = :id AND deleted = 0 AND "version" = :version """, generateParamSource(entity)); - AssertResult.update(i, 1); + AssertResult.updateOneRow(i); Long id = entity.getId().orElseThrow(); this.roleMenuRefDAO.clearRoleMenuRefs(entity); @@ -106,7 +107,8 @@ public class RoleRepositoryImpl extends JdbcRepositorySupport implem FROM sys_role AS r LEFT JOIN sys_account_role AS ar ON r.id = ar.role_id WHERE ar.account_id = :accountId AND r.deleted = 0 - """, new MapSqlParameterSource("accountId", accountId)); + """, + "accountId", accountId); } @Override