用户查询接口与SQL优化,增加组织机构ID查询,提供给前端通过组织机构id查询对应的用户列表,唯一缺点是这个sql当一个用户有多个组织机构时查询条数为10条时返…
…回的数据会不够十条.但是分页出来的总数据不变.
正在显示
7 个修改的文件
包含
139 行增加
和
16 行删除
@@ -57,10 +57,11 @@ public class UserController { | @@ -57,10 +57,11 @@ public class UserController { | ||
57 | @RequestParam(value = "pageSize",required = false,defaultValue = "5") | 57 | @RequestParam(value = "pageSize",required = false,defaultValue = "5") |
58 | int pageSize, | 58 | int pageSize, |
59 | @RequestParam(value = "userName",required = false) String username, | 59 | @RequestParam(value = "userName",required = false) String username, |
60 | - @RequestParam(value = "realName",required = false) String realname) | 60 | + @RequestParam(value = "realName",required = false) String realname, |
61 | + @RequestParam(value = "companyId",required = false) Integer companyId) | ||
61 | { | 62 | { |
62 | 63 | ||
63 | - PageInfo<USERS> usersPageInfo = userService.selectAllUser(pageNum,pageSize, username, realname); | 64 | + PageInfo<USERS> usersPageInfo = userService.selectAllUser(pageNum,pageSize, username, realname,companyId); |
64 | return new ResultJson("200","success",usersPageInfo); | 65 | return new ResultJson("200","success",usersPageInfo); |
65 | } | 66 | } |
66 | 67 |
@@ -28,6 +28,8 @@ public interface ROLEMapper { | @@ -28,6 +28,8 @@ 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(); | ||
32 | + | ||
31 | List<ROLE> findAllWithOutTree(@Param("roleName") String roleName, | 33 | List<ROLE> findAllWithOutTree(@Param("roleName") String roleName, |
32 | @Param("type") String type); | 34 | @Param("type") String type); |
33 | 35 |
@@ -10,7 +10,7 @@ import java.util.List; | @@ -10,7 +10,7 @@ import java.util.List; | ||
10 | public interface UserService { | 10 | public interface UserService { |
11 | USERS loadByUsername(String username); | 11 | USERS loadByUsername(String username); |
12 | 12 | ||
13 | - PageInfo<USERS> selectAllUser(int pageNum, int pageSize,String username, String realName); | 13 | + PageInfo<USERS> selectAllUser(int pageNum, int pageSize,String username, String realName,Integer companyId); |
14 | 14 | ||
15 | int updateByPrimaryKeySelective(USERS record); | 15 | int updateByPrimaryKeySelective(USERS record); |
16 | 16 |
@@ -103,18 +103,19 @@ public class UserServiceImpl implements UserService{ | @@ -103,18 +103,19 @@ public class UserServiceImpl implements UserService{ | ||
103 | } | 103 | } |
104 | 104 | ||
105 | @Override | 105 | @Override |
106 | - public PageInfo<USERS> selectAllUser(int pageNum, int pageSize,String username, String realName){ | 106 | + public PageInfo<USERS> selectAllUser(int pageNum, int pageSize,String username, String realName,Integer companyId){ |
107 | Page<USERS> page = PageHelper.startPage(pageNum,pageSize); | 107 | Page<USERS> page = PageHelper.startPage(pageNum,pageSize); |
108 | USERS users = new USERS(); | 108 | USERS users = new USERS(); |
109 | users.setUsername(username); | 109 | users.setUsername(username); |
110 | users.setRealname(realName); | 110 | users.setRealname(realName); |
111 | + users.setCompanyId(companyId); | ||
111 | List<USERS> list = usersMapper.selectAllUser(users); | 112 | List<USERS> list = usersMapper.selectAllUser(users); |
112 | for (USERS user: list) { | 113 | for (USERS user: list) { |
113 | // List<PERMISSION> permissionList = permissionMapper.findByUserId(user.getUserId()); | 114 | // List<PERMISSION> permissionList = permissionMapper.findByUserId(user.getUserId()); |
114 | // user.setPermissions(permissionList); | 115 | // user.setPermissions(permissionList); |
115 | //为了前端role配置的适配 | 116 | //为了前端role配置的适配 |
116 | - List<ROLE> roleList = roleMapper.findRolesByUserId(user.getUserId()); | ||
117 | - user.setRoles(roleList); | 117 | +// List<ROLE> roleList = roleMapper.findRolesByUserId(user.getUserId()); |
118 | +// user.setRoles(roleList); | ||
118 | } | 119 | } |
119 | PageInfo<USERS> result = new PageInfo<USERS>(list); | 120 | PageInfo<USERS> result = new PageInfo<USERS>(list); |
120 | return result; | 121 | return result; |
@@ -15,7 +15,20 @@ | @@ -15,7 +15,20 @@ | ||
15 | <result column="mq_code" jdbcType="VARCHAR" property="mqcode" /> | 15 | <result column="mq_code" jdbcType="VARCHAR" property="mqcode" /> |
16 | </resultMap> | 16 | </resultMap> |
17 | <resultMap id="TreeWithPermResultMap" type="com.tianbo.warehouse.model.ROLE" extends="BaseResultMap"> | 17 | <resultMap id="TreeWithPermResultMap" type="com.tianbo.warehouse.model.ROLE" extends="BaseResultMap"> |
18 | - <collection column="role_id" javaType="java.util.ArrayList" ofType="com.tianbo.warehouse.model.PERMISSION" property="permissions" select="com.tianbo.warehouse.dao.PERMISSIONMapper.getRolePermisson" /> | 18 | + <collection javaType="java.util.ArrayList" ofType="com.tianbo.warehouse.model.PERMISSION" property="permissions"> |
19 | + <result column="permission_id" property="permissionId" jdbcType="INTEGER" /> | ||
20 | + <result column="name" property="name" jdbcType="VARCHAR" /> | ||
21 | + <result column="permission_order" property="permissionOrder" jdbcType="VARCHAR" /> | ||
22 | + <result column="permission_description" property="description" jdbcType="VARCHAR" /> | ||
23 | + <result column="ismenu" property="ismenu" jdbcType="BOOLEAN" /> | ||
24 | + <result column="hidden" property="hidden" jdbcType="BOOLEAN" /> | ||
25 | + <result column="parent_id" property="parentId" jdbcType="INTEGER" /> | ||
26 | + <result column="path" property="path" jdbcType="VARCHAR" /> | ||
27 | + <result column="url" property="url" jdbcType="VARCHAR" /> | ||
28 | + <result column="method" property="method" jdbcType="VARCHAR" /> | ||
29 | + <result column="iconCls" property="iconCls" jdbcType="VARCHAR" /> | ||
30 | + <result column="component" property="component" jdbcType="VARCHAR" /> | ||
31 | + </collection> | ||
19 | <collection column="role_id" property="children" select="selectByParentId" /> | 32 | <collection column="role_id" property="children" select="selectByParentId" /> |
20 | </resultMap> | 33 | </resultMap> |
21 | <resultMap id="TreeWithResultMap" type="com.tianbo.warehouse.model.ROLE" extends="BaseResultMap"> | 34 | <resultMap id="TreeWithResultMap" type="com.tianbo.warehouse.model.ROLE" extends="BaseResultMap"> |
@@ -225,6 +238,65 @@ WHERE | @@ -225,6 +238,65 @@ WHERE | ||
225 | </where> | 238 | </where> |
226 | order by rsort | 239 | order by rsort |
227 | </select> | 240 | </select> |
241 | + | ||
242 | + <select id="findAllWithChildAndTree" resultMap="TreeWithPermResultMap"> | ||
243 | + select r.role_id, | ||
244 | + r.role_name, | ||
245 | + r.role_sign, | ||
246 | + r.description, | ||
247 | + r.`type`, | ||
248 | + r.parentId, | ||
249 | + r.rsort, | ||
250 | + r.customs_reg_code, | ||
251 | + r.business_license, | ||
252 | + r.departmentId, | ||
253 | + r.mq_code, | ||
254 | + p.permission_id, | ||
255 | + p.name, | ||
256 | + p.permission_order, | ||
257 | + p.description as permission_description, | ||
258 | + p.ismenu, | ||
259 | + p.hidden, | ||
260 | + p.parent_id, | ||
261 | + p.path, | ||
262 | + p.url, | ||
263 | + p.method, | ||
264 | + p.iconCls, | ||
265 | + p.component | ||
266 | + from ( | ||
267 | + SELECT role_id, | ||
268 | + role_name, | ||
269 | + role_sign, | ||
270 | + description, | ||
271 | + `type`, | ||
272 | + parentId, | ||
273 | + rsort, | ||
274 | + customs_reg_code, | ||
275 | + business_license, | ||
276 | + departmentId, | ||
277 | + mq_code | ||
278 | + FROM role | ||
279 | + <where> | ||
280 | + parentId = 0 | ||
281 | + <if test="roleName != '' and roleName !=null"> | ||
282 | + and role_name = #{roleName, jdbcType=VARCHAR} | ||
283 | + </if> | ||
284 | + <if test="type != '' and type !=null"> | ||
285 | + and type like '%' #{type} '%' | ||
286 | + </if> | ||
287 | + </where> | ||
288 | + order by rsort | ||
289 | + limit 5 | ||
290 | + ) r | ||
291 | + left join role_permission rp | ||
292 | + on r.role_id = rp.role_id | ||
293 | + left join permission p | ||
294 | + 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 | ||
297 | + | ||
298 | + </select> | ||
299 | + | ||
228 | <select id="findRolesByUserId" parameterType="java.lang.Integer" resultMap="BaseResultMap"> | 300 | <select id="findRolesByUserId" parameterType="java.lang.Integer" resultMap="BaseResultMap"> |
229 | SELECT | 301 | SELECT |
230 | R.* | 302 | R.* |
@@ -32,6 +32,21 @@ | @@ -32,6 +32,21 @@ | ||
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="WithRoleResultMap" 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 | + </resultMap> | ||
35 | <resultMap id="WithRoleAndPermResultMap" type="com.tianbo.warehouse.model.USERS" extends="BaseResultMap"> | 50 | <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"> | 51 | <collection property="roles" javaType="java.util.ArrayList" ofType="com.tianbo.warehouse.model.ROLE"> |
37 | <result column="role_id" jdbcType="INTEGER" property="roleId" /> | 52 | <result column="role_id" jdbcType="INTEGER" property="roleId" /> |
@@ -101,19 +116,42 @@ | @@ -101,19 +116,42 @@ | ||
101 | left join role_permission rp on r.role_id = rp.role_id | 116 | left join role_permission rp on r.role_id = rp.role_id |
102 | left join permission p on rp.permission_id = p.permission_id | 117 | left join permission p on rp.permission_id = p.permission_id |
103 | </select> | 118 | </select> |
104 | - <select id="selectAllUser" resultMap="BaseResultMap" parameterType="com.tianbo.warehouse.model.USERS" > | ||
105 | - select | ||
106 | -<!-- <include refid="Base_Column_List" />--> | ||
107 | - user_id, username, birthday, sex, address, state, mobilePhone, creatTime, | ||
108 | - updateTime, userFace, realName, email, age,company_id | ||
109 | - from users | ||
110 | - WHERE 1=1 | ||
111 | - <if test=" username != null" > | 119 | + <select id="selectAllUser" resultMap="WithRoleResultMap" parameterType="com.tianbo.warehouse.model.USERS" > |
120 | + SELECT | ||
121 | + u.user_id, | ||
122 | + username, | ||
123 | + birthday, | ||
124 | + sex, | ||
125 | + address, | ||
126 | + state, | ||
127 | + mobilePhone, | ||
128 | + creatTime, | ||
129 | + updateTime, | ||
130 | + userFace, | ||
131 | + realName, | ||
132 | + email, | ||
133 | + age, | ||
134 | + company_id, | ||
135 | + r.role_id,role_name,role_sign,r.description AS rdescription,`type`,parentId,rsort,customs_reg_code,business_license,departmentId,mq_code | ||
136 | + FROM | ||
137 | + ( | ||
138 | + SELECT | ||
139 | + user_id,username,birthday,sex,address,state,mobilePhone,creatTime,updateTime,userFace,realName,email,age,company_id | ||
140 | + FROM users | ||
141 | + <where> | ||
142 | + <if test=" username != null and username != ''" > | ||
112 | and username = #{username,jdbcType=VARCHAR} | 143 | and username = #{username,jdbcType=VARCHAR} |
113 | </if> | 144 | </if> |
114 | - <if test="realname != null" > | 145 | + <if test="realname != null and realname != ''" > |
115 | and realName = #{realname,jdbcType=VARCHAR} | 146 | and realName = #{realname,jdbcType=VARCHAR} |
116 | </if> | 147 | </if> |
148 | + </where> | ||
149 | + ) u | ||
150 | + LEFT JOIN user_role ur ON u.user_id = ur.user_id | ||
151 | + LEFT JOIN role r ON ur.role_id = r.role_id | ||
152 | + <if test=" companyId != null and companyId != ''" > | ||
153 | + WHERE r.role_id = #{companyId,jdbcType=INTEGER} | ||
154 | + </if> | ||
117 | </select> | 155 | </select> |
118 | <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" > | 156 | <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" > |
119 | delete from users | 157 | delete from users |
1 | package com.tianbo.warehouse; | 1 | package com.tianbo.warehouse; |
2 | 2 | ||
3 | import com.tianbo.warehouse.WarehouseApplication; | 3 | import com.tianbo.warehouse.WarehouseApplication; |
4 | +import com.tianbo.warehouse.model.ROLE; | ||
4 | import com.tianbo.warehouse.security.CustomUserDetailService; | 5 | import com.tianbo.warehouse.security.CustomUserDetailService; |
5 | import lombok.extern.slf4j.Slf4j; | 6 | import lombok.extern.slf4j.Slf4j; |
6 | import org.junit.Test; | 7 | import org.junit.Test; |
@@ -9,6 +10,10 @@ import org.springframework.beans.factory.annotation.Autowired; | @@ -9,6 +10,10 @@ import org.springframework.beans.factory.annotation.Autowired; | ||
9 | import org.springframework.boot.test.context.SpringBootTest; | 10 | import org.springframework.boot.test.context.SpringBootTest; |
10 | import org.springframework.security.core.userdetails.UserDetails; | 11 | import org.springframework.security.core.userdetails.UserDetails; |
11 | import org.springframework.test.context.junit4.SpringRunner; | 12 | import org.springframework.test.context.junit4.SpringRunner; |
13 | +import com.tianbo.warehouse.dao.ROLEMapper; | ||
14 | + | ||
15 | +import javax.annotation.Resource; | ||
16 | +import java.util.List; | ||
12 | 17 | ||
13 | @RunWith(SpringRunner.class) | 18 | @RunWith(SpringRunner.class) |
14 | @SpringBootTest(classes = WarehouseApplication.class,webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) | 19 | @SpringBootTest(classes = WarehouseApplication.class,webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) |
@@ -18,10 +23,14 @@ public class UserTest { | @@ -18,10 +23,14 @@ public class UserTest { | ||
18 | @Autowired | 23 | @Autowired |
19 | CustomUserDetailService customUserDetailService; | 24 | CustomUserDetailService customUserDetailService; |
20 | 25 | ||
26 | + @Resource | ||
27 | + ROLEMapper roleMapper; | ||
28 | + | ||
21 | @Test | 29 | @Test |
22 | public void contextLoads() { | 30 | public void contextLoads() { |
23 | 31 | ||
24 | UserDetails u = customUserDetailService.loadUserByUsername("nmms"); | 32 | UserDetails u = customUserDetailService.loadUserByUsername("nmms"); |
33 | + List<ROLE> roles = roleMapper.findAllWithChildAndTree(); | ||
25 | log.info("ok"); | 34 | log.info("ok"); |
26 | } | 35 | } |
27 | } | 36 | } |
-
请 注册 或 登录 后发表评论