1. 数据库映射对象与视图对象
笔者在开发过程中在面向客户端展示时都是使用二次封装的视图对象(VO)来进行内容展示.
package com.fod.fodapi.vo; import lombok.Data; /** * 用户视图对象 * @author 86137 * @date 2021-09-26 15:03 */ @Data public class UrmUserInfoVO { /** * 用户主键 */ private Integer id; /** * 用户账号 */ private String userNumber; /** * 用户名称 */ private String userName; /** * 用户头像 */ private String userImage; /** * 用户性别 */ private Integer userSex; /** * 联系方式 */ private String userPhone; /** * 状态 */ private Integer status; /* * 用户角色 * */ private String userRole; }
2. 测试SQL
测试sql是在数据库客户端进行数据测试时进行编写,确保在编写代码时不会出现sql查询错误
SELECT users.id, users.user_number, users.user_name, users.user_image, users.user_sex, users.user_phone, users.native_place, users.status, roles.role_name FROM urm_user_info AS users JOIN urm_user_online AS onlines ON users.id = onlines.user_info JOIN urm_user_role_relevance AS userAndRole ON users.id = userAndRole.user_id LEFT JOIN urm_role_info AS roles ON userAndRole.role_id = roles.id WHERE users.STATUS = 1 AND users.deleted = 0 AND onlines.online_status = 1 ORDER BY users.add_time
测试结果:
3. MVC层分解
3.1 查询参数封装
查询过程中部分参数是需要重复使用比如:当前页,当前页的数量,筛选条件等等。同时也可以解决查询参数过多导致接口层接收数据的代码过多。
公共使用的字段封装:(类名:PublicSelect)
private Integer page = 1; //第几页 private Integer limit = 10; // 每页多少条数据 private String sort = "add_time"; // 排序字段 private Boolean order = true; // 排序方式(true:asc,false:desc)
特定功能查询字段封装:(类名:UrmUserInfoSelect )
在使用公共字段的时候继承一下即可
/** * 用户查询条件 */ @Data public class UrmUserInfoSelect extends PublicSelect{ /**用户账号*/ private String userNumber; /**用户名称*/ private String userName; /**用户性别*/ private Integer userSex; /**联系方式*/ private String userPhone; /** 状态*/ private Integer status; /**新增时间*/ private Date addTime; }
3.2 Controller层代码
笔者在controller层只用做数据接收,数据的基本过滤验证,以及返回操作,业务全部在接口实现层里面(serverImpl)
@ApiOperation(value = "人员在线列表") @GetMapping("/list") @ResponseBody public Object listSelective(@RequestBody UrmUserInfoSelect select){ return rdmPersonOnlineService.listSelective(select); }
3.3 Service接口层
public interface RdmPersonOnlineService { Object listSelective(UrmUserInfoSelect select); }
3.4 ServiceImpl接口实现层
在这个类里面将编写所有与业务有关的内容
@Override public Object listSelective(UrmUserInfoSelect select) { // QueryWrapper用于构建sql的过滤数据条件内容,详细看Mybatis-Plus官方文档即可 QueryWrappervoQueryWrapper = new QueryWrapper<>(); voQueryWrapper.eq("users.status",STATUS_START); voQueryWrapper.eq("users.deleted",DELETED_NO); voQueryWrapper.eq("onlines.online_status",USER_ONLINE); voQueryWrapper.orderBy(true, select.getOrder(), select.getSort()); // 构建分页条件,通过Page自动进行分页操作 Page voPage = new Page<>(select.getPage(),select.getLimit()); // setRecords是Page类里面的一个放置查询结果的参数 voPage.setRecords( urmUserInfoMapper.selectiveUserInfoByOnlineStatus(voPage,voQueryWrapper)); return ResponseUtil.ok(voPage); }
3.5 Mapper数据持久层
在mypper层编写sql时:在方法里面的条件参数中必须加上**@Param(Constants.WRAPPER),在Sql末尾必须加上${ew.customSqlSegment}**否在定义的条件无效。由于筛选条件(查询条件)在条件参数中已经配置完成,在mapper的sql里面就不需要再次写入。
@Mapper @Repository public interface UrmUserInfoMapper extends BaseMapper{ // 出现的+为回车换行 @Select("SELECT " + "users.id, users.user_number, users.user_name, users.user_image, users.user_sex, users.user_phone, users.native_place, users.status, roles.role_name " + "FROM " + "urm_user_info AS users " + "JOIN urm_user_online AS onlines ON users.id = onlines.user_info " + "JOIN urm_user_role_relevance AS userAndRole ON users.id = userAndRole.user_id " + "LEFT JOIN urm_role_info AS roles ON userAndRole.role_id = roles.id ${ew.customSqlSegment}") List selectiveUserInfoByOnlineStatus(Page voPage, @Param(Constants.WRAPPER)QueryWrapper voQueryWrapper); }
4. 结果
筛选条件
{ "page":1, "limit":5, "sort": null, "order":true, "userNumber":null, "userName":null, "userSex": null, "userPhone": null, "status": null, "addTime":null }
筛选结果:
{ "errno": 0, "data": { "records": [ { "id": 1, "userNumber": "admin", "userName": "admin", "userImage": null, "userSex": 1, "userPhone": "1234567890123", "status": 1, "userRole": null, "userOrganize": null }, { "id": 2, "userNumber": "123456", "userName": "张三", "userImage": null, "userSex": 0, "userPhone": "1234567890123", "status": 1, "userRole": null, "userOrganize": null }, { "id": 3, "userNumber": "123456789", "userName": "李四", "userImage": null, "userSex": 1, "userPhone": "1234567890123", "status": 1, "userRole": null, "userOrganize": null }, { "id": 4, "userNumber": "123123", "userName": "王五", "userImage": null, "userSex": 1, "userPhone": "1234567890123", "status": 1, "userRole": null, "userOrganize": null }, { "id": 5, "userNumber": "12121212", "userName": "马六", "userImage": null, "userSex": 1, "userPhone": "1234567890123", "status": 1, "userRole": null, "userOrganize": null } ], "total": 6, "size": 5, "current": 1, "orders": [], "optimizeCountSql": true, "hitCount": false, "countId": null, "maxLimit": null, "searchCount": true, "pages": 2 }, "errmsg": "成功" }
图示:
5 补充
5.1 分页失效问题
分页时出现查询出来的都是所有数据,并不会进行分页。原因是mybatis-plus配置出现问题。
不同版本可能会出现配置差异(笔者使用的是3.4.1)
@Configuration(proxyBeanMethods = false) public class MybatisPlusConfig { @Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); // 分页插件 interceptor.addInnerInterceptor(paginationInterceptor()); return interceptor; } /** * 分页插件 */ public PaginationInnerInterceptor paginationInterceptor() { PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor(); // 设置数据库类型为mysql paginationInnerInterceptor.setDbType(DbType.MYSQL); // 设置请求的页面大于最大页后操作, true调回到首页,false 继续请求 默认false paginationInnerInterceptor.setOverflow(false); // 设置最大单页限制数量,默认 500 条,-1 不受限制 paginationInnerInterceptor.setMaxLimit(-1L); return paginationInnerInterceptor; } }
到此这篇关于Mybatis-plus多条件筛选分页的实现的文章就介绍到这了,更多相关Mybatisplus多条件筛选分页内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!