进一步完善 JdbcEntityDaoSupport 和 AssertResult 的 API,使构建 Repository 的实现更容易。

pull/1/head
ZhouXY108 2023-02-09 00:35:17 +08:00
parent d01db60309
commit cec72ba7f9
6 changed files with 61 additions and 29 deletions

View File

@ -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) { public static void exist(boolean expression) {
if (!expression) { if (!expression) {
throw new DataNotExistException(); throw new DataNotExistException();

View File

@ -10,6 +10,7 @@ import javax.annotation.Nonnull;
import org.springframework.jdbc.core.ResultSetExtractor; import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.RowMapper; 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.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource; import org.springframework.jdbc.core.namedparam.SqlParameterSource;
@ -35,6 +36,10 @@ public abstract class JdbcEntityDaoSupport<T extends Entity<ID>, ID extends Seri
return this.jdbc.query(sql, paramSource, this.resultSetExtractor); 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<T> queryForList(String sql) { protected final List<T> queryForList(String sql) {
return this.jdbc.query(sql, this.rowMapper); return this.jdbc.query(sql, this.rowMapper);
} }
@ -43,19 +48,36 @@ public abstract class JdbcEntityDaoSupport<T extends Entity<ID>, ID extends Seri
return this.jdbc.query(sql, parameterSource, this.rowMapper); return this.jdbc.query(sql, parameterSource, this.rowMapper);
} }
protected final List<T> queryForList(String sql, String paramName, Object value) {
return this.jdbc.query(sql, new MapSqlParameterSource(paramName, value), this.rowMapper);
}
protected final Stream<T> queryForStream(String sql, SqlParameterSource parameterSource) { protected final Stream<T> queryForStream(String sql, SqlParameterSource parameterSource) {
return this.jdbc.queryForStream(sql, parameterSource, this.rowMapper); return this.jdbc.queryForStream(sql, parameterSource, this.rowMapper);
} }
protected final Stream<T> queryForStream(String sql, String paramName, Object value) {
return this.jdbc.queryForStream(sql, new MapSqlParameterSource(paramName, value), this.rowMapper);
}
protected final <E> Stream<E> queryForStream(String sql, SqlParameterSource parameterSource, Class<E> elementType) { protected final <E> Stream<E> queryForStream(String sql, SqlParameterSource parameterSource, Class<E> elementType) {
return this.jdbc.queryForList(sql, parameterSource, elementType).stream(); return this.jdbc.queryForList(sql, parameterSource, elementType).stream();
} }
protected final <E> Stream<E> queryForStream(String sql, String paramName, Object value, Class<E> elementType) {
return this.jdbc.queryForList(sql, new MapSqlParameterSource(paramName, value), elementType).stream();
}
protected final boolean queryExists(String sql, SqlParameterSource parameterSource) { protected final boolean queryExists(String sql, SqlParameterSource parameterSource) {
Boolean isExists = this.jdbc.query(sql, parameterSource, ResultSet::next); Boolean isExists = this.jdbc.query(sql, parameterSource, ResultSet::next);
return Boolean.TRUE.equals(isExists); 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) { protected final int update(String sql, SqlParameterSource parameterSource) {
return this.jdbc.update(sql, parameterSource); return this.jdbc.update(sql, parameterSource);
} }

View File

@ -41,7 +41,7 @@ public class AccountRepositoryImpl extends JdbcRepositorySupport<Account, Long>
new MapSqlParameterSource() new MapSqlParameterSource()
.addValue("id", entity.getId().orElseThrow()) .addValue("id", entity.getId().orElseThrow())
.addValue("version", entity.getVersion())); .addValue("version", entity.getVersion()));
AssertResult.update(i, 1); AssertResult.updateOneRow(i);
} }
@Override @Override
@ -53,7 +53,7 @@ public class AccountRepositoryImpl extends JdbcRepositorySupport<Account, Long>
FROM sys_account FROM sys_account
WHERE id = :id AND deleted = 0 WHERE id = :id AND deleted = 0
""", """,
new MapSqlParameterSource("id", id)); "id", id);
} }
@Override @Override
@ -65,7 +65,7 @@ public class AccountRepositoryImpl extends JdbcRepositorySupport<Account, Long>
FROM sys_account FROM sys_account
WHERE email = :email AND deleted = 0 WHERE email = :email AND deleted = 0
""", """,
new MapSqlParameterSource("email", email.value())); "email", email.value());
} }
@Override @Override
@ -77,7 +77,7 @@ public class AccountRepositoryImpl extends JdbcRepositorySupport<Account, Long>
FROM sys_account FROM sys_account
WHERE mobile_phone = :mobilePhone AND deleted = 0 WHERE mobile_phone = :mobilePhone AND deleted = 0
""", """,
new MapSqlParameterSource("mobilePhone", mobilePhone.value())); "mobilePhone", mobilePhone.value());
} }
@Override @Override
@ -89,31 +89,31 @@ public class AccountRepositoryImpl extends JdbcRepositorySupport<Account, Long>
FROM sys_account FROM sys_account
WHERE username = :username AND deleted = 0 WHERE username = :username AND deleted = 0
""", """,
new MapSqlParameterSource("username", username.value())); "username", username.value());
} }
@Override @Override
public boolean exists(Long id) { public boolean exists(Long id) {
return queryExists("SELECT 1 FROM sys_account WHERE id = :id AND deleted = 0 LIMIT 1", return queryExists("SELECT 1 FROM sys_account WHERE id = :id AND deleted = 0 LIMIT 1",
new MapSqlParameterSource("id", id)); "id", id);
} }
@Override @Override
public boolean existsUsername(Username username) { public boolean existsUsername(Username username) {
return queryExists("SELECT 1 FROM sys_account WHERE username = :username AND deleted = 0 LIMIT 1", return queryExists("SELECT 1 FROM sys_account WHERE username = :username AND deleted = 0 LIMIT 1",
new MapSqlParameterSource("username", username.value())); "username", username.value());
} }
@Override @Override
public boolean existsEmail(Email email) { public boolean existsEmail(Email email) {
return queryExists("SELECT 1 FROM sys_account WHERE email = :email AND deleted = 0 LIMIT 1", return queryExists("SELECT 1 FROM sys_account WHERE email = :email AND deleted = 0 LIMIT 1",
new MapSqlParameterSource("email", email.value())); "email", email.value());
} }
@Override @Override
public boolean existsMobilePhone(MobilePhone mobilePhone) { public boolean existsMobilePhone(MobilePhone mobilePhone) {
return queryExists("SELECT 1 FROM sys_account WHERE mobile_phone = :mobile_phone AND deleted = 0 LIMIT 1", 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 @Override
@ -127,7 +127,7 @@ public class AccountRepositoryImpl extends JdbcRepositorySupport<Account, Long>
LEFT JOIN sys_account_role ar ON a.id = ar.account_id LEFT JOIN sys_account_role ar ON a.id = ar.account_id
WHERE ar.role_id = :roleId AND a.deleted = 0 WHERE ar.role_id = :roleId AND a.deleted = 0
""", """,
new MapSqlParameterSource("roleId", roleId)); "roleId", roleId);
} }
@Override @Override
@ -142,7 +142,7 @@ public class AccountRepositoryImpl extends JdbcRepositorySupport<Account, Long>
:createdBy, :createTime) :createdBy, :createTime)
""", """,
generateParamSource(id, entity)); generateParamSource(id, entity));
AssertResult.update(i, 1); AssertResult.updateOneRow(i);
this.accountRoleDAO.insertAccountRoleRefs(id, entity.getRoleIds()); this.accountRoleDAO.insertAccountRoleRefs(id, entity.getRoleIds());
return entity; return entity;
} }
@ -166,7 +166,7 @@ public class AccountRepositoryImpl extends JdbcRepositorySupport<Account, Long>
WHERE id = :id AND deleted = 0 AND "version" = :version WHERE id = :id AND deleted = 0 AND "version" = :version
""", """,
generateParamSource(entity)); generateParamSource(entity));
AssertResult.update(i, 1); AssertResult.updateOneRow(i);
this.accountRoleDAO.saveAccountRoleRefs(entity); this.accountRoleDAO.saveAccountRoleRefs(entity);
return entity; return entity;
} }

View File

@ -36,7 +36,7 @@ public class DictRepositoryImpl extends JdbcRepositorySupport<Dict, Long> implem
@Override @Override
public Dict doFindById(@Nonnull Long id) { public Dict doFindById(@Nonnull Long id) {
return queryForObject("SELECT id, dict_type, dict_label, \"version\" WHERE id = :id AND deleted = 0", return queryForObject("SELECT id, dict_type, dict_label, \"version\" WHERE id = :id AND deleted = 0",
new MapSqlParameterSource("id", id)); "id", id);
} }
@Override @Override
@ -47,7 +47,7 @@ public class DictRepositoryImpl extends JdbcRepositorySupport<Dict, Long> implem
VALUES (:dictType, :dictLabel, :createTime, :createdBy) VALUES (:dictType, :dictLabel, :createTime, :createdBy)
""", """,
generateParamSource(id, entity)); generateParamSource(id, entity));
AssertResult.update(i, 1); AssertResult.updateOneRow(i);
this.dictValueDAO.insertDictValues(id, entity); this.dictValueDAO.insertDictValues(id, entity);
return find(id); return find(id);
} }
@ -64,7 +64,7 @@ public class DictRepositoryImpl extends JdbcRepositorySupport<Dict, Long> implem
WHERE id = :id AND deleted = 0 AND "version" = :version WHERE id = :id AND deleted = 0 AND "version" = :version
""", """,
generateParamSource(entity)); generateParamSource(entity));
AssertResult.update(i, 1); AssertResult.updateOneRow(i);
this.dictValueDAO.updateDictValues(entity); this.dictValueDAO.updateDictValues(entity);
return find(entity.getId().orElseThrow()); return find(entity.getId().orElseThrow());
} }
@ -76,7 +76,7 @@ public class DictRepositoryImpl extends JdbcRepositorySupport<Dict, Long> implem
WHERE id = :id AND deleted = 0 AND "version" = :version WHERE id = :id AND deleted = 0 AND "version" = :version
""", """,
generateParamSource(entity)); generateParamSource(entity));
AssertResult.update(i, 1); AssertResult.updateOneRow(i);
} }
@Override @Override

View File

@ -46,7 +46,7 @@ public class MenuRepositoryImpl extends JdbcRepositorySupport<Menu, Long> implem
FROM sys_menu FROM sys_menu
WHERE id = :id AND deleted = 0 WHERE id = :id AND deleted = 0
""", """,
new MapSqlParameterSource("id", id)); "id", id);
} }
@Override @Override
@ -62,7 +62,7 @@ public class MenuRepositoryImpl extends JdbcRepositorySupport<Menu, Long> implem
"""; """;
MapSqlParameterSource paramSource = generateParamSource(id, entity); MapSqlParameterSource paramSource = generateParamSource(id, entity);
int i = update(sql, paramSource); int i = update(sql, paramSource);
AssertResult.update(i, 1); AssertResult.updateOneRow(i);
this.actionDAO.saveActions(id, entity.getActions()); this.actionDAO.saveActions(id, entity.getActions());
return entity; return entity;
} }
@ -92,7 +92,7 @@ public class MenuRepositoryImpl extends JdbcRepositorySupport<Menu, Long> implem
// 更新菜单 // 更新菜单
int i = update(sql, generateParamSource(entity)); int i = update(sql, generateParamSource(entity));
AssertResult.update(i, 1); AssertResult.updateOneRow(i);
// 保存权限 // 保存权限
Long id = entity.getId().orElseThrow(); Long id = entity.getId().orElseThrow();
@ -108,13 +108,13 @@ public class MenuRepositoryImpl extends JdbcRepositorySupport<Menu, Long> implem
""", """,
new MapSqlParameterSource("id", entity.getId().orElseThrow()) new MapSqlParameterSource("id", entity.getId().orElseThrow())
.addValue("version", entity.getVersion())); .addValue("version", entity.getVersion()));
AssertResult.update(i, 1); AssertResult.updateOneRow(i);
} }
@Override @Override
public boolean exists(Long id) { public boolean exists(Long id) {
return queryExists("SELECT 1 FROM sys_menu WHERE id = :id AND deleted = 0 LIMIT 1", return queryExists("SELECT 1 FROM sys_menu WHERE id = :id AND deleted = 0 LIMIT 1",
new MapSqlParameterSource("id", id)); "id", id);
} }
@Override @Override
@ -129,7 +129,7 @@ public class MenuRepositoryImpl extends JdbcRepositorySupport<Menu, Long> implem
FROM sys_menu FROM sys_menu
WHERE id IN (:ids) AND deleted = 0 WHERE id IN (:ids) AND deleted = 0
""", """,
new MapSqlParameterSource("ids", ids)); "ids", ids);
} }
@Override @Override
@ -147,7 +147,7 @@ public class MenuRepositoryImpl extends JdbcRepositorySupport<Menu, Long> implem
LEFT JOIN sys_role_menu AS rm ON m.id = rm.menu_id LEFT JOIN sys_role_menu AS rm ON m.id = rm.menu_id
WHERE rm.role_id = :roleId AND r.deleted = 0 WHERE rm.role_id = :roleId AND r.deleted = 0
""", """,
new MapSqlParameterSource("roleId", roleId)); "roleId", roleId);
} }
@Override @Override

View File

@ -41,7 +41,8 @@ public class RoleRepositoryImpl extends JdbcRepositorySupport<Role, Long> implem
SELECT "id","name","identifier","status","remarks","version" SELECT "id","name","identifier","status","remarks","version"
FROM "sys_role" FROM "sys_role"
WHERE id = :id AND deleted = 0 WHERE id = :id AND deleted = 0
""", new MapSqlParameterSource("id", id)); """,
"id", id);
} }
@Override @Override
@ -52,13 +53,13 @@ public class RoleRepositoryImpl extends JdbcRepositorySupport<Role, Long> implem
""", """,
new MapSqlParameterSource("id", entity.getId().orElseThrow()) new MapSqlParameterSource("id", entity.getId().orElseThrow())
.addValue("version", entity.getVersion())); .addValue("version", entity.getVersion()));
AssertResult.update(i, 1); AssertResult.updateOneRow(i);
} }
@Override @Override
public boolean exists(Long id) { public boolean exists(Long id) {
return queryExists("SELECT 1 FROM sys_role WHERE id = :id AND deleted = 0 LIMIT 1", return queryExists("SELECT 1 FROM sys_role WHERE id = :id AND deleted = 0 LIMIT 1",
new MapSqlParameterSource("id", id)); "id", id);
} }
@Override @Override
@ -70,7 +71,7 @@ public class RoleRepositoryImpl extends JdbcRepositorySupport<Role, Long> implem
VALUES VALUES
(:id, :name, :identifier, :status, :remarks, :createTime, :createdBy) (:id, :name, :identifier, :status, :remarks, :createTime, :createdBy)
""", generateParamSource(id, entity)); """, generateParamSource(id, entity));
AssertResult.update(i, 1); AssertResult.updateOneRow(i);
this.roleMenuRefDAO.saveRoleMenuRefs(id, entity); this.roleMenuRefDAO.saveRoleMenuRefs(id, entity);
this.rolePermissionRefDAO.saveRolePermissionRefs(id, entity); this.rolePermissionRefDAO.saveRolePermissionRefs(id, entity);
return entity; return entity;
@ -89,7 +90,7 @@ public class RoleRepositoryImpl extends JdbcRepositorySupport<Role, Long> implem
"version" = "version" + 1 "version" = "version" + 1
WHERE id = :id AND deleted = 0 AND "version" = :version WHERE id = :id AND deleted = 0 AND "version" = :version
""", generateParamSource(entity)); """, generateParamSource(entity));
AssertResult.update(i, 1); AssertResult.updateOneRow(i);
Long id = entity.getId().orElseThrow(); Long id = entity.getId().orElseThrow();
this.roleMenuRefDAO.clearRoleMenuRefs(entity); this.roleMenuRefDAO.clearRoleMenuRefs(entity);
@ -106,7 +107,8 @@ public class RoleRepositoryImpl extends JdbcRepositorySupport<Role, Long> implem
FROM sys_role AS r FROM sys_role AS r
LEFT JOIN sys_account_role AS ar ON r.id = ar.role_id LEFT JOIN sys_account_role AS ar ON r.id = ar.role_id
WHERE ar.account_id = :accountId AND r.deleted = 0 WHERE ar.account_id = :accountId AND r.deleted = 0
""", new MapSqlParameterSource("accountId", accountId)); """,
"accountId", accountId);
} }
@Override @Override