学习mybatisplus使用CURD方法进行快速开发
程序员文章站
2024-01-05 21:01:16
...
mybatis-plus 3.4版本 如何优雅使用CURD
目前最新的版本
pom中配置
com.baomidou
mybatis-plus-boot-starter
3.4.0
准备分页工具
PageUtils
public class PageUtils implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 总记录数
*/
private int totalCount;
/**
* 每页记录数
*/
private int pageSize;
/**
* 总页数
*/
private int totalPage;
/**
* 当前页数
*/
private int currPage;
/**
* 列表数据
*/
private List<?> list;
/**
* 分页
* @param list 列表数据
* @param totalCount 总记录数
* @param pageSize 每页记录数
* @param currPage 当前页数
*/
public PageUtils(List<?> list, int totalCount, int pageSize, int currPage) {
this.list = list;
this.totalCount = totalCount;
this.pageSize = pageSize;
this.currPage = currPage;
this.totalPage = (int)Math.ceil((double)totalCount/pageSize);
}
/**
* 分页
*/
public PageUtils(IPage<?> page) {
this.list = page.getRecords();
this.totalCount = (int)page.getTotal();
this.pageSize = (int)page.getSize();
this.currPage = (int)page.getCurrent();
this.totalPage = (int)page.getPages();
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getCurrPage() {
return currPage;
}
public void setCurrPage(int currPage) {
this.currPage = currPage;
}
public List<?> getList() {
return list;
}
public void setList(List<?> list) {
this.list = list;
}
}
封装查询参数
/**
* 查询参数
*
*/
public class Query<T> {
public IPage<T> getPage(Map<String, Object> params) {
return this.getPage(params, null, false);
}
public IPage<T> getPage(Map<String, Object> params, String defaultOrderField, boolean isAsc) {
//分页参数
long curPage = 1;
long limit = 10;
if(params.get(Constant.PAGE) != null){
curPage = Long.parseLong((String)params.get(Constant.PAGE));
}
if(params.get(Constant.LIMIT) != null){
limit = Long.parseLong((String)params.get(Constant.LIMIT));
}
//分页对象
Page<T> page = new Page<>(curPage, limit);
//分页参数
params.put(Constant.PAGE, page);
//排序字段
//防止SQL注入(因为sidx、order是通过拼接SQL实现排序的,会有SQL注入风险)
//String orderField = SQLFilter.sqlInject((String)params.get(Constant.ORDER_FIELD));
String order = (String)params.get(Constant.ORDER);
//前端字段排序
/* if(StringUtils.isNotEmpty(orderField) && StringUtils.isNotEmpty(order)){
if(Constant.ASC.equalsIgnoreCase(order)) {
return page.addOrder(OrderItem.asc(orderField));
}else {
return page.addOrder(OrderItem.desc(orderField));
}
}
*/
//没有排序字段,则不排序
if(StringUtils.isBlank(defaultOrderField)){
return page;
}
//默认排序
if(isAsc) {
page.addOrder(OrderItem.asc(defaultOrderField));
}else {
page.addOrder(OrderItem.desc(defaultOrderField));
}
return page;
}
}
常量
/**
* 常量
*
*
*/
public class Constant {
/** 超级管理员ID */
public static final int SUPER_ADMIN = 1;
/**
* 当前页码
*/
public static final String PAGE = "page";
/**
* 每页显示记录数
*/
public static final String LIMIT = "limit";
/**
* 排序字段
*/
public static final String ORDER_FIELD = "sidx";
/**
* 排序方式
*/
public static final String ORDER = "order";
/**
* 升序
*/
public static final String ASC = "asc";
}
Controller
/**
* 列表 这里前端传递分页参数是分别是
* Constant中封装的常量 、
* curPage :当前页
* limit :分页数
* ()
*/
@RequestMapping("/list")
//@RequiresPermissions("product:attrattrgrouprelation:list")
public R list(@RequestParam Map<String, Object> params){
PageUtils page = attrAttrgroupRelationService.queryPage(params);
return R.ok().put("page", page);
}
service
@Override
public PageUtils queryPage(Map<String, Object> params) {
IPage<AttrAttrgroupRelationEntity> page = this.page(
new Query<AttrAttrgroupRelationEntity>().getPage(params),
new QueryWrapper<AttrAttrgroupRelationEntity>()
);
return new PageUtils(page);
}
传递查询参数进行分页查询
这里使用mybatisplus的方法可以免去手写sql
从params中获取前端传递的查询参数,调用QueryWrapper封装的方法传入列明和值进行查询
@Override
public PageUtils queryPage(Map<String, Object> params) {
String roleName = (String)params.get("roleName");
Long createUserId = (Long)params.get("createUserId");
IPage<SysRoleEntity> page = this.page(
new Query<SysRoleEntity>().getPage(params),
new QueryWrapper<SysRoleEntity>()
.like(StringUtils.isNotBlank(roleName),"role_name", roleName)
.eq(createUserId != null,"create_user_id", createUserId)
);
return new PageUtils(page);
}
以上sql select * from sys_role where role_name like "%xxx%" and create_user_id=x
limit 10
快速使用CURD
进入mybatis官网
https://baomidou.com/guide/crud-interface.html
因为是中国人搞的mabtis-plus所有全是中文文档爽歪歪