fix(roleAuthority): 优化角色权限关联查询

This commit is contained in:
verto 2024-02-01 22:33:29 +08:00
parent e26d837e35
commit 22bb6a47c5
7 changed files with 34 additions and 28 deletions

View File

@ -3,7 +3,7 @@ package com.zsc.edu.bill.framework.security;
import com.zsc.edu.bill.modules.system.entity.Authority;
import com.zsc.edu.bill.modules.system.entity.RoleAuthority;
import com.zsc.edu.bill.modules.system.entity.User;
import com.zsc.edu.bill.modules.system.repo.RoleAuthoritiesReposity;
import com.zsc.edu.bill.modules.system.repo.RoleAuthoritiesRepository;
import com.zsc.edu.bill.modules.system.repo.UserRepository;
import lombok.AllArgsConstructor;
import org.springframework.security.core.userdetails.UserDetails;
@ -12,9 +12,7 @@ import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
/**
@ -25,14 +23,14 @@ import java.util.stream.Collectors;
public class JpaUserDetailsServiceImpl implements UserDetailsService {
private final UserRepository userRepo;
private final RoleAuthoritiesReposity roleAuthoritiesReposity;
private final RoleAuthoritiesRepository roleAuthoritiesRepository;
@Override
@Transactional(rollbackFor = Exception.class)
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
User user = userRepo.selectByUsername(username);
List<RoleAuthority> roleAuthorities= roleAuthoritiesReposity.selectByRoleId(user.getRoleId());
List<RoleAuthority> roleAuthorities= roleAuthoritiesRepository.selectByRoleId(user.getRoleId());
user.role.authorities=roleAuthorities.stream()
.map(i -> Authority.valueOf(i.getAuthority()))
.collect(Collectors.toSet());

View File

@ -3,6 +3,8 @@ package com.zsc.edu.bill.modules.system.entity;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import java.io.Serializable;
@ -11,12 +13,13 @@ import java.io.Serializable;
* @author
*/
@AllArgsConstructor
@Data
@Getter
@Setter
@TableName("sys_role_authorities")
public class RoleAuthority implements Serializable {
private Long roleId;
private String authority;
private static final long serialVersionUID = 1L;
}

View File

@ -2,10 +2,15 @@ package com.zsc.edu.bill.modules.system.repo;
import com.zsc.edu.bill.modules.system.entity.RoleAuthority;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
import java.util.Set;
public interface RoleAuthoritiesReposity extends BaseMapper<RoleAuthority> {
/**
* @author Yao
*/
public interface RoleAuthoritiesRepository extends BaseMapper<RoleAuthority> {
@Select("select * from sys_role_authorities where role_id=#{roleId}")
List<RoleAuthority> selectByRoleId(Long roleId);
}

View File

@ -1,14 +1,14 @@
package com.zsc.edu.bill.modules.system.service.impl;
import com.zsc.edu.bill.modules.system.entity.RoleAuthority;
import com.zsc.edu.bill.modules.system.repo.RoleAuthoritiesReposity;
import com.zsc.edu.bill.modules.system.repo.RoleAuthoritiesRepository;
import com.zsc.edu.bill.modules.system.service.RoleAuthService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
@Service
public class RoleAuthServiceImpl extends ServiceImpl<RoleAuthoritiesReposity, RoleAuthority> implements RoleAuthService {
public class RoleAuthServiceImpl extends ServiceImpl<RoleAuthoritiesRepository, RoleAuthority> implements RoleAuthService {
@Override
public boolean removeByRoleId(Long roleId) {
return remove(new LambdaQueryWrapper<RoleAuthority>().eq(RoleAuthority::getRoleId, roleId));

View File

@ -9,11 +9,15 @@ mybatis-plus:
spring:
datasource:
url: jdbc:mysql://59.110.238.182:3306/study?useSSL=false&allowPublicKeyRetrieval=true&createDatabaseIfNotExist=true&characterEncoding=utf8&serverTimezone=Hongkong
url: jdbc:mysql://localhost:3306/bill?useSSL=false&allowPublicKeyRetrieval=true&createDatabaseIfNotExist=true&characterEncoding=utf8&serverTimezone=Hongkong
username: root
password: 2002
password: 123123
driver-class-name: com.mysql.cj.jdbc.Driver
servlet:
multipart:
max-file-size: 40MB
max-request-size: 40MB
storage:
attachment: ./storage/attachment
temp: ./storage/temp

View File

@ -1,17 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zsc.edu.bill.modules.system.repo.RoleAuthoritiesReposity">
<resultMap id="BaseResultMap" type="com.zsc.edu.bill.modules.system.entity.RoleAuthority">
<result column="role_id" jdbcType="BIGINT" property="roleId"/>
<result column="authority" jdbcType="BIGINT" property="authority"/>
</resultMap>
<mapper namespace="com.zsc.edu.bill.modules.system.repo.RoleAuthoritiesRepository">
<!-- <resultMap id="BaseResultMap" type="com.zsc.edu.bill.modules.system.entity.RoleAuthority">-->
<!-- <result column="role_id" jdbcType="BIGINT" property="roleId"/>-->
<!-- <result column="authority" jdbcType="BIGINT" property="authority"/>-->
<!-- </resultMap>-->
<select id="selectByRoleId" parameterType="java.lang.Long" resultMap="BaseResultMap">
select
*
from sys_role_authorities a
where a.role_id = #{roleId}
</select>
<!-- <select id="selectByRoleId" parameterType="java.lang.Long" resultMap="BaseResultMap">-->
<!-- select * from sys_role_authorities sra where sra.role_id = #{roleId}-->
<!-- </select>-->

View File

@ -3,9 +3,8 @@ package com.zsc.edu.bill.service;
import com.zsc.edu.bill.modules.system.entity.Authority;
import com.zsc.edu.bill.modules.system.entity.RoleAuthority;
import com.zsc.edu.bill.modules.system.entity.User;
import com.zsc.edu.bill.modules.system.repo.RoleAuthoritiesReposity;
import com.zsc.edu.bill.modules.system.repo.RoleAuthoritiesRepository;
import com.zsc.edu.bill.modules.system.repo.UserRepository;
import com.zsc.edu.bill.modules.system.service.UserService;
import jakarta.annotation.Resource;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@ -22,13 +21,13 @@ import java.util.Set;
@SpringBootTest
public class UserServiceTest {
@Resource
private RoleAuthoritiesReposity roleAuthoritiesReposity;
private RoleAuthoritiesRepository roleAuthoritiesRepository;
@Resource
private UserRepository userRepository;
@Test
void test() {
User user=userRepository.selectByUsername("admin");
List<RoleAuthority> authorities= roleAuthoritiesReposity.selectByRoleId(user.getRoleId());
List<RoleAuthority> authorities= roleAuthoritiesRepository.selectByRoleId(user.getRoleId());
Set<Authority> authorities1=new HashSet<>();
authorities.stream().forEach(authority ->authorities1.add(Authority.valueOf(authority.getAuthority())));
user.role.authorities=authorities1;