重构 DAO 基础代码。

This commit is contained in:
2023-02-11 20:34:16 +08:00
parent 960a4d4ef3
commit cbc2711540
11 changed files with 292 additions and 182 deletions

View File

@@ -15,7 +15,6 @@ import org.springframework.stereotype.Repository;
import cn.hutool.core.util.IdUtil;
import xyz.zhouxy.plusone.jdbc.JdbcRepositorySupport;
import xyz.zhouxy.plusone.util.AssertResult;
/**
* AccountRepository 实现类
@@ -41,7 +40,7 @@ public class AccountRepositoryImpl extends JdbcRepositorySupport<Account, Long>
new MapSqlParameterSource()
.addValue("id", entity.getId().orElseThrow())
.addValue("version", entity.getVersion()));
AssertResult.updateOneRow(i);
assertUpdateOneRow(i);
}
@Override
@@ -142,7 +141,7 @@ public class AccountRepositoryImpl extends JdbcRepositorySupport<Account, Long>
:createdBy, :createTime)
""",
generateParamSource(id, entity));
AssertResult.updateOneRow(i);
assertUpdateOneRow(i);
this.accountRoleDAO.insertAccountRoleRefs(id, entity.getRoleIds());
return entity;
}
@@ -166,7 +165,7 @@ public class AccountRepositoryImpl extends JdbcRepositorySupport<Account, Long>
WHERE id = :id AND deleted = 0 AND "version" = :version
""",
generateParamSource(entity));
AssertResult.updateOneRow(i);
assertUpdateOneRow(i);
this.accountRoleDAO.saveAccountRoleRefs(entity);
return entity;
}

View File

@@ -1,47 +1,43 @@
package xyz.zhouxy.plusone.system.domain.model.account;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import javax.annotation.Nonnull;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import xyz.zhouxy.plusone.util.AssertResult;
import xyz.zhouxy.plusone.util.NumberUtil;
import xyz.zhouxy.plusone.jdbc.PlusoneJdbcDaoSupport;
class AccountRoleRefDAO {
private final NamedParameterJdbcTemplate jdbc;
class AccountRoleRefDAO extends PlusoneJdbcDaoSupport {
AccountRoleRefDAO(NamedParameterJdbcTemplate jdbc) {
this.jdbc = jdbc;
AccountRoleRefDAO(@Nonnull NamedParameterJdbcTemplate jdbc) {
super(jdbc);
}
Set<Long> selectRoleIdsByAccountId(Long accountId) {
List<Long> roleRefs = this.jdbc.queryForList("""
return queryForStream("""
SELECT r.id FROM sys_role r RIGHT JOIN sys_account_role ar ON r.id = ar.role_id
WHERE r.deleted = 0 AND ar.account_id = :accountId;
""",
new MapSqlParameterSource("accountId", accountId),
Long.TYPE);
return new HashSet<>(roleRefs);
"accountId", accountId,
Long.TYPE)
.collect(Collectors.toSet());
}
void clearAccountRoleRefs(Account entity) {
var param = new MapSqlParameterSource("accountId", entity.getId().orElseThrow());
this.jdbc.update("DELETE FROM sys_account_role WHERE account_id = :accountId", param);
update("DELETE FROM sys_account_role WHERE account_id = :accountId", "accountId", entity.getId().orElseThrow());
}
void insertAccountRoleRefs(Long accountId, Set<Long> roleRefs) {
String sql = "INSERT INTO sys_account_role (account_id, role_id) VALUES (:accountId, :roleId)";
MapSqlParameterSource[] batchArgs = roleRefs
.stream()
.map((Long roleId) -> new MapSqlParameterSource()
int i = batchUpdate(
"INSERT INTO sys_account_role (account_id, role_id) VALUES (:accountId, :roleId)",
roleRefs,
(Long roleId) -> new MapSqlParameterSource()
.addValue("accountId", accountId)
.addValue("roleId", roleId))
.toArray(MapSqlParameterSource[]::new);
int[] i = this.jdbc.batchUpdate(sql, batchArgs);
AssertResult.update(roleRefs.size(), NumberUtil.sum(i));
.addValue("roleId", roleId));
assertResultEquals(i, roleRefs.size());
}
void saveAccountRoleRefs(Account entity) {

View File

@@ -4,48 +4,45 @@ import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import javax.annotation.Nonnull;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.util.CollectionUtils;
import xyz.zhouxy.plusone.util.AssertResult;
import xyz.zhouxy.plusone.util.NumberUtil;
import xyz.zhouxy.plusone.jdbc.PlusoneJdbcDaoSupport;
class DictValueDAO {
private final NamedParameterJdbcTemplate jdbc;
class DictValueDAO extends PlusoneJdbcDaoSupport {
DictValueDAO(NamedParameterJdbcTemplate jdbc) {
this.jdbc = jdbc;
DictValueDAO(@Nonnull NamedParameterJdbcTemplate jdbc) {
super(jdbc);
}
void updateDictValues(Dict entity) {
MapSqlParameterSource deleteParam = new MapSqlParameterSource("dictType", entity.getId().orElseThrow());
this.jdbc.update("DELETE FROM sys_dict_value WHERE dict_type = :dictType", deleteParam);
update("DELETE FROM sys_dict_value WHERE dict_type = :dictType",
"dictType", entity.getId().orElseThrow());
int i = insertDictValues(entity.getId().orElseThrow(), entity);
AssertResult.update(i, entity.count());
assertResultEquals(i, entity.count());
}
int insertDictValues(Long dictId, Dict entity) {
if (Objects.isNull(dictId) || Objects.isNull(entity) || CollectionUtils.isEmpty(entity.getValues())) {
return 0;
}
int[] i = this.jdbc.batchUpdate(
return batchUpdate(
"INSERT INTO sys_dict_value (dict_type, dict_key, label) VALUES (:dictType, :dictKey, :label)",
entity.getValues().stream()
.map(dictValue -> new MapSqlParameterSource()
.addValue("dictType", dictId)
.addValue("dictKey", dictValue.getKey())
.addValue("label", dictValue.getLabel()))
.toArray(SqlParameterSource[]::new));
return NumberUtil.sum(i);
entity.getValues(),
dictValue -> new MapSqlParameterSource()
.addValue("dictType", dictId)
.addValue("dictKey", dictValue.getKey())
.addValue("label", dictValue.getLabel()));
}
Set<DictValue> selectDictValuesByDictId(long id) {
return this.jdbc.queryForStream("""
SELECT dict_key, label FROM sys_dict_value WHERE dict_type = :dictType
""", new MapSqlParameterSource("dictType", id),
(rs, rowNum) -> DictValue.of(rs.getInt("dict_key"), rs.getString("label")))
return queryForStream(
"SELECT dict_key, label FROM sys_dict_value WHERE dict_type = :dictType",
"dictType", id,
(rs, i) -> DictValue.of(rs.getInt("dict_key"), rs.getString("label")))
.collect(Collectors.toSet());
}
}

View File

@@ -39,15 +39,14 @@ class ActionDAO extends JdbcEntityDaoSupport<Action, Long> {
.map(action -> action.getId().orElseThrow())
.collect(Collectors.toSet());
if (!ids.isEmpty()) {
this.jdbc.update(
"UPDATE sys_action SET deleted = id WHERE resource = :resource AND id NOT IN (:ids) AND deleted = 0",
update("UPDATE sys_action SET deleted = id WHERE resource = :resource AND id NOT IN (:ids) AND deleted = 0",
new MapSqlParameterSource()
.addValue("resource", menuId)
.addValue("ids", ids));
}
// 更新存在的数据
this.jdbc.batchUpdate("""
batchUpdate("""
UPDATE sys_action
SET resource = :resource,
identifier = :identifier,
@@ -56,22 +55,18 @@ class ActionDAO extends JdbcEntityDaoSupport<Action, Long> {
updated_by = :updatedBy
WHERE id = :id AND deleted = 0
""",
actions.stream()
.filter(action -> action.getId().isPresent())
.map(action -> generateParamSource(menuId, action))
.toArray(MapSqlParameterSource[]::new));
actions.stream().filter(action -> action.getId().isPresent()),
action -> generateParamSource(menuId, action));
// 插入新添加的数据
this.jdbc.batchUpdate("""
batchUpdate("""
INSERT INTO sys_action
(id, resource, identifier, "label", create_time, created_by)
VALUES
(:id, :resource, :identifier, :label, :createTime, :createdBy)
""",
actions.stream()
.filter(action -> action.getId().isEmpty())
.map(action -> generateParamSource(menuId, IdUtil.getSnowflakeNextId(), action))
.toArray(MapSqlParameterSource[]::new));
actions.stream().filter(action -> action.getId().isEmpty()),
action -> generateParamSource(menuId, IdUtil.getSnowflakeNextId(), action));
}
List<Action> selectActionsByMenuId(long menuId) {
@@ -80,7 +75,8 @@ class ActionDAO extends JdbcEntityDaoSupport<Action, Long> {
FROM sys_action a
JOIN (SELECT id, resource FROM sys_menu WHERE id = :menuId AND deleted = 0) m ON a.resource = m.id
WHERE a.deleted = 0
""", new MapSqlParameterSource("menuId", menuId));
""",
"menuId", menuId);
}
Collection<Action> selectActionsByIdIn(Collection<Long> actionIds) {
@@ -92,7 +88,8 @@ class ActionDAO extends JdbcEntityDaoSupport<Action, Long> {
FROM sys_action a
LEFT JOIN sys_menu m ON a.resource = m.id
WHERE a.id IN (:actionIds) AND a.deleted = 0
""", new MapSqlParameterSource("actionIds", actionIds));
""",
"actionIds", actionIds);
}
private SqlParameterSource generateParamSource(Long menuId, Action action) {

View File

@@ -3,43 +3,38 @@ package xyz.zhouxy.plusone.system.domain.model.role;
import java.util.Set;
import java.util.stream.Collectors;
import javax.annotation.Nonnull;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import xyz.zhouxy.plusone.util.AssertResult;
import xyz.zhouxy.plusone.util.NumberUtil;
import xyz.zhouxy.plusone.jdbc.PlusoneJdbcDaoSupport;
class RoleMenuRefDAO {
class RoleMenuRefDAO extends PlusoneJdbcDaoSupport {
private final NamedParameterJdbcTemplate jdbc;
public RoleMenuRefDAO(NamedParameterJdbcTemplate jdbc) {
this.jdbc = jdbc;
public RoleMenuRefDAO(@Nonnull NamedParameterJdbcTemplate jdbc) {
super(jdbc);
}
Set<MenuRef> selectMenuRefsByRoleId(long roleId) {
return this.jdbc.queryForList("SELECT menu_id FROM sys_role_menu WHERE role_id = :roleId",
new MapSqlParameterSource("roleId", roleId),
return queryForStream("SELECT menu_id FROM sys_role_menu WHERE role_id = :roleId",
"roleId", roleId,
Long.TYPE)
.stream()
.map(MenuRef::of)
.collect(Collectors.toSet());
}
void clearRoleMenuRefs(Role entity) {
MapSqlParameterSource param = new MapSqlParameterSource("roleId", entity.getId().orElseThrow());
this.jdbc.update("DELETE FROM sys_role_menu WHERE role_id = :roleId", param);
update("DELETE FROM sys_role_menu WHERE role_id = :roleId", "roleId", entity.getId().orElseThrow());
}
void saveRoleMenuRefs(Long roleId, Role entity) {
String sql = "INSERT INTO sys_role_menu(role_id, menu_id) VALUES (:roleId, :menuId)";
MapSqlParameterSource[] batchArgs = entity.getMenus()
.stream()
.map(menuRef -> new MapSqlParameterSource()
int i = batchUpdate(
"INSERT INTO sys_role_menu(role_id, menu_id) VALUES (:roleId, :menuId)",
entity.getMenus(),
menuRef -> new MapSqlParameterSource()
.addValue("roleId", roleId)
.addValue("menuId", menuRef.menuId()))
.toArray(MapSqlParameterSource[]::new);
int[] i = this.jdbc.batchUpdate(sql, batchArgs);
AssertResult.update(entity.getMenus().size(), NumberUtil.sum(i));
.addValue("menuId", menuRef.menuId()));
assertResultEquals(i, entity.getMenus().size());
}
}

View File

@@ -3,43 +3,39 @@ package xyz.zhouxy.plusone.system.domain.model.role;
import java.util.Set;
import java.util.stream.Collectors;
import javax.annotation.Nonnull;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import xyz.zhouxy.plusone.util.AssertResult;
import xyz.zhouxy.plusone.util.NumberUtil;
import xyz.zhouxy.plusone.jdbc.PlusoneJdbcDaoSupport;
class RolePermissionRefDAO {
private final NamedParameterJdbcTemplate jdbc;
class RolePermissionRefDAO extends PlusoneJdbcDaoSupport {
public RolePermissionRefDAO(NamedParameterJdbcTemplate jdbc) {
this.jdbc = jdbc;
protected RolePermissionRefDAO(@Nonnull NamedParameterJdbcTemplate jdbc) {
super(jdbc);
}
Set<ActionRef> selectPermissionRefsByRoleId(long roleId) {
return this.jdbc.queryForList("SELECT permission_id FROM sys_role_permission WHERE role_id = :roleId",
new MapSqlParameterSource("roleId", roleId),
return queryForStream("SELECT permission_id FROM sys_role_permission WHERE role_id = :roleId",
"roleId", roleId,
Long.TYPE)
.stream()
.map(ActionRef::of)
.collect(Collectors.toSet());
}
void clearRolePermissionRefs(Role entity) {
MapSqlParameterSource param = new MapSqlParameterSource("roleId", entity.getId().orElseThrow());
this.jdbc.update("DELETE FROM sys_role_permission WHERE role_id = :roleId", param);
update("DELETE FROM sys_role_permission WHERE role_id = :roleId",
"roleId", entity.getId().orElseThrow());
}
void saveRolePermissionRefs(Long roleId, Role entity) {
String sql = "INSERT INTO sys_role_permission(role_id, permission_id) VALUES (:roleId, :permissionId)";
SqlParameterSource[] batchArgs = entity.getPermissions()
.stream()
.map(menuRef -> new MapSqlParameterSource()
int i = batchUpdate(
"INSERT INTO sys_role_permission(role_id, permission_id) VALUES (:roleId, :permissionId)",
entity.getPermissions(),
actionRef -> new MapSqlParameterSource()
.addValue("roleId", roleId)
.addValue("permissionId", menuRef.actionId()))
.toArray(MapSqlParameterSource[]::new);
int[] i = this.jdbc.batchUpdate(sql, batchArgs);
AssertResult.update(entity.getMenus().size(), NumberUtil.sum(i));
.addValue("permissionId", actionRef.actionId()));
assertResultEquals(i, entity.getMenus().size());
}
}