作者 朱兆平

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

... ... @@ -28,7 +28,14 @@ public interface ROLEMapper {
List<ROLE> findAll(@Param("roleName") String roleName,
@Param("type") String type);
List<ROLE> findAllWithChildAndTree();
/**
* 为findAll的sql优化版本
* @param roleName
* @param type
* @return
*/
List<ROLE> findAllWithChildAndTree(@Param("roleName") String roleName,
@Param("type") String type);
List<ROLE> findAllWithOutTree(@Param("roleName") String roleName,
@Param("type") String type);
... ...
... ... @@ -47,7 +47,7 @@ public class RoleServiceImp implements RoleService{
@Override
public PageInfo<ROLE> findAll(int pageNum, int pageSize, String roleName, String type){
Page<ROLE> page = PageHelper.startPage(pageNum,pageSize);
List<ROLE> list = roleMapper.findAll(roleName, type);
List<ROLE> list = roleMapper.findAllWithChildAndTree(roleName, type);
// List<ROLE> treeList = list.stream()
// .filter(role -> role.getParentid() == 0)
... ...
... ... @@ -239,7 +239,7 @@ WHERE
order by rsort
</select>
<select id="findAllWithChildAndTree" resultMap="TreeWithPermResultMap">
<select id="findAllWithChildAndTree" parameterType="java.lang.String" resultMap="TreeWithPermResultMap">
select r.role_id,
r.role_name,
r.role_sign,
... ... @@ -278,21 +278,19 @@ WHERE
FROM role
<where>
parentId = 0
<if test="roleName != '' and roleName !=null">
<if test="roleName !=null and roleName != '' ">
and role_name = #{roleName, jdbcType=VARCHAR}
</if>
<if test="type != '' and type !=null">
<if test="type !=null and type != ''">
and type like '%' #{type} '%'
</if>
</where>
order by rsort
limit 5
) r
left join role_permission rp
on r.role_id = rp.role_id
left join permission p
on rp.permission_id = p.permission_id
where p.permission_id is not null
order by r.rsort, r.role_id, p.ismenu, p.name, p.permission_order
</select>
... ...
package com.tianbo.warehouse;
import com.github.pagehelper.PageInfo;
import com.tianbo.warehouse.WarehouseApplication;
import com.tianbo.warehouse.model.ROLE;
import com.tianbo.warehouse.security.CustomUserDetailService;
import com.tianbo.warehouse.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
... ... @@ -26,11 +28,14 @@ public class UserTest {
@Resource
ROLEMapper roleMapper;
@Autowired
RoleService roleService;
@Test
public void contextLoads() {
UserDetails u = customUserDetailService.loadUserByUsername("nmms");
List<ROLE> roles = roleMapper.findAllWithChildAndTree();
PageInfo<ROLE> all = roleService.findAll(1, 20, "瑞泰", null);
log.info("ok");
}
}
... ...