封装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> <artifactId>mysql-connector-java</artifactId>
</dependency> </dependency>
<!-- mybatis-plus start --> <!-- mybatis-plus start -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>2.1.4</version>
</dependency>
<dependency> <dependency>
<groupId>com.baomidou</groupId> <groupId>com.baomidou</groupId>
<artifactId>mybatisplus-spring-boot-starter</artifactId> <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.entity.SysDict;
import com.github.pig.admin.service.SysDictService; import com.github.pig.admin.service.SysDictService;
import com.github.pig.common.constant.CommonConstant; import com.github.pig.common.constant.CommonConstant;
import com.github.pig.common.util.Query;
import com.github.pig.common.web.BaseController; import com.github.pig.common.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable; import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
* <p> * <p>
@ -44,17 +43,13 @@ public class DictController extends BaseController {
/** /**
* 分页查询字典信息 * 分页查询字典信息
* *
* @param page 分页对象 * @param params 分页对象
* @param limit 每页限制
* @return 分页对象 * @return 分页对象
*/ */
@RequestMapping("/dictPage") @RequestMapping("/dictPage")
public Page dictPage(Integer page, Integer limit) { public Page dictPage(@RequestParam Map<String, Object> params) {
SysDict condition = new SysDict(); params.put(CommonConstant.DEL_FLAG, CommonConstant.STATUS_NORMAL);
condition.setDelFlag(CommonConstant.STATUS_NORMAL); return sysDictService.selectPage(new Query<>(params),new EntityWrapper<>());
EntityWrapper wrapper = new EntityWrapper(condition);
wrapper.orderBy("createTime", false);
return sysDictService.selectPage(new Page<>(page, limit), wrapper);
} }
/** /**

View File

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

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

View File

@ -1,11 +1,12 @@
package com.github.pig.admin.mapper; package com.github.pig.admin.mapper;
import com.baomidou.mybatisplus.mapper.BaseMapper; import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.github.pig.admin.entity.SysUser; import com.github.pig.admin.entity.SysUser;
import com.github.pig.common.util.Query;
import com.github.pig.common.vo.UserVo; import com.github.pig.common.vo.UserVo;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
* <p> * <p>
@ -27,9 +28,8 @@ public interface SysUserMapper extends BaseMapper<SysUser> {
/** /**
* 分页查询用户信息含角色 * 分页查询用户信息含角色
* *
* @param page * @param query 查询条件
* @param sysUser
* @return * @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.plugins.Page;
import com.baomidou.mybatisplus.service.IService; import com.baomidou.mybatisplus.service.IService;
import com.github.pig.admin.entity.SysUser; import com.github.pig.admin.entity.SysUser;
import com.github.pig.common.util.Query;
import com.github.pig.common.vo.UserVo; import com.github.pig.common.vo.UserVo;
/** /**
@ -21,11 +22,10 @@ public interface UserService extends IService<SysUser> {
/** /**
* 分页查询用户信息含有角色信息 * 分页查询用户信息含有角色信息
* *
* @param page 分页对象 * @param query 查询条件
* @param sysUser 查询条件
* @return * @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.entity.SysUser;
import com.github.pig.admin.mapper.SysUserMapper; import com.github.pig.admin.mapper.SysUserMapper;
import com.github.pig.admin.service.UserService; import com.github.pig.admin.service.UserService;
import com.github.pig.common.util.Query;
import com.github.pig.common.vo.UserVo; import com.github.pig.common.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.CacheEvict;
@ -27,9 +28,9 @@ public class UserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impleme
} }
@Override @Override
public Page selectWithRolePage(Page<UserVo> page, SysUser sysUser) { public Page selectWithRolePage(Query query) {
page.setRecords(sysUserMapper.selectUserVoPage(page, sysUser)); query.setRecords(sysUserMapper.selectUserVoPage(query,query.getCondition()));
return page; return query;
} }
@Override @Override

View File

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

View File

@ -41,4 +41,9 @@ public interface CommonConstant {
* log rabbit队列名称 * log rabbit队列名称
*/ */
String LOG_QUEUE = "log"; 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 * @date 2017/10/28
*/ */
public class BaseController { public class BaseController {
public org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(this.getClass());
@Autowired @Autowired
private HttpServletRequest request; private HttpServletRequest request;
protected org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(this.getClass());
/** /**
* 根据请求heard中的token获取用户角色 * 根据请求heard中的token获取用户角色