作者 朱兆平

组织机构sql优化,一条sql查出组织机构树形结构以及相关联的权限信息,提高查询效率.

@@ -28,7 +28,14 @@ public interface ROLEMapper { @@ -28,7 +28,14 @@ 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> findAllWithChildAndTree(); 31 + /**
  32 + * 为findAll的sql优化版本
  33 + * @param roleName
  34 + * @param type
  35 + * @return
  36 + */
  37 + List<ROLE> findAllWithChildAndTree(@Param("roleName") String roleName,
  38 + @Param("type") String type);
32 39
33 List<ROLE> findAllWithOutTree(@Param("roleName") String roleName, 40 List<ROLE> findAllWithOutTree(@Param("roleName") String roleName,
34 @Param("type") String type); 41 @Param("type") String type);
@@ -47,7 +47,7 @@ public class RoleServiceImp implements RoleService{ @@ -47,7 +47,7 @@ public class RoleServiceImp implements RoleService{
47 @Override 47 @Override
48 public PageInfo<ROLE> findAll(int pageNum, int pageSize, String roleName, String type){ 48 public PageInfo<ROLE> findAll(int pageNum, int pageSize, String roleName, String type){
49 Page<ROLE> page = PageHelper.startPage(pageNum,pageSize); 49 Page<ROLE> page = PageHelper.startPage(pageNum,pageSize);
50 - List<ROLE> list = roleMapper.findAll(roleName, type); 50 + List<ROLE> list = roleMapper.findAllWithChildAndTree(roleName, type);
51 51
52 // List<ROLE> treeList = list.stream() 52 // List<ROLE> treeList = list.stream()
53 // .filter(role -> role.getParentid() == 0) 53 // .filter(role -> role.getParentid() == 0)
@@ -239,7 +239,7 @@ WHERE @@ -239,7 +239,7 @@ WHERE
239 order by rsort 239 order by rsort
240 </select> 240 </select>
241 241
242 - <select id="findAllWithChildAndTree" resultMap="TreeWithPermResultMap"> 242 + <select id="findAllWithChildAndTree" parameterType="java.lang.String" resultMap="TreeWithPermResultMap">
243 select r.role_id, 243 select r.role_id,
244 r.role_name, 244 r.role_name,
245 r.role_sign, 245 r.role_sign,
@@ -278,21 +278,19 @@ WHERE @@ -278,21 +278,19 @@ WHERE
278 FROM role 278 FROM role
279 <where> 279 <where>
280 parentId = 0 280 parentId = 0
281 - <if test="roleName != '' and roleName !=null"> 281 + <if test="roleName !=null and roleName != '' ">
282 and role_name = #{roleName, jdbcType=VARCHAR} 282 and role_name = #{roleName, jdbcType=VARCHAR}
283 </if> 283 </if>
284 - <if test="type != '' and type !=null"> 284 + <if test="type !=null and type != ''">
285 and type like '%' #{type} '%' 285 and type like '%' #{type} '%'
286 </if> 286 </if>
287 </where> 287 </where>
288 order by rsort 288 order by rsort
289 - limit 5  
290 ) r 289 ) r
291 left join role_permission rp 290 left join role_permission rp
292 on r.role_id = rp.role_id 291 on r.role_id = rp.role_id
293 left join permission p 292 left join permission p
294 on rp.permission_id = p.permission_id 293 on rp.permission_id = p.permission_id
295 - where p.permission_id is not null  
296 order by r.rsort, r.role_id, p.ismenu, p.name, p.permission_order 294 order by r.rsort, r.role_id, p.ismenu, p.name, p.permission_order
297 295
298 </select> 296 </select>
1 package com.tianbo.warehouse; 1 package com.tianbo.warehouse;
2 2
  3 +import com.github.pagehelper.PageInfo;
3 import com.tianbo.warehouse.WarehouseApplication; 4 import com.tianbo.warehouse.WarehouseApplication;
4 import com.tianbo.warehouse.model.ROLE; 5 import com.tianbo.warehouse.model.ROLE;
5 import com.tianbo.warehouse.security.CustomUserDetailService; 6 import com.tianbo.warehouse.security.CustomUserDetailService;
  7 +import com.tianbo.warehouse.service.RoleService;
6 import lombok.extern.slf4j.Slf4j; 8 import lombok.extern.slf4j.Slf4j;
7 import org.junit.Test; 9 import org.junit.Test;
8 import org.junit.runner.RunWith; 10 import org.junit.runner.RunWith;
@@ -26,11 +28,14 @@ public class UserTest { @@ -26,11 +28,14 @@ public class UserTest {
26 @Resource 28 @Resource
27 ROLEMapper roleMapper; 29 ROLEMapper roleMapper;
28 30
  31 + @Autowired
  32 + RoleService roleService;
  33 +
29 @Test 34 @Test
30 public void contextLoads() { 35 public void contextLoads() {
31 36
32 UserDetails u = customUserDetailService.loadUserByUsername("nmms"); 37 UserDetails u = customUserDetailService.loadUserByUsername("nmms");
33 - List<ROLE> roles = roleMapper.findAllWithChildAndTree(); 38 + PageInfo<ROLE> all = roleService.findAll(1, 20, "瑞泰", null);
34 log.info("ok"); 39 log.info("ok");
35 } 40 }
36 } 41 }