封装mybatis 分页查询,优化入参。

This commit is contained in:
wangiegie@gmail.com 2017-12-11 00:14:08 +08:00
parent 5c9fde0a92
commit 4c672e2700
13 changed files with 143 additions and 46 deletions

View File

@ -29,11 +29,6 @@
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- mybatis-plus start -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatisplus-spring-boot-starter</artifactId>

View File

@ -6,15 +6,14 @@ import com.baomidou.mybatisplus.plugins.Page;
import com.github.pig.admin.entity.SysDict;
import com.github.pig.admin.service.SysDictService;
import com.github.pig.common.constant.CommonConstant;
import com.github.pig.common.util.Query;
import com.github.pig.common.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;
/**
* <p>
@ -44,17 +43,13 @@ public class DictController extends BaseController {
/**
* 分页查询字典信息
*
* @param page 分页对象
* @param limit 每页限制
* @param params 分页对象
* @return 分页对象
*/
@RequestMapping("/dictPage")
public Page dictPage(Integer page, Integer limit) {
SysDict condition = new SysDict();
condition.setDelFlag(CommonConstant.STATUS_NORMAL);
EntityWrapper wrapper = new EntityWrapper(condition);
wrapper.orderBy("createTime", false);
return sysDictService.selectPage(new Page<>(page, limit), wrapper);
public Page dictPage(@RequestParam Map<String, Object> params) {
params.put(CommonConstant.DEL_FLAG, CommonConstant.STATUS_NORMAL);
return sysDictService.selectPage(new Query<>(params),new EntityWrapper<>());
}
/**

View File

@ -6,12 +6,14 @@ import com.baomidou.mybatisplus.plugins.Page;
import com.github.pig.admin.service.SysLogService;
import com.github.pig.common.constant.CommonConstant;
import com.github.pig.common.entity.SysLog;
import com.github.pig.common.util.Query;
import com.github.pig.common.util.UserUtils;
import com.github.pig.common.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.Map;
/**
* <p>
@ -36,17 +38,13 @@ public class LogController extends BaseController {
/**
* 分页查询日志信息
*
* @param page 分页对象
* @param limit 每页限制
* @param params 分页对象
* @return 分页对象
*/
@RequestMapping("/logPage")
public Page logPage(Integer page, Integer limit) {
SysLog condition = new SysLog();
condition.setDelFlag(CommonConstant.STATUS_NORMAL);
EntityWrapper wrapper = new EntityWrapper(condition);
wrapper.orderBy("createTime", false);
return sysLogService.selectPage(new Page<>(page, limit), wrapper);
public Page logPage(@RequestParam Map<String, Object> params) {
params.put(CommonConstant.DEL_FLAG, CommonConstant.STATUS_NORMAL);
return sysLogService.selectPage(new Query<>(params),new EntityWrapper<>());
}
/**

View File

@ -7,6 +7,7 @@ import com.github.pig.admin.entity.SysRoleMenu;
import com.github.pig.admin.service.SysRoleMenuService;
import com.github.pig.admin.service.SysRoleService;
import com.github.pig.common.constant.CommonConstant;
import com.github.pig.common.util.Query;
import com.github.pig.common.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
@ -14,6 +15,7 @@ import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* @author lengleng
@ -85,15 +87,13 @@ public class RoleController extends BaseController {
/**
* 分页查询角色信息
*
* @param page 分页对象
* @param limit 每页限制
* @param params 分页对象
* @return 分页对象
*/
@RequestMapping("/rolePage")
public Page rolePage(Integer page, Integer limit) {
SysRole condition = new SysRole();
condition.setDelFlag(CommonConstant.STATUS_NORMAL);
return sysRoleService.selectPage(new Page<>(page, limit), new EntityWrapper<>(condition));
public Page rolePage(@RequestParam Map<String, Object> params) {
params.put(CommonConstant.DEL_FLAG, CommonConstant.STATUS_NORMAL);
return sysRoleService.selectPage(new Query<>(params),new EntityWrapper<>());
}
/**
@ -104,7 +104,7 @@ public class RoleController extends BaseController {
* @return successfalse
*/
@PutMapping("/roleMenuUpd")
@CacheEvict(value = "menu_details",allEntries = true)
@CacheEvict(value = "menu_details", allEntries = true)
public Boolean roleMenuUpd(Integer roleId, @RequestParam("menuIds[]") Integer[] menuIds) {
SysRoleMenu condition = new SysRoleMenu();
condition.setRoleId(roleId);

View File

@ -11,6 +11,7 @@ import com.github.pig.admin.service.SysMenuService;
import com.github.pig.admin.service.SysUserRoleService;
import com.github.pig.admin.service.UserService;
import com.github.pig.common.constant.CommonConstant;
import com.github.pig.common.util.Query;
import com.github.pig.common.util.UserUtils;
import com.github.pig.common.vo.UserVo;
import com.github.pig.common.web.BaseController;
@ -165,8 +166,8 @@ public class UserController extends BaseController {
* @return 用户集合
*/
@RequestMapping("/userPage")
public Page userPage(Integer page, Integer limit, SysUser sysUser) {
return userService.selectWithRolePage(new Page<>(page, limit), sysUser);
public Page userPage(@RequestParam Map<String, Object> params) {
return userService.selectWithRolePage(new Query(params));
}
/**

View File

@ -1,11 +1,12 @@
package com.github.pig.admin.mapper;
import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.github.pig.admin.entity.SysUser;
import com.github.pig.common.util.Query;
import com.github.pig.common.vo.UserVo;
import java.util.List;
import java.util.Map;
/**
* <p>
@ -27,9 +28,8 @@ public interface SysUserMapper extends BaseMapper<SysUser> {
/**
* 分页查询用户信息含角色
*
* @param page
* @param sysUser
* @param query 查询条件
* @return
*/
List selectUserVoPage(Page<UserVo> page, SysUser sysUser);
List selectUserVoPage(Query query, Map<String,Object> params);
}

View File

@ -3,6 +3,7 @@ package com.github.pig.admin.service;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.IService;
import com.github.pig.admin.entity.SysUser;
import com.github.pig.common.util.Query;
import com.github.pig.common.vo.UserVo;
/**
@ -21,11 +22,10 @@ public interface UserService extends IService<SysUser> {
/**
* 分页查询用户信息含有角色信息
*
* @param page 分页对象
* @param sysUser 查询条件
* @param query 查询条件
* @return
*/
Page selectWithRolePage(Page<UserVo> page, SysUser sysUser);
Page selectWithRolePage(Query query);
/**
* 清除用户缓存

View File

@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.github.pig.admin.entity.SysUser;
import com.github.pig.admin.mapper.SysUserMapper;
import com.github.pig.admin.service.UserService;
import com.github.pig.common.util.Query;
import com.github.pig.common.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
@ -27,9 +28,9 @@ public class UserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impleme
}
@Override
public Page selectWithRolePage(Page<UserVo> page, SysUser sysUser) {
page.setRecords(sysUserMapper.selectUserVoPage(page, sysUser));
return page;
public Page selectWithRolePage(Query query) {
query.setRecords(sysUserMapper.selectUserVoPage(query,query.getCondition()));
return query;
}
@Override

View File

@ -56,6 +56,12 @@
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>1.16</version>
</dependency>
<!--mybatis-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>2.1.4</version>
</dependency>
</dependencies>

View File

@ -41,4 +41,9 @@ public interface CommonConstant {
* log rabbit队列名称
*/
String LOG_QUEUE = "log";
/**
* 删除标记
*/
String DEL_FLAG = "del_flag";
}

View File

@ -0,0 +1,35 @@
package com.github.pig.common.util;
import com.baomidou.mybatisplus.plugins.Page;
import org.apache.commons.lang.StringUtils;
import java.util.Map;
/**
* @author lengleng
* @date 2017/12/10
*/
public class Query<T> extends Page<T> {
private static final String PAGE = "page";
private static final String LIMIT = "limit";
private static final String ORDER_BY_FIELD = "orderByField";
private static final String IS_ASC = "isAsc";
public Query(Map<String, Object> params) {
super(Integer.parseInt(params.getOrDefault(PAGE, 1).toString())
, Integer.parseInt(params.getOrDefault(LIMIT, 10).toString()));
params.remove(PAGE);
params.remove(LIMIT);
params.remove(ORDER_BY_FIELD);
params.remove(IS_ASC);
this.setCondition(params);
String orderByField = params.getOrDefault(ORDER_BY_FIELD, "").toString();
if (StringUtils.isNotEmpty(orderByField)) {
this.setOrderByField(orderByField);
}
Boolean isAsc = Boolean.parseBoolean(params.getOrDefault(IS_ASC, Boolean.TRUE).toString());
this.setAsc(isAsc);
}
}

View File

@ -0,0 +1,62 @@
package com.github.pig.common.util;
import java.io.Serializable;
public class R<T> implements Serializable {
private static final long serialVersionUID = 1L;
public static final int NO_LOGIN = -1;
public static final int SUCCESS = 0;
public static final int FAIL = 1;
public static final int NO_PERMISSION = 2;
private String msg = "success";
private int code = SUCCESS;
private T data;
public R() {
super();
}
public R(T data) {
super();
this.data = data;
}
public R(Throwable e) {
super();
this.msg = e.toString();
this.code = FAIL;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
}

View File

@ -15,10 +15,9 @@ import java.util.List;
* @date 2017/10/28
*/
public class BaseController {
public org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(this.getClass());
@Autowired
private HttpServletRequest request;
protected org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(this.getClass());
/**
* 根据请求heard中的token获取用户角色