作者 朱兆平

优化sql树形结构的查询-改为查询出所有的数据后用steam进行children Set

@@ -28,6 +28,9 @@ public interface ROLEMapper { @@ -28,6 +28,9 @@ public interface ROLEMapper {
28 List<ROLE> findAll(@Param("roleName") String roleName, 28 List<ROLE> findAll(@Param("roleName") String roleName,
29 @Param("type") String type); 29 @Param("type") String type);
30 30
  31 + List<ROLE> findAllWithOutTree(@Param("roleName") String roleName,
  32 + @Param("type") String type);
  33 +
31 int updateByPrimaryKeySelective(ROLE record); 34 int updateByPrimaryKeySelective(ROLE record);
32 35
33 int updateByPrimaryKey(ROLE record); 36 int updateByPrimaryKey(ROLE record);
@@ -20,6 +20,8 @@ public interface USERSMapper { @@ -20,6 +20,8 @@ public interface USERSMapper {
20 20
21 List<USERS> selectByUsername(String userName); 21 List<USERS> selectByUsername(String userName);
22 22
  23 + USERS selectByUsernameWithRoleAndPerm(String userName);
  24 +
23 List<USERS> selectAllUser(USERS users); 25 List<USERS> selectAllUser(USERS users);
24 26
25 } 27 }
@@ -11,6 +11,7 @@ import org.springframework.security.web.util.matcher.AntPathRequestMatcher; @@ -11,6 +11,7 @@ import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
11 import org.springframework.stereotype.Service; 11 import org.springframework.stereotype.Service;
12 import org.springframework.util.AntPathMatcher; 12 import org.springframework.util.AntPathMatcher;
13 13
  14 +import javax.annotation.Resource;
14 import javax.servlet.http.HttpServletRequest; 15 import javax.servlet.http.HttpServletRequest;
15 import java.util.*; 16 import java.util.*;
16 17
@@ -21,8 +22,9 @@ import java.util.*; @@ -21,8 +22,9 @@ import java.util.*;
21 @Service 22 @Service
22 public class MyInvocationSecurityMetadataSourceService implements FilterInvocationSecurityMetadataSource{ 23 public class MyInvocationSecurityMetadataSourceService implements FilterInvocationSecurityMetadataSource{
23 24
24 - @Autowired 25 + @Resource
25 PERMISSIONMapper permissionMapper; 26 PERMISSIONMapper permissionMapper;
  27 +
26 AntPathMatcher pathMatcher = new AntPathMatcher(); 28 AntPathMatcher pathMatcher = new AntPathMatcher();
27 29
28 private HashMap<String, Collection<ConfigAttribute>> map =null; 30 private HashMap<String, Collection<ConfigAttribute>> map =null;
@@ -8,16 +8,15 @@ import com.tianbo.warehouse.annotation.cache.annotation.RedisCacheEvict; @@ -8,16 +8,15 @@ import com.tianbo.warehouse.annotation.cache.annotation.RedisCacheEvict;
8 import com.tianbo.warehouse.annotation.cache.annotation.RedisCacheable; 8 import com.tianbo.warehouse.annotation.cache.annotation.RedisCacheable;
9 import com.tianbo.warehouse.dao.PERMISSIONMapper; 9 import com.tianbo.warehouse.dao.PERMISSIONMapper;
10 import com.tianbo.warehouse.model.PERMISSION; 10 import com.tianbo.warehouse.model.PERMISSION;
  11 +import com.tianbo.warehouse.model.ROLE;
11 import com.tianbo.warehouse.service.PermissionService; 12 import com.tianbo.warehouse.service.PermissionService;
12 import lombok.extern.slf4j.Slf4j; 13 import lombok.extern.slf4j.Slf4j;
13 import org.springframework.stereotype.Service; 14 import org.springframework.stereotype.Service;
14 15
15 import javax.annotation.Resource; 16 import javax.annotation.Resource;
16 import javax.validation.constraints.NotNull; 17 import javax.validation.constraints.NotNull;
17 -import java.util.ArrayList;  
18 -import java.util.HashMap;  
19 -import java.util.List;  
20 -import java.util.Map; 18 +import java.util.*;
  19 +import java.util.stream.Collectors;
21 20
22 @Slf4j 21 @Slf4j
23 @Service("PermissionService") 22 @Service("PermissionService")
@@ -84,6 +83,17 @@ public class PermissionServiceImp implements PermissionService { @@ -84,6 +83,17 @@ public class PermissionServiceImp implements PermissionService {
84 try { 83 try {
85 //查询所有菜单 84 //查询所有菜单
86 List<PERMISSION> allMenu = permissionMapper.findByUserId(userId); 85 List<PERMISSION> allMenu = permissionMapper.findByUserId(userId);
  86 +
  87 + List<PERMISSION> allTreeMenu = allMenu.stream()
  88 + .filter(perm -> perm.getParentId() == 0)
  89 + .map((perm) ->{
  90 + perm.setChildren(getChildrens(perm, allMenu));
  91 + return perm;
  92 + }).sorted((befor,after) -> {
  93 + return (befor.getPermissionOrder() == null ? 0 : Integer.parseInt(befor.getPermissionOrder())) - (after.getPermissionOrder() == null ? 0 : Integer.parseInt(after.getPermissionOrder()));
  94 + }).collect(Collectors.toList());
  95 +
  96 +
87 // //根节点 97 // //根节点
88 // List<PERMISSION> rootMenu = new ArrayList<PERMISSION>(); 98 // List<PERMISSION> rootMenu = new ArrayList<PERMISSION>();
89 // for (PERMISSION nav : allMenu) { 99 // for (PERMISSION nav : allMenu) {
@@ -105,7 +115,7 @@ public class PermissionServiceImp implements PermissionService { @@ -105,7 +115,7 @@ public class PermissionServiceImp implements PermissionService {
105 * 115 *
106 */ 116 */
107 data.put("success", "true"); 117 data.put("success", "true");
108 - data.put("list", allMenu); 118 + data.put("list", allTreeMenu);
109 return data; 119 return data;
110 } catch (Exception e) { 120 } catch (Exception e) {
111 data.put("success", "false"); 121 data.put("success", "false");
@@ -184,7 +194,32 @@ public class PermissionServiceImp implements PermissionService { @@ -184,7 +194,32 @@ public class PermissionServiceImp implements PermissionService {
184 @Override 194 @Override
185 // @RedisCacheable(cacheKey = "getUserMenuTreeByUserId") 195 // @RedisCacheable(cacheKey = "getUserMenuTreeByUserId")
186 public List<PERMISSION> getUserMenuTreeByUserId(@NotNull Integer userId) { 196 public List<PERMISSION> getUserMenuTreeByUserId(@NotNull Integer userId) {
187 - return permissionMapper.getUserMenuTreeByUserId(userId); 197 + List<PERMISSION> loginedUserMenus = permissionMapper.getUserMenuTreeByUserId(userId);
  198 + List<PERMISSION> loginedUserMenusTree = loginedUserMenus.stream()
  199 + .filter(perm -> perm.getParentId() == 0)
  200 + .map((perm) ->{
  201 + perm.setChildren(getChildrens(perm, loginedUserMenus));
  202 + return perm;
  203 + }).sorted((befor,after) -> {
  204 + return (befor.getPermissionOrder() == null ? 0 : Integer.parseInt(befor.getPermissionOrder())) - (after.getPermissionOrder() == null ? 0 : Integer.parseInt(after.getPermissionOrder()));
  205 + }).collect(Collectors.toList());
  206 + return loginedUserMenusTree;
  207 + }
  208 +
  209 + //递归查找所有菜单的子菜单
  210 + private List<PERMISSION> getChildrens(PERMISSION root, List<PERMISSION> listAll) {
  211 + List<PERMISSION> children = listAll.stream().filter(categoryEntity -> {
  212 + return categoryEntity.getParentId().equals(root.getPermissionId());
  213 + }).map(categoryEntity -> {
  214 + //1、找到子菜单(递归)
  215 + categoryEntity.setChildren(getChildrens(categoryEntity, listAll));
  216 + return categoryEntity;
  217 + }).sorted((befor,after) -> {
  218 + return (befor.getPermissionOrder() == null ? 0 : Integer.parseInt(befor.getPermissionOrder())) - (after.getPermissionOrder() == null ? 0 : Integer.parseInt(after.getPermissionOrder()));
  219 + }).collect(Collectors.toList());
  220 +
  221 + return children;
  222 +
188 } 223 }
189 224
190 } 225 }
@@ -15,32 +15,76 @@ import org.springframework.beans.factory.annotation.Autowired; @@ -15,32 +15,76 @@ import org.springframework.beans.factory.annotation.Autowired;
15 import org.springframework.stereotype.Service; 15 import org.springframework.stereotype.Service;
16 import org.springframework.transaction.annotation.Transactional; 16 import org.springframework.transaction.annotation.Transactional;
17 17
  18 +import javax.annotation.Resource;
  19 +import java.util.Comparator;
18 import java.util.List; 20 import java.util.List;
  21 +import java.util.stream.Collectors;
19 22
20 @Service(value = "roleService") 23 @Service(value = "roleService")
21 public class RoleServiceImp implements RoleService{ 24 public class RoleServiceImp implements RoleService{
22 25
23 - @Autowired 26 + @Resource
24 private ROLEMapper roleMapper; 27 private ROLEMapper roleMapper;
25 28
26 - @Autowired 29 + @Resource
27 private RolePermissionMapper rolePermissionMapper; 30 private RolePermissionMapper rolePermissionMapper;
28 31
29 - @Autowired 32 + @Resource
30 DepartmentMapper departmentMapper; 33 DepartmentMapper departmentMapper;
31 34
32 @Autowired 35 @Autowired
33 PermissionService permissionService; 36 PermissionService permissionService;
34 37
  38 + /**
  39 + *
  40 + * @param pageNum
  41 + * @param pageSize
  42 + * @param roleName
  43 + * @param type
  44 + * @return 返回树形结构
  45 + */
35 @Override 46 @Override
36 public PageInfo<ROLE> findAll(int pageNum, int pageSize, String roleName, String type){ 47 public PageInfo<ROLE> findAll(int pageNum, int pageSize, String roleName, String type){
37 Page<ROLE> page = PageHelper.startPage(pageNum,pageSize); 48 Page<ROLE> page = PageHelper.startPage(pageNum,pageSize);
38 List<ROLE> list = roleMapper.findAll(roleName, type); 49 List<ROLE> list = roleMapper.findAll(roleName, type);
  50 +
  51 +// List<ROLE> treeList = list.stream()
  52 +// .filter(role -> role.getParentid() == 0)
  53 +// .map((role) ->{
  54 +// role.setChildren(getChildrens(role, list));
  55 +// return role;
  56 +// })
  57 +// .sorted(Comparator.comparingInt(ROLE::getRsort))
  58 +// .collect(Collectors.toList());
  59 +
39 PageInfo<ROLE> result = new PageInfo<ROLE>(list); 60 PageInfo<ROLE> result = new PageInfo<ROLE>(list);
40 61
41 return result; 62 return result;
42 } 63 }
43 64
  65 + /**
  66 + * sql查询出所有的组织机构,再根据结果集进行children Set
  67 + * @param roleName
  68 + * @param type
  69 + * @return
  70 + */
  71 + public PageInfo<ROLE> findAllWithOutTree(String roleName, String type){
  72 + List<ROLE> list = roleMapper.findAllWithOutTree(roleName, type);
  73 +
  74 + List<ROLE> treeList = list.stream()
  75 + .filter(role -> role.getParentid() == 0)
  76 + .map((role) ->{
  77 + role.setChildren(getChildrens(role, list));
  78 + return role;
  79 + })
  80 + .sorted(Comparator.comparingInt(ROLE::getRsort))
  81 + .collect(Collectors.toList());
  82 +
  83 + PageInfo<ROLE> result = new PageInfo<ROLE>(treeList);
  84 +
  85 + return result;
  86 + }
  87 +
44 @Override 88 @Override
45 public int insertSelective(ROLE record){ 89 public int insertSelective(ROLE record){
46 return roleMapper.insertSelective(record); 90 return roleMapper.insertSelective(record);
@@ -88,4 +132,21 @@ public class RoleServiceImp implements RoleService{ @@ -88,4 +132,21 @@ public class RoleServiceImp implements RoleService{
88 } 132 }
89 } 133 }
90 134
  135 +
  136 + //递归查找所有菜单的子菜单
  137 + private List<ROLE> getChildrens(ROLE root, List<ROLE> listAll) {
  138 + List<ROLE> children = listAll.stream().filter(categoryEntity -> {
  139 + return categoryEntity.getParentid().equals(root.getRoleId());
  140 + }).map(categoryEntity -> {
  141 + //1、找到子菜单(递归)
  142 + categoryEntity.setChildren(getChildrens(categoryEntity, listAll));
  143 + return categoryEntity;
  144 + }).sorted(Comparator.comparingInt(ROLE::getRsort))
  145 + .collect(Collectors.toList());
  146 +
  147 + return children;
  148 +
  149 + }
  150 +
  151 +
91 } 152 }
@@ -44,11 +44,30 @@ public class UserServiceImpl implements UserService{ @@ -44,11 +44,30 @@ public class UserServiceImpl implements UserService{
44 @Override 44 @Override
45 public USERS loadByUsername(String username){ 45 public USERS loadByUsername(String username){
46 List<USERS> userList = usersMapper.selectByUsername(username); 46 List<USERS> userList = usersMapper.selectByUsername(username);
  47 + USERS userInfo = usersMapper.selectByUsernameWithRoleAndPerm(username);
47 48
  49 + //新SQL MYBATIS SQL 绑定role跟perm的方法,以减少数据库查询次数
  50 + if (userInfo!=null){
  51 + if (userInfo.getRoles()!=null && userInfo.getRoles().isEmpty()){
  52 + List<ROLE> topRoles = roleMapper.selectTopByChildID(userInfo.getRoles().get(0).getRoleId());
  53 + topRoles.removeAll(Collections.singleton(null));
  54 +
  55 + if (!topRoles.isEmpty()){
  56 + userInfo.setCompanyId(topRoles.get(0).getRoleId());
  57 + userInfo.setCompanyName(topRoles.get(0).getRoleName());
  58 + topRoles.get(0).setChildren(null);
  59 + topRoles.get(0).setPermissions(null);
  60 + userInfo.setCompanyInfo(topRoles.get(0));
  61 + }
  62 + }
  63 + return userInfo;
  64 + }
  65 +
  66 + //旧方法
48 if (userList != null && userList.size() > 0) { 67 if (userList != null && userList.size() > 0) {
49 USERS user = userList.get(0); 68 USERS user = userList.get(0);
50 69
51 - //获取用户角色列表,用户没有配置相关组织机构时,获取不到权限 70 + //获取用户角色列表-非树形结构,用户没有配置相关组织机构时,获取不到权限
52 List<ROLE> roleList = roleMapper.findRolesByUserId(user.getUserId()); 71 List<ROLE> roleList = roleMapper.findRolesByUserId(user.getUserId());
53 //用户未绑定组织机构时会查询出null数组信息,现象为All elements are null 72 //用户未绑定组织机构时会查询出null数组信息,现象为All elements are null
54 73
@@ -69,7 +88,7 @@ public class UserServiceImpl implements UserService{ @@ -69,7 +88,7 @@ public class UserServiceImpl implements UserService{
69 user.setRoles(roleList); 88 user.setRoles(roleList);
70 } 89 }
71 90
72 - //用户权限列表 91 + //用户权限列表-非树形
73 List<PERMISSION> permissionList = permissionService.findByUserIdWithLogin(user.getUserId()); 92 List<PERMISSION> permissionList = permissionService.findByUserIdWithLogin(user.getUserId());
74 if (!permissionList.isEmpty()){ 93 if (!permissionList.isEmpty()){
75 user.setPermissions(permissionList); 94 user.setPermissions(permissionList);
@@ -92,6 +111,7 @@ public class UserServiceImpl implements UserService{ @@ -92,6 +111,7 @@ public class UserServiceImpl implements UserService{
92 for (USERS user: list) { 111 for (USERS user: list) {
93 // List<PERMISSION> permissionList = permissionMapper.findByUserId(user.getUserId()); 112 // List<PERMISSION> permissionList = permissionMapper.findByUserId(user.getUserId());
94 // user.setPermissions(permissionList); 113 // user.setPermissions(permissionList);
  114 + //为了前端role配置的适配
95 List<ROLE> roleList = roleMapper.findRolesByUserId(user.getUserId()); 115 List<ROLE> roleList = roleMapper.findRolesByUserId(user.getUserId());
96 user.setRoles(roleList); 116 user.setRoles(roleList);
97 } 117 }
@@ -101,7 +101,7 @@ FROM @@ -101,7 +101,7 @@ FROM
101 LEFT JOIN role R ON R.role_id= RP.role_id 101 LEFT JOIN role R ON R.role_id= RP.role_id
102 where P.url = #{permissionUrl,jdbcType=VARCHAR} ORDER BY permission_order 102 where P.url = #{permissionUrl,jdbcType=VARCHAR} ORDER BY permission_order
103 </select> 103 </select>
104 - <select id="findByUserId" parameterType="java.lang.Integer" resultMap="treeResultMap"> 104 + <select id="findByUserId" parameterType="java.lang.Integer" resultMap="BaseResultMap">
105 SELECT 105 SELECT
106 P.* 106 P.*
107 FROM 107 FROM
@@ -129,7 +129,6 @@ where P.url = #{permissionUrl,jdbcType=VARCHAR} ORDER BY permission_order @@ -129,7 +129,6 @@ where P.url = #{permissionUrl,jdbcType=VARCHAR} ORDER BY permission_order
129 ) 129 )
130 ) 130 )
131 ) 131 )
132 - AND P.parent_id = 0  
133 ORDER BY permission_order 132 ORDER BY permission_order
134 </select> 133 </select>
135 <select id="findByUserIdWithLogin" parameterType="java.lang.Integer" resultMap="BaseResultMap"> 134 <select id="findByUserIdWithLogin" parameterType="java.lang.Integer" resultMap="BaseResultMap">
@@ -325,14 +324,12 @@ where P.url = #{permissionUrl,jdbcType=VARCHAR} ORDER BY permission_order @@ -325,14 +324,12 @@ where P.url = #{permissionUrl,jdbcType=VARCHAR} ORDER BY permission_order
325 where permission_id = #{permissionId,jdbcType=INTEGER} 324 where permission_id = #{permissionId,jdbcType=INTEGER}
326 </update> 325 </update>
327 326
328 - <select id="getUserMenuTreeByUserId" parameterType="java.lang.Integer" resultMap="treeMap"> 327 + <select id="getUserMenuTreeByUserId" parameterType="java.lang.Integer" resultMap="BaseResultMap">
329 SELECT 328 SELECT
330 P.* 329 P.*
331 FROM 330 FROM
332 permission P 331 permission P
333 WHERE 332 WHERE
334 - parent_id = 0  
335 - AND  
336 ismenu = 1 333 ismenu = 1
337 AND 334 AND
338 P.permission_id IN ( 335 P.permission_id IN (
@@ -13,9 +13,36 @@ @@ -13,9 +13,36 @@
13 <result column="business_license" jdbcType="VARCHAR" property="businessLicense" /> 13 <result column="business_license" jdbcType="VARCHAR" property="businessLicense" />
14 <result column="departmentId" jdbcType="VARCHAR" property="departmentid" /> 14 <result column="departmentId" jdbcType="VARCHAR" property="departmentid" />
15 <result column="mq_code" jdbcType="VARCHAR" property="mqcode" /> 15 <result column="mq_code" jdbcType="VARCHAR" property="mqcode" />
  16 + </resultMap>
  17 + <resultMap id="TreeWithPermResultMap" type="com.tianbo.warehouse.model.ROLE">
  18 + <id column="role_id" jdbcType="INTEGER" property="roleId" />
  19 + <result column="role_name" jdbcType="VARCHAR" property="roleName" />
  20 + <result column="role_sign" jdbcType="VARCHAR" property="roleSign" />
  21 + <result column="description" jdbcType="VARCHAR" property="description" />
  22 + <result column="type" jdbcType="VARCHAR" property="type" />
  23 + <result column="parentId" jdbcType="INTEGER" property="parentid" />
  24 + <result column="rsort" jdbcType="INTEGER" property="rsort" />
  25 + <result column="customs_reg_code" jdbcType="VARCHAR" property="customsRegCode" />
  26 + <result column="business_license" jdbcType="VARCHAR" property="businessLicense" />
  27 + <result column="departmentId" jdbcType="VARCHAR" property="departmentid" />
  28 + <result column="mq_code" jdbcType="VARCHAR" property="mqcode" />
16 <collection column="role_id" javaType="java.util.ArrayList" ofType="com.tianbo.warehouse.model.PERMISSION" property="permissions" select="com.tianbo.warehouse.dao.PERMISSIONMapper.getRolePermisson" /> 29 <collection column="role_id" javaType="java.util.ArrayList" ofType="com.tianbo.warehouse.model.PERMISSION" property="permissions" select="com.tianbo.warehouse.dao.PERMISSIONMapper.getRolePermisson" />
17 <collection column="role_id" property="children" select="selectByParentId" /> 30 <collection column="role_id" property="children" select="selectByParentId" />
18 </resultMap> 31 </resultMap>
  32 + <resultMap id="TreeWithResultMap" type="com.tianbo.warehouse.model.ROLE">
  33 + <id column="role_id" jdbcType="INTEGER" property="roleId" />
  34 + <result column="role_name" jdbcType="VARCHAR" property="roleName" />
  35 + <result column="role_sign" jdbcType="VARCHAR" property="roleSign" />
  36 + <result column="description" jdbcType="VARCHAR" property="description" />
  37 + <result column="type" jdbcType="VARCHAR" property="type" />
  38 + <result column="parentId" jdbcType="INTEGER" property="parentid" />
  39 + <result column="rsort" jdbcType="INTEGER" property="rsort" />
  40 + <result column="customs_reg_code" jdbcType="VARCHAR" property="customsRegCode" />
  41 + <result column="business_license" jdbcType="VARCHAR" property="businessLicense" />
  42 + <result column="departmentId" jdbcType="VARCHAR" property="departmentid" />
  43 + <result column="mq_code" jdbcType="VARCHAR" property="mqcode" />
  44 + <collection column="role_id" property="children" select="selectByParentId" />
  45 + </resultMap>
19 <sql id="Base_Column_List"> 46 <sql id="Base_Column_List">
20 role_id, role_name, role_sign, description, type, parentId, rsort, customs_reg_code, 47 role_id, role_name, role_sign, description, type, parentId, rsort, customs_reg_code,
21 business_license, departmentId, mq_code 48 business_license, departmentId, mq_code
@@ -26,7 +53,7 @@ @@ -26,7 +53,7 @@
26 from role 53 from role
27 where role_id = #{roleId,jdbcType=INTEGER} 54 where role_id = #{roleId,jdbcType=INTEGER}
28 </select> 55 </select>
29 - <select id="selectByParentId" parameterType="java.lang.Integer" resultMap="BaseResultMap"> 56 + <select id="selectByParentId" parameterType="java.lang.Integer" resultMap="TreeWithResultMap">
30 select 57 select
31 <include refid="Base_Column_List" /> 58 <include refid="Base_Column_List" />
32 from role 59 from role
@@ -205,18 +232,20 @@ WHERE @@ -205,18 +232,20 @@ WHERE
205 where role_id = #{roleId,jdbcType=INTEGER} 232 where role_id = #{roleId,jdbcType=INTEGER}
206 </update> 233 </update>
207 234
208 - <select id="findAll" parameterType="java.lang.String" resultMap="BaseResultMap"> 235 + <select id="findAll" parameterType="java.lang.String" resultMap="TreeWithResultMap">
209 SELECT 236 SELECT
210 <include refid="Base_Column_List" /> 237 <include refid="Base_Column_List" />
211 FROM role 238 FROM role
212 - where parentId=0  
213 - and 1=1 239 + <where>
  240 + parentId = 0
214 <if test="roleName != '' and roleName !=null"> 241 <if test="roleName != '' and roleName !=null">
215 and role_name = #{roleName, jdbcType=VARCHAR} 242 and role_name = #{roleName, jdbcType=VARCHAR}
216 </if> 243 </if>
217 <if test="type != '' and type !=null"> 244 <if test="type != '' and type !=null">
218 and type like '%' #{type} '%' 245 and type like '%' #{type} '%'
219 </if> 246 </if>
  247 + </where>
  248 + order by rsort
220 </select> 249 </select>
221 <select id="findRolesByUserId" parameterType="java.lang.Integer" resultMap="BaseResultMap"> 250 <select id="findRolesByUserId" parameterType="java.lang.Integer" resultMap="BaseResultMap">
222 SELECT 251 SELECT
@@ -227,4 +256,19 @@ WHERE @@ -227,4 +256,19 @@ WHERE
227 LEFT JOIN role R ON R.role_id= UR.role_id 256 LEFT JOIN role R ON R.role_id= UR.role_id
228 where U.user_id = #{userId,jdbcType=INTEGER} 257 where U.user_id = #{userId,jdbcType=INTEGER}
229 </select> 258 </select>
  259 +
  260 + <select id="findAllWithOutTree" parameterType="java.lang.String" resultMap="BaseResultMap">
  261 + SELECT
  262 + <include refid="Base_Column_List" />
  263 + FROM role
  264 + <where>
  265 + <if test="roleName != '' and roleName !=null">
  266 + and role_name = #{roleName, jdbcType=VARCHAR}
  267 + </if>
  268 + <if test="type != '' and type !=null">
  269 + and type like '%' #{type} '%'
  270 + </if>
  271 + </where>
  272 + order by parentId,rsort
  273 + </select>
230 </mapper> 274 </mapper>
@@ -32,6 +32,35 @@ @@ -32,6 +32,35 @@
32 <result column="age" property="age" jdbcType="INTEGER" /> 32 <result column="age" property="age" jdbcType="INTEGER" />
33 <result column="company_id" property="companyId" jdbcType="INTEGER" /> 33 <result column="company_id" property="companyId" jdbcType="INTEGER" />
34 </resultMap> 34 </resultMap>
  35 + <resultMap id="WithRoleAndPermResultMap" type="com.tianbo.warehouse.model.USERS" extends="BaseResultMap">
  36 + <collection property="roles" javaType="java.util.ArrayList" ofType="com.tianbo.warehouse.model.ROLE">
  37 + <result column="role_id" jdbcType="INTEGER" property="roleId" />
  38 + <result column="role_name" jdbcType="VARCHAR" property="roleName" />
  39 + <result column="role_sign" jdbcType="VARCHAR" property="roleSign" />
  40 + <result column="rdescription" jdbcType="VARCHAR" property="description" />
  41 + <result column="type" jdbcType="VARCHAR" property="type" />
  42 + <result column="parentId" jdbcType="INTEGER" property="parentid" />
  43 + <result column="rsort" jdbcType="INTEGER" property="rsort" />
  44 + <result column="customs_reg_code" jdbcType="VARCHAR" property="customsRegCode" />
  45 + <result column="business_license" jdbcType="VARCHAR" property="businessLicense" />
  46 + <result column="departmentId" jdbcType="VARCHAR" property="departmentid" />
  47 + <result column="mq_code" jdbcType="VARCHAR" property="mqcode" />
  48 + </collection>
  49 + <collection property="permissions" javaType="java.util.ArrayList" ofType="com.tianbo.warehouse.model.PERMISSION">
  50 + <result column="permission_id" property="permissionId" jdbcType="INTEGER" />
  51 + <result column="name" property="name" jdbcType="VARCHAR" />
  52 + <result column="permission_order" property="permissionOrder" jdbcType="VARCHAR" />
  53 + <result column="pdescription" property="description" jdbcType="VARCHAR" />
  54 + <result column="ismenu" property="ismenu" jdbcType="BOOLEAN" />
  55 + <result column="hidden" property="hidden" jdbcType="BOOLEAN" />
  56 + <result column="parent_id" property="parentId" jdbcType="INTEGER" />
  57 + <result column="path" property="path" jdbcType="VARCHAR" />
  58 + <result column="url" property="url" jdbcType="VARCHAR" />
  59 + <result column="method" property="method" jdbcType="VARCHAR" />
  60 + <result column="iconCls" property="iconCls" jdbcType="VARCHAR" />
  61 + <result column="component" property="component" jdbcType="VARCHAR" />
  62 + </collection>
  63 + </resultMap>
35 <sql id="Base_Column_List" > 64 <sql id="Base_Column_List" >
36 user_id, username, password, birthday, sex, address, state, mobilePhone, creatTime, 65 user_id, username, password, birthday, sex, address, state, mobilePhone, creatTime,
37 updateTime, userFace, realName, email, age,company_id 66 updateTime, userFace, realName, email, age,company_id
@@ -51,6 +80,27 @@ @@ -51,6 +80,27 @@
51 from users 80 from users
52 where username = #{username,jdbcType=VARCHAR} 81 where username = #{username,jdbcType=VARCHAR}
53 </select> 82 </select>
  83 +
  84 + <select id="selectByUsernameWithRoleAndPerm" resultMap="WithRoleAndPermResultMap" parameterType="java.lang.String" >
  85 + select
  86 + u.user_id, username, password, birthday, sex, address, state, mobilePhone, creatTime,
  87 + updateTime, userFace, realName, email, age,company_id,
  88 +
  89 + r.role_id, role_name, role_sign, r.description as rdescription, `type`, parentId, rsort, customs_reg_code,
  90 + business_license, departmentId, mq_code,
  91 +
  92 + p.permission_id, `name`, permission_order, p.description as pdescription, ismenu,hidden,parent_id,
  93 + path, url, method, iconCls, component
  94 + from
  95 + (select
  96 + <include refid="Base_Column_List" />
  97 + from users
  98 + where username = 'nmms') u
  99 + left join user_role ur on u.user_id = ur.user_id
  100 + left join role r on r.role_id = ur.role_id
  101 + left join role_permission rp on r.role_id = rp.role_id
  102 + left join permission p on rp.permission_id = p.permission_id
  103 + </select>
54 <select id="selectAllUser" resultMap="BaseResultMap" parameterType="com.tianbo.warehouse.model.USERS" > 104 <select id="selectAllUser" resultMap="BaseResultMap" parameterType="com.tianbo.warehouse.model.USERS" >
55 select 105 select
56 <!-- <include refid="Base_Column_List" />--> 106 <!-- <include refid="Base_Column_List" />-->
  1 +package com.tianbo.warehouse;
  2 +
  3 +import com.tianbo.warehouse.WarehouseApplication;
  4 +import com.tianbo.warehouse.security.CustomUserDetailService;
  5 +import lombok.extern.slf4j.Slf4j;
  6 +import org.junit.Test;
  7 +import org.junit.runner.RunWith;
  8 +import org.springframework.beans.factory.annotation.Autowired;
  9 +import org.springframework.boot.test.context.SpringBootTest;
  10 +import org.springframework.security.core.userdetails.UserDetails;
  11 +import org.springframework.test.context.junit4.SpringRunner;
  12 +
  13 +@RunWith(SpringRunner.class)
  14 +@SpringBootTest(classes = WarehouseApplication.class,webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
  15 +@Slf4j
  16 +public class UserTest {
  17 +
  18 + @Autowired
  19 + CustomUserDetailService customUserDetailService;
  20 +
  21 + @Test
  22 + public void contextLoads() {
  23 +
  24 + UserDetails u = customUserDetailService.loadUserByUsername("nmms");
  25 + log.info("ok");
  26 + }
  27 +}