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

mybatisplus 多表關聯條件分頁查詢的實現

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

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

一.表結構:

主表

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;

在這里插入圖片描述

明細表

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

在這里插入圖片描述

二.代碼實現:

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.測試:

在這里插入圖片描述

結果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個數也計入總數,后面修復,再補博客

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

相關文章

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

    Java加密解密和數字簽名完整代碼示例

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

    java中transient關鍵字的作用解析

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

    Java 讀取文件方法大全

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

    Java中的注解與注解處理器

    這篇文章主要介紹了Java中的注解與注解處理器,元注解的作用是負責注解其他注解, Java5.0定義了4個標準的meta-annotation(元注解)類型,它們被用來提供對其它注解類型進行說明,需要的朋友可以參考下
    2023-11-11
  • mybatis-plus @DS實現動態(tài)切換數據源原理

    mybatis-plus @DS實現動態(tài)切換數據源原理

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

    解讀JDK1.8?默認使用什么垃圾收集器

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

    SpringBoot項目在IntelliJ IDEA中如何實現熱部署

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

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

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

    SpringBoot項目加載配置文件的6種方式小結

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

    Spring Boot+Mybatis的整合過程

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

最新評論

勃利县| 嘉兴市| 汝州市| 长沙县| 长治市| 呼伦贝尔市| 桓台县| 衡山县| 郑州市| 屏南县| 新巴尔虎右旗| 澄迈县| 顺昌县| 新密市| 阿城市| 建瓯市| 乌拉特中旗| 云和县| 缙云县| 丁青县| 旺苍县| 昆山市| 新龙县| 方山县| 陆川县| 万全县| 嵩明县| 扬州市| 博客| 获嘉县| 怀远县| 嘉定区| 达孜县| 江都市| 壤塘县| 含山县| 黑水县| 青海省| 胶州市| 阳春市| 昌吉市|