最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

mybatisplus 多表關(guān)聯(lián)條件分頁查詢的實(shí)現(xiàn)

 更新時(shí)間:2025年01月16日 09:16:21   作者:哈嘍,樹先生  
本文主要介紹了mybatisplus 多表關(guān)聯(lián)條件分頁查詢的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

此處以一對(duì)多,條件分頁查詢?yōu)槔?/p>

一.表結(jié)構(gòu):

主表

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 '手機(jī)號(hào)',
  `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;

在這里插入圖片描述

明細(xì)表

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='用戶詳情表';

在這里插入圖片描述

二.代碼實(shí)現(xiàn):

0.請(qǐng)求dto

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

@Data
public class PageQuery {
    @ApiModelProperty("頁數(shù)據(jù)條數(shù)")
    public Integer pageSize = 10;

    @ApiModelProperty("當(dāng)前為第幾頁")
    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("手機(jī)號(hào)")
    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;

    /**
     * 多表關(guān)聯(lián)分頁 條件查詢
     * @param dto
     * @return IPage<UserVO>
     */
    @PostMapping("/userInfoPage")
    public IPage<UserVO> findByPage(@RequestBody UserInfoPageDTO dto) {
        return userRService.findByPage(dto);
    }
}

注:我的項(xiàng)目中進(jìn)行了service 讀寫分類配置,實(shí)際使用中,直接使用mybatis-plus中的 IUserService 對(duì)應(yīng)的接口就行。

2.service 層

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

service impl實(shí)現(xiàn)層:

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:一對(duì)多
            assocication:一對(duì)一
            -->
        <collection property="details" ofType="com.up.openfeign.api.user.vo.UserDetailVO">
            <!--  一對(duì)多,如果多個(gè)表字段名相同,要記住使用別名,否則多條數(shù)據(jù)只顯示一條   -->
            <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.測試:

在這里插入圖片描述

結(jié)果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 分頁會(huì)把details個(gè)數(shù)也計(jì)入總數(shù),后面修復(fù),再補(bǔ)博客

到此這篇關(guān)于mybatisplus 多表關(guān)聯(lián)條件分頁查詢的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)mybatisplus 多表分頁查詢內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java加密解密和數(shù)字簽名完整代碼示例

    Java加密解密和數(shù)字簽名完整代碼示例

    這篇文章主要介紹了Java加密解密和數(shù)字簽名完整代碼示例,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-12-12
  • java中transient關(guān)鍵字的作用解析

    java中transient關(guān)鍵字的作用解析

    這篇文章主要介紹了java中transient關(guān)鍵字的作用解析,日常業(yè)務(wù)中,為了安全起見,有些敏感信息我們不希望在網(wǎng)絡(luò)間被傳輸可以使用transient對(duì)字段進(jìn)行修飾,不進(jìn)行序列化,則返回獲取到的字段為null,需要的朋友可以參考下
    2023-11-11
  • Java 讀取文件方法大全

    Java 讀取文件方法大全

    這篇文章主要介紹了Java 讀取文件方法大全,需要的朋友可以參考下
    2014-11-11
  • Java中的注解與注解處理器

    Java中的注解與注解處理器

    這篇文章主要介紹了Java中的注解與注解處理器,元注解的作用是負(fù)責(zé)注解其他注解, Java5.0定義了4個(gè)標(biāo)準(zhǔn)的meta-annotation(元注解)類型,它們被用來提供對(duì)其它注解類型進(jìn)行說明,需要的朋友可以參考下
    2023-11-11
  • mybatis-plus @DS實(shí)現(xiàn)動(dòng)態(tài)切換數(shù)據(jù)源原理

    mybatis-plus @DS實(shí)現(xiàn)動(dòng)態(tài)切換數(shù)據(jù)源原理

    本文主要介紹了mybatis-plus @DS實(shí)現(xiàn)動(dòng)態(tài)切換數(shù)據(jù)源原理,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • 解讀JDK1.8?默認(rèn)使用什么垃圾收集器

    解讀JDK1.8?默認(rèn)使用什么垃圾收集器

    這篇文章主要介紹了解讀JDK1.8?默認(rèn)使用什么垃圾收集器,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • SpringBoot項(xiàng)目在IntelliJ IDEA中如何實(shí)現(xiàn)熱部署

    SpringBoot項(xiàng)目在IntelliJ IDEA中如何實(shí)現(xiàn)熱部署

    spring-boot-devtools是一個(gè)為開發(fā)者服務(wù)的一個(gè)模塊,其中最重要的功能就是自動(dòng)應(yīng)用代碼更改到最新的App上面去。,這篇文章主要介紹了SpringBoot項(xiàng)目在IntelliJ IDEA中如何實(shí)現(xiàn)熱部署,感興趣的小伙伴們可以參考一下
    2018-07-07
  • Java中的使用及連接Redis數(shù)據(jù)庫(附源碼)

    Java中的使用及連接Redis數(shù)據(jù)庫(附源碼)

    這篇文章主要介紹了Java中的使用及連接Redis數(shù)據(jù)庫(附源碼),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • SpringBoot項(xiàng)目加載配置文件的6種方式小結(jié)

    SpringBoot項(xiàng)目加載配置文件的6種方式小結(jié)

    這篇文章給大家總結(jié)了六種SpringBoot項(xiàng)目加載配置文件的方式,通過@value注入,通過@ConfigurationProperties注入,通過框架自帶對(duì)象Environment實(shí)現(xiàn)屬性動(dòng)態(tài)注入,通過@PropertySource注解,yml外部文件,Java原生態(tài)方式注入這六種,需要的朋友可以參考下
    2023-09-09
  • Spring Boot+Mybatis的整合過程

    Spring Boot+Mybatis的整合過程

    這篇文章主要介紹了Spring Boot+Mybatis的整合過程,需要的朋友可以參考下
    2017-07-07

最新評(píng)論

盐池县| 成都市| 昭苏县| 贡觉县| 招远市| 肥东县| 铁力市| 工布江达县| 和政县| 北票市| 遵化市| 建昌县| 富源县| 兰州市| 永仁县| 长沙市| 泰州市| 琼海市| 塔河县| 岑溪市| 邢台县| 阿拉尔市| 富源县| 玉屏| 临清市| 克拉玛依市| 平山县| 宁海县| 明溪县| 汤原县| 安西县| 通许县| 广平县| 法库县| 荔波县| 噶尔县| 铜山县| 普定县| 宜川县| 闽侯县| 泸西县|