Skip to content

Commit

Permalink
fix: 完善事务注解
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles7c committed Apr 3, 2023
1 parent 9b2a924 commit 18c54a7
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,12 @@ public interface RoleDeptService {
boolean save(List<Long> deptIds, Long roleId);

/**
* 根据角色 ID 查询
* 根据角色 ID 删除
*
* @param roleId
* 角色 ID
* @return 部门 ID 列表
* @param roleIds
* 角色 ID 列表
*/
List<Long> listDeptIdByRoleId(Long roleId);
void deleteByRoleIds(List<Long> roleIds);

/**
* 根据部门 ID 删除
Expand All @@ -55,10 +54,11 @@ public interface RoleDeptService {
void deleteByDeptIds(List<Long> deptIds);

/**
* 根据角色 ID 删除
* 根据角色 ID 查询
*
* @param roleIds
* 角色 ID 列表
* @param roleId
* 角色 ID
* @return 部门 ID 列表
*/
void deleteByRoleIds(List<Long> roleIds);
List<Long> listDeptIdByRoleId(Long roleId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,19 @@ public interface RoleMenuService {
boolean save(List<Long> menuIds, Long roleId);

/**
* 根据角色 ID 查询
* 根据角色 ID 删除
*
* @param roleIds
* 角色 ID 列表
* @return 菜单 ID 列表
*/
List<Long> listMenuIdByRoleIds(List<Long> roleIds);
void deleteByRoleIds(List<Long> roleIds);

/**
* 根据角色 ID 删除
* 根据角色 ID 查询
*
* @param roleIds
* 角色 ID 列表
* @return 菜单 ID 列表
*/
void deleteByRoleIds(List<Long> roleIds);
List<Long> listMenuIdByRoleIds(List<Long> roleIds);
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,12 @@ public interface UserRoleService {
boolean save(List<Long> roleIds, Long userId);

/**
* 根据角色 ID 列表查询
* 根据用户 ID 删除
*
* @param roleIds
* 角色 ID 列表
* @return 总记录数
* @param userIds
* 用户 ID 列表
*/
Long countByRoleIds(List<Long> roleIds);
void deleteByUserIds(List<Long> userIds);

/**
* 根据用户 ID 查询
Expand All @@ -56,10 +55,11 @@ public interface UserRoleService {
List<Long> listRoleIdByUserId(Long userId);

/**
* 根据用户 ID 删除
* 根据角色 ID 列表查询
*
* @param userIds
* 用户 ID 列表
* @param roleIds
* 角色 ID 列表
* @return 总记录数
*/
void deleteByUserIds(List<Long> userIds);
Long countByRoleIds(List<Long> roleIds);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import lombok.RequiredArgsConstructor;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.hutool.core.collection.CollUtil;

Expand All @@ -42,6 +43,7 @@ public class RoleDeptServiceImpl implements RoleDeptService {
private final RoleDeptMapper roleDeptMapper;

@Override
@Transactional(rollbackFor = Exception.class)
public boolean save(List<Long> deptIds, Long roleId) {
// 检查是否有变更
List<Long> oldDeptIdList = roleDeptMapper.lambdaQuery().select(RoleDeptDO::getDeptId)
Expand All @@ -58,17 +60,19 @@ public boolean save(List<Long> deptIds, Long roleId) {
}

@Override
public List<Long> listDeptIdByRoleId(Long roleId) {
return roleDeptMapper.selectDeptIdByRoleId(roleId);
@Transactional(rollbackFor = Exception.class)
public void deleteByRoleIds(List<Long> roleIds) {
roleDeptMapper.lambdaUpdate().in(RoleDeptDO::getRoleId, roleIds).remove();
}

@Override
@Transactional(rollbackFor = Exception.class)
public void deleteByDeptIds(List<Long> deptIds) {
roleDeptMapper.lambdaUpdate().in(RoleDeptDO::getDeptId, deptIds).remove();
}

@Override
public void deleteByRoleIds(List<Long> roleIds) {
roleDeptMapper.lambdaUpdate().in(RoleDeptDO::getRoleId, roleIds).remove();
public List<Long> listDeptIdByRoleId(Long roleId) {
return roleDeptMapper.selectDeptIdByRoleId(roleId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import lombok.RequiredArgsConstructor;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.hutool.core.collection.CollUtil;

Expand All @@ -43,6 +44,7 @@ public class RoleMenuServiceImpl implements RoleMenuService {
private final RoleMenuMapper roleMenuMapper;

@Override
@Transactional(rollbackFor = Exception.class)
public boolean save(List<Long> menuIds, Long roleId) {
// 检查是否有变更
List<Long> oldMenuIdList = roleMenuMapper.lambdaQuery().select(RoleMenuDO::getMenuId)
Expand All @@ -58,16 +60,17 @@ public boolean save(List<Long> menuIds, Long roleId) {
return roleMenuMapper.insertBatch(roleMenuList);
}

@Override
@Transactional(rollbackFor = Exception.class)
public void deleteByRoleIds(List<Long> roleIds) {
roleMenuMapper.lambdaUpdate().in(RoleMenuDO::getRoleId, roleIds).remove();
}

@Override
public List<Long> listMenuIdByRoleIds(List<Long> roleIds) {
if (CollUtil.isEmpty(roleIds)) {
return new ArrayList<>(0);
}
return roleMenuMapper.selectMenuIdByRoleIds(roleIds);
}

@Override
public void deleteByRoleIds(List<Long> roleIds) {
roleMenuMapper.lambdaUpdate().in(RoleMenuDO::getRoleId, roleIds).remove();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import lombok.RequiredArgsConstructor;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.hutool.core.collection.CollUtil;

Expand All @@ -42,6 +43,7 @@ public class UserRoleServiceImpl implements UserRoleService {
private final UserRoleMapper userRoleMapper;

@Override
@Transactional(rollbackFor = Exception.class)
public boolean save(List<Long> roleIds, Long userId) {
// 检查是否有变更
List<Long> oldRoleIdList = userRoleMapper.lambdaQuery().select(UserRoleDO::getRoleId)
Expand All @@ -58,8 +60,9 @@ public boolean save(List<Long> roleIds, Long userId) {
}

@Override
public Long countByRoleIds(List<Long> roleIds) {
return userRoleMapper.lambdaQuery().in(UserRoleDO::getRoleId, roleIds).count();
@Transactional(rollbackFor = Exception.class)
public void deleteByUserIds(List<Long> userIds) {
userRoleMapper.lambdaUpdate().in(UserRoleDO::getUserId, userIds).remove();
}

@Override
Expand All @@ -68,7 +71,7 @@ public List<Long> listRoleIdByUserId(Long userId) {
}

@Override
public void deleteByUserIds(List<Long> userIds) {
userRoleMapper.lambdaUpdate().in(UserRoleDO::getUserId, userIds).remove();
public Long countByRoleIds(List<Long> roleIds) {
return userRoleMapper.lambdaQuery().in(UserRoleDO::getRoleId, roleIds).count();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ public String uploadAvatar(MultipartFile avatarFile, Long id) {
}

@Override
@Transactional(rollbackFor = Exception.class)
public void updateBasicInfo(UpdateBasicInfoRequest request, Long id) {
super.getById(id);
baseMapper.lambdaUpdate().set(UserDO::getNickname, request.getNickname())
Expand Down Expand Up @@ -219,6 +220,7 @@ public void updateEmail(String newEmail, String currentPassword, Long id) {
}

@Override
@Transactional(rollbackFor = Exception.class)
public void resetPassword(Long id) {
UserDO user = super.getById(id);
user.setPassword(SecureUtils.md5Salt(SysConsts.DEFAULT_PASSWORD, id.toString()));
Expand All @@ -227,6 +229,7 @@ public void resetPassword(Long id) {
}

@Override
@Transactional(rollbackFor = Exception.class)
public void updateRole(UpdateUserRoleRequest request, Long id) {
super.getById(id);
// 保存用户和角色关联
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ public R updatePassword(@Validated @RequestBody UpdatePasswordRequest updatePass
String rawNewPassword =
ExceptionUtils.exToNull(() -> SecureUtils.decryptByRsaPrivateKey(updatePasswordRequest.getNewPassword()));
ValidationUtils.throwIfBlank(rawNewPassword, "新密码解密失败");
ValidationUtils.throwIf(!ReUtil.isMatch(RegexConsts.PASSWORD, rawNewPassword), "密码长度 6 到 32 位,同时包含字母和数字");
ValidationUtils.throwIf(!ReUtil.isMatch(RegexConsts.PASSWORD, rawNewPassword),
"密码长度为 6 到 32 位,可以包含字母、数字、下划线,特殊字符,同时包含字母和数字");

// 修改密码
userService.updatePassword(rawOldPassword, rawNewPassword, LoginHelper.getUserId());
Expand Down

0 comments on commit 18c54a7

Please sign in to comment.