mybatis-plus 多表关联条件分页查询

这篇具有很好参考价值的文章主要介绍了mybatis-plus 多表关联条件分页查询。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

此处以一对多,条件分页查询为例:

一.表结构:

主表

CREATE TABLE `t_user` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `user_name` varchar(255) DEFAULT NULL COMMENT '用户名',
  `sex` tinyint DEFAULT NULL,
  `email` varchar(255) DEFAULT NULL COMMENT '邮箱',
  `phone` varchar(12) DEFAULT NULL COMMENT '手机号',
  `password` varchar(255) DEFAULT NULL COMMENT '密码',
  `is_delete` tinyint(2) unsigned zerofill DEFAULT '00',
  `create_time` datetime DEFAULT NULL,
  `update_time` datetime DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

mybatisplus多表分页查询,mybatis,mybatis,mysql,java

明细表

CREATE TABLE `t_user_detail` (
  `id` bigint NOT NULL AUTO_INCREMENT COMMENT '主键id',
  `user_id` bigint NOT NULL COMMENT 't_user表主键id',
  `address` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '住址',
  `hobby` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '爱好',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='用户详情表';

mybatisplus多表分页查询,mybatis,mybatis,mysql,java

二.代码实现:

0.请求dto

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

@Data
public class PageQuery {
    @ApiModelProperty("页数据条数")
    public Integer pageSize = 10;

    @ApiModelProperty("当前为第几页")
    public Integer currentPage = 1;
}
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;

@Data
@EqualsAndHashCode
public class UserInfoPageDTO extends PageQuery {

    @ApiModelProperty("用户名")
    private String userName;

    private Integer sex;

    @ApiModelProperty("邮箱")
    private String email;

    @ApiModelProperty("手机号")
    private String phone;

    @ApiModelProperty("爱好")
    private String hobby;
}

1.Controller 层:

@RestController
@RequestMapping("/user")
public class UserController {

    //用户表读的service
    @Resource
    @Qualifier("userServiceWriteImpl")
    private IUserService userWService;

    //用户表写的service
    @Resource
    @Qualifier("userServiceReadImpl")
    private IUserService userRService;

    /**
     * 多表关联分页 条件查询
     * @param dto
     * @return IPage<UserVO>
     */
    @PostMapping("/userInfoPage")
    public IPage<UserVO> findByPage(@RequestBody UserInfoPageDTO dto) {
        return userRService.findByPage(dto);
    }
}

注:我的项目中进行了service 读写分类配置,实际使用中,直接使用mybatis-plus中的 IUserService 对应的接口就行。
2.service 层

public interface IUserService extends IService<User> {
    IPage<UserVO> findByPage(UserInfoPageDTO dto);
}

service impl实现层:

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.up.openfeign.api.user.dto.UserInfoPageDTO;
import com.up.openfeign.api.user.vo.UserVO;
import com.up.user.entity.User;
import com.up.user.mapper.UserMapper;
import com.up.user.service.IUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
@DS("slave")
public class UserServiceReadImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Resource
    private UserMapper userMapper;
    @Override
    public IPage<UserVO> findByPage(UserInfoPageDTO dto) {
        Page<UserVO> page = new Page<>(dto.currentPage, dto.pageSize);
        IPage<UserVO> queryVoPage = userMapper.findByPage(page, dto);
        return queryVoPage;
    }
}

3.mapper 层

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.up.openfeign.api.user.dto.UserInfoPageDTO;
import com.up.openfeign.api.user.vo.UserVO;
import com.up.user.entity.User;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

/**
 * Mapper 接口
 */
@Mapper
public interface UserMapper extends BaseMapper<User> {
    IPage<UserVO> findByPage(Page<UserVO> page, @Param("dto") UserInfoPageDTO dto);
}

4.mapper.xml层

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.up.user.mapper.UserMapper">

    <resultMap id="page_user_vo" type="com.up.openfeign.api.user.vo.UserVO">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="user_name" jdbcType="VARCHAR" property="userName"/>
        <result column="sex" jdbcType="TINYINT" property="sex"/>
        <result column="email" jdbcType="VARCHAR" property="email"/>
        <result column="phone" jdbcType="VARCHAR" property="phone"/>
        <result column="password" jdbcType="VARCHAR" property="password"/>
        <result column="is_delete" jdbcType="TINYINT" property="isDelete"/>
        <result column="create_time"  property="createTime"/>
        <result column="update_time"  property="updateTime"/>
        <!--collection:一对多
            assocication:一对一
            -->
        <collection property="details" ofType="com.up.openfeign.api.user.vo.UserDetailVO">
            <!--  一对多,如果多个表字段名相同,要记住使用别名,否则多条数据只显示一条   -->
            <id column="udId" jdbcType="BIGINT" property="id"/>
            <result column="user_id" jdbcType="BIGINT" property="userId"/>
            <result column="address" jdbcType="VARCHAR" property="address"/>
            <result column="hobby" jdbcType="VARCHAR" property="hobby"/>
        </collection>
    </resultMap>

    <select id="findByPage" resultMap="page_user_vo" parameterType="com.up.openfeign.api.user.dto.UserInfoPageDTO">
        select u.id,u.user_name,u.sex,u.email,u.phone,u.password,u.is_delete,u.create_time,u.update_time,
        ud.id as udId,ud.user_id,ud.address,ud.hobby from t_user u left join t_user_detail ud on u.id=ud.user_id
        <where>
            <if test="dto.userName !='' and dto.userName != null">
                and u.user_name = #{dto.userName,jdbcType=VARCHAR}
            </if>
            <if test="dto.sex != null">
                and u.sex = #{dto.sex,jdbcType=TINYINT}
            </if>
            <if test="dto.email !='' and dto.email != null">
                and u.email = #{dto.email,jdbcType=VARCHAR}
            </if>
            <if test="dto.phone != null and dto.phone!='' ">
                and u.phone = #{dto.phone,jdbcType=VARCHAR}
            </if>
            <if test="dto.hobby != null and dto.hobby!='' ">
                and ud.hobby = #{dto.hobby,jdbcType=VARCHAR}
            </if>
        </where>
    </select>
</mapper>

5.测试:

mybatisplus多表分页查询,mybatis,mybatis,mysql,java
结果body:

{
    "records": [
        {
            "id": 2,
            "userName": "hc",
            "sex": 1,
            "email": "46494588@qq.com",
            "phone": "18062731203",
            "password": "123456",
            "isDelete": 0,
            "createTime": "2022-08-04T13:59:38.000+0000",
            "updateTime": "2022-08-04T14:00:56.000+0000",
            "details": [
                {
                    "id": 3,
                    "userId": 2,
                    "address": "上海",
                    "hobby": "足球"
                }
            ]
        },
        {
            "id": 1,
            "userName": "hc1",
            "sex": 2,
            "email": "46494588@qq.com",
            "phone": "18062731203",
            "password": "123456",
            "isDelete": 0,
            "createTime": "2022-10-20T06:35:12.000+0000",
            "updateTime": "2022-10-21T06:35:15.000+0000",
            "details": [
                {
                    "id": 4,
                    "userId": 1,
                    "address": "北京",
                    "hobby": "足球"
                }
            ]
        }
    ],
    "total": 2,
    "size": 10,
    "current": 1,
    "orders": [],
    "optimizeCountSql": true,
    "searchCount": true,
    "countId": null,
    "maxLimit": null,
    "pages": 1
}

Q:todo page 分页会把details个数也计入总数,后面修复,再补博客文章来源地址https://www.toymoban.com/news/detail-518969.html

到了这里,关于mybatis-plus 多表关联条件分页查询的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包赞助服务器费用

相关文章

  • mybatis-plus实现分页查询

    mybatis-plus实现分页查询

    分页查询使用的方法是: IPage:用来构建分页查询条件 Wrapper:用来构建条件查询的条件,目前我们没有可直接传为Null IPage:返回值,你会发现构建分页条件和方法的返回值都是IPage IPage是一个接口,我们需要找到它的实现类来构建它,具体的实现类,可以进入到IPage类中按ctrl+

    2023年04月08日
    浏览(9)
  • MyBatis-Plus(三.Wrapper条件查询)

    MyBatis-Plus(三.Wrapper条件查询)

    Wrapper是Mybatis-plus中特有的 条件封装接口 也就是把 查询的条件 封装到Wrapper实现类中 它的各个实现类有什么作用呢, 我觉得直接 顾名思义 吧  QueryWrapper(删,查) 因为参数一般都从前端传来的数据中得到, 所以必须用条件封装的第一个参数确认它不为null UpdateWrapper(改) LambdaQuer

    2024年02月04日
    浏览(9)
  • MyBatis-Plus分页查询(快速上手运用)

    MyBatis-Plus分页查询(快速上手运用)

    Mybatis-Plus知识点[MyBatis+MyBatis-Plus的基础运用]_心态还需努力呀的博客-CSDN博客   Mybatis-Plus+SpringBoot结合运用_心态还需努力呀的博客-CSDN博客 MyBaits-Plus中@TableField和@TableId用法_心态还需努力呀的博客-CSDN博客 MyBatis-Plus中的更新操作(通过id更新和条件更新)_心态还需努力呀的博

    2024年02月16日
    浏览(10)
  • mybatis-plus分页查询三种方法

    mybatis-plus分页查询三种方法

    说明: 1、mybatis-plus中分页接口需要包含一个IPage类型的参数。 2、多个实体参数,需要添加@Param参数注解,方便在xml中配置sql时获取参数值。 注意这里我虽然加了@Param但是我并没有使用 这是控制台打印的查询语句,大家发现最后的LIMIT 函数没,正常来说mybatis-plus里是没有写

    2024年01月25日
    浏览(14)
  • Mybatis-plus动态条件查询QueryWrapper的使用

    Mybatis-plus动态条件查询QueryWrapper的使用

    queryWrapper是mybatis plus中实现查询的对象封装操作类,可以封装sql对象,包括where条件,order by排序,select哪些字段等等,他的层级关系如下图: 2.1-案例一:根据name模糊查看未删除的用户列表信息 过滤条件: queryWrapper实现: 2.2-案例二:查看姓李的并且邮箱不为空的用户列表

    2024年02月14日
    浏览(12)
  • 窥探系列之Mybatis-plus XML分页查询

    窥探系列之Mybatis-plus XML分页查询

    Page类在mybatisPlus中用于分页查询,继承Pagination类,Pagination类的searchCount字段控制是否查询总记录数 顺着看哪里用到了searchCount: com.baomidou.mybatisplus.plugins.PaginationInterceptor 是mybatisPlus的一个插件,也就是说mybatis是通过插件的方式在分页的时候查询总数; 红圈中使用sql解析包

    2024年02月13日
    浏览(14)
  • Mybatis-Plus分页插件查询慢解决方案

    Mybatis-Plus分页插件查询慢解决方案

    需求反馈前端界面查询速度很慢。 f12查看接口响应时间达到了5.47s。 查看后端代码逻辑,就是传了些参数,分页查询了一个列表的数据。分页插件使用的是mybatis-plus的分页插件,PaginationInterceptor。 把后端调用的sql单独拿出来在navicat中进行执行,才0.54s左右,其实很快了,数

    2024年02月11日
    浏览(10)
  • 【SpringBoot】MyBatis与MyBatis-Plus分页查询 & github中的PageHelper

    【SpringBoot】MyBatis与MyBatis-Plus分页查询 & github中的PageHelper

            笔者写这篇博客是因为近期遇到的关于两者之间的分页代码差距,其实之前也遇见过但是没有去整理这篇博客,但由于还是被困扰了小一会儿时间,所以还是需要 加深记忆 。其实会看前后端传参解决这个问题很快、不麻烦。关于这两个框架的分页代码问题主要就

    2024年02月03日
    浏览(11)
  • SpringBoot整合mybatis-plus实现分页查询(建议收藏)

    SpringBoot整合mybatis-plus实现分页查询(建议收藏)

    一、前言         最近学习了SpringBoot分页查询的两种写法,一种是手动实现,另一种是使用框架实现。现在我将具体的实现流程分享一下。 二、手动实现分页查询         先复习一下,SQL中的limit,下面一行sql语句的意思是从第二个数据开始查,查询出两条数据

    2024年01月16日
    浏览(9)
  • MyBatis-Plus使用条件构造器实现不同条件的查询,更新删除操作

    Wrapper 是所有条件构造器的父类,作用是生成条件语句,即where后面的sql子句 在调用查询,更新,删除操作时,需要根据条件进行判断,可以使用条件构造器进行组合条件,生成where后面条件子句 QueryWrapper,UpdateWrapper LambdaQueryWrapper,LambdaUpdateWrapper UpdateWrapper提供了set方法,可以

    2024年02月15日
    浏览(18)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包