作者 朱兆平

用户查询接口与SQL优化,增加组织机构ID查询,提供给前端通过组织机构id查询对应的用户列表,唯一缺点是这个sql当一个用户有多个组织机构时查询条数为10条时返…

…回的数据会不够十条.但是分页出来的总数据不变.
... ... @@ -57,10 +57,11 @@ public class UserController {
@RequestParam(value = "pageSize",required = false,defaultValue = "5")
int pageSize,
@RequestParam(value = "userName",required = false) String username,
@RequestParam(value = "realName",required = false) String realname)
@RequestParam(value = "realName",required = false) String realname,
@RequestParam(value = "companyId",required = false) Integer companyId)
{
PageInfo<USERS> usersPageInfo = userService.selectAllUser(pageNum,pageSize, username, realname);
PageInfo<USERS> usersPageInfo = userService.selectAllUser(pageNum,pageSize, username, realname,companyId);
return new ResultJson("200","success",usersPageInfo);
}
... ...
... ... @@ -28,6 +28,8 @@ public interface ROLEMapper {
List<ROLE> findAll(@Param("roleName") String roleName,
@Param("type") String type);
List<ROLE> findAllWithChildAndTree();
List<ROLE> findAllWithOutTree(@Param("roleName") String roleName,
@Param("type") String type);
... ...
... ... @@ -10,7 +10,7 @@ import java.util.List;
public interface UserService {
USERS loadByUsername(String username);
PageInfo<USERS> selectAllUser(int pageNum, int pageSize,String username, String realName);
PageInfo<USERS> selectAllUser(int pageNum, int pageSize,String username, String realName,Integer companyId);
int updateByPrimaryKeySelective(USERS record);
... ...
... ... @@ -103,18 +103,19 @@ public class UserServiceImpl implements UserService{
}
@Override
public PageInfo<USERS> selectAllUser(int pageNum, int pageSize,String username, String realName){
public PageInfo<USERS> selectAllUser(int pageNum, int pageSize,String username, String realName,Integer companyId){
Page<USERS> page = PageHelper.startPage(pageNum,pageSize);
USERS users = new USERS();
users.setUsername(username);
users.setRealname(realName);
users.setCompanyId(companyId);
List<USERS> list = usersMapper.selectAllUser(users);
for (USERS user: list) {
// List<PERMISSION> permissionList = permissionMapper.findByUserId(user.getUserId());
// user.setPermissions(permissionList);
//为了前端role配置的适配
List<ROLE> roleList = roleMapper.findRolesByUserId(user.getUserId());
user.setRoles(roleList);
// List<ROLE> roleList = roleMapper.findRolesByUserId(user.getUserId());
// user.setRoles(roleList);
}
PageInfo<USERS> result = new PageInfo<USERS>(list);
return result;
... ...
... ... @@ -15,8 +15,21 @@
<result column="mq_code" jdbcType="VARCHAR" property="mqcode" />
</resultMap>
<resultMap id="TreeWithPermResultMap" type="com.tianbo.warehouse.model.ROLE" extends="BaseResultMap">
<collection column="role_id" javaType="java.util.ArrayList" ofType="com.tianbo.warehouse.model.PERMISSION" property="permissions" select="com.tianbo.warehouse.dao.PERMISSIONMapper.getRolePermisson" />
<collection column="role_id" property="children" select="selectByParentId" />
<collection javaType="java.util.ArrayList" ofType="com.tianbo.warehouse.model.PERMISSION" property="permissions">
<result column="permission_id" property="permissionId" jdbcType="INTEGER" />
<result column="name" property="name" jdbcType="VARCHAR" />
<result column="permission_order" property="permissionOrder" jdbcType="VARCHAR" />
<result column="permission_description" property="description" jdbcType="VARCHAR" />
<result column="ismenu" property="ismenu" jdbcType="BOOLEAN" />
<result column="hidden" property="hidden" jdbcType="BOOLEAN" />
<result column="parent_id" property="parentId" jdbcType="INTEGER" />
<result column="path" property="path" jdbcType="VARCHAR" />
<result column="url" property="url" jdbcType="VARCHAR" />
<result column="method" property="method" jdbcType="VARCHAR" />
<result column="iconCls" property="iconCls" jdbcType="VARCHAR" />
<result column="component" property="component" jdbcType="VARCHAR" />
</collection>
<collection column="role_id" property="children" select="selectByParentId" />
</resultMap>
<resultMap id="TreeWithResultMap" type="com.tianbo.warehouse.model.ROLE" extends="BaseResultMap">
<collection column="role_id" property="children" select="selectByParentId" />
... ... @@ -225,6 +238,65 @@ WHERE
</where>
order by rsort
</select>
<select id="findAllWithChildAndTree" resultMap="TreeWithPermResultMap">
select r.role_id,
r.role_name,
r.role_sign,
r.description,
r.`type`,
r.parentId,
r.rsort,
r.customs_reg_code,
r.business_license,
r.departmentId,
r.mq_code,
p.permission_id,
p.name,
p.permission_order,
p.description as permission_description,
p.ismenu,
p.hidden,
p.parent_id,
p.path,
p.url,
p.method,
p.iconCls,
p.component
from (
SELECT role_id,
role_name,
role_sign,
description,
`type`,
parentId,
rsort,
customs_reg_code,
business_license,
departmentId,
mq_code
FROM role
<where>
parentId = 0
<if test="roleName != '' and roleName !=null">
and role_name = #{roleName, jdbcType=VARCHAR}
</if>
<if test="type != '' and type !=null">
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>
<select id="findRolesByUserId" parameterType="java.lang.Integer" resultMap="BaseResultMap">
SELECT
R.*
... ...
... ... @@ -32,6 +32,21 @@
<result column="age" property="age" jdbcType="INTEGER" />
<result column="company_id" property="companyId" jdbcType="INTEGER" />
</resultMap>
<resultMap id="WithRoleResultMap" type="com.tianbo.warehouse.model.USERS" extends="BaseResultMap">
<collection property="roles" javaType="java.util.ArrayList" ofType="com.tianbo.warehouse.model.ROLE">
<result column="role_id" jdbcType="INTEGER" property="roleId" />
<result column="role_name" jdbcType="VARCHAR" property="roleName" />
<result column="role_sign" jdbcType="VARCHAR" property="roleSign" />
<result column="rdescription" jdbcType="VARCHAR" property="description" />
<result column="type" jdbcType="VARCHAR" property="type" />
<result column="parentId" jdbcType="INTEGER" property="parentid" />
<result column="rsort" jdbcType="INTEGER" property="rsort" />
<result column="customs_reg_code" jdbcType="VARCHAR" property="customsRegCode" />
<result column="business_license" jdbcType="VARCHAR" property="businessLicense" />
<result column="departmentId" jdbcType="VARCHAR" property="departmentid" />
<result column="mq_code" jdbcType="VARCHAR" property="mqcode" />
</collection>
</resultMap>
<resultMap id="WithRoleAndPermResultMap" type="com.tianbo.warehouse.model.USERS" extends="BaseResultMap">
<collection property="roles" javaType="java.util.ArrayList" ofType="com.tianbo.warehouse.model.ROLE">
<result column="role_id" jdbcType="INTEGER" property="roleId" />
... ... @@ -101,18 +116,41 @@
left join role_permission rp on r.role_id = rp.role_id
left join permission p on rp.permission_id = p.permission_id
</select>
<select id="selectAllUser" resultMap="BaseResultMap" parameterType="com.tianbo.warehouse.model.USERS" >
select
<!-- <include refid="Base_Column_List" />-->
user_id, username, birthday, sex, address, state, mobilePhone, creatTime,
updateTime, userFace, realName, email, age,company_id
from users
WHERE 1=1
<if test=" username != null" >
and username = #{username,jdbcType=VARCHAR}
</if>
<if test="realname != null" >
and realName = #{realname,jdbcType=VARCHAR}
<select id="selectAllUser" resultMap="WithRoleResultMap" parameterType="com.tianbo.warehouse.model.USERS" >
SELECT
u.user_id,
username,
birthday,
sex,
address,
state,
mobilePhone,
creatTime,
updateTime,
userFace,
realName,
email,
age,
company_id,
r.role_id,role_name,role_sign,r.description AS rdescription,`type`,parentId,rsort,customs_reg_code,business_license,departmentId,mq_code
FROM
(
SELECT
user_id,username,birthday,sex,address,state,mobilePhone,creatTime,updateTime,userFace,realName,email,age,company_id
FROM users
<where>
<if test=" username != null and username != ''" >
and username = #{username,jdbcType=VARCHAR}
</if>
<if test="realname != null and realname != ''" >
and realName = #{realname,jdbcType=VARCHAR}
</if>
</where>
) u
LEFT JOIN user_role ur ON u.user_id = ur.user_id
LEFT JOIN role r ON ur.role_id = r.role_id
<if test=" companyId != null and companyId != ''" >
WHERE r.role_id = #{companyId,jdbcType=INTEGER}
</if>
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
... ...
package com.tianbo.warehouse;
import com.tianbo.warehouse.WarehouseApplication;
import com.tianbo.warehouse.model.ROLE;
import com.tianbo.warehouse.security.CustomUserDetailService;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
... ... @@ -9,6 +10,10 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.test.context.junit4.SpringRunner;
import com.tianbo.warehouse.dao.ROLEMapper;
import javax.annotation.Resource;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = WarehouseApplication.class,webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
... ... @@ -18,10 +23,14 @@ public class UserTest {
@Autowired
CustomUserDetailService customUserDetailService;
@Resource
ROLEMapper roleMapper;
@Test
public void contextLoads() {
UserDetails u = customUserDetailService.loadUserByUsername("nmms");
List<ROLE> roles = roleMapper.findAllWithChildAndTree();
log.info("ok");
}
}
... ...