SpringBoot + MyBatisPlus基本使用

前言:
mybatis在持久層框架中還是比較火的,一般項(xiàng)目都是基于ssm。雖然mybatis可以直接在xml中通過SQL語句操作數(shù)據(jù)庫,很是靈活。但正其操作都要通過SQL語句進(jìn)行,就必須寫大量的xml文件,很是麻煩。mybatis-plus就很好的解決了這個(gè)問題。

簡(jiǎn)介

MyBatis-Plus(簡(jiǎn)稱 MP)是一個(gè) MyBatis的增強(qiáng)工具,在 MyBatis 的基礎(chǔ)上只做增強(qiáng)不做改變,為簡(jiǎn)化開發(fā)、提高效率而生。
官網(wǎng):https://mp.baomidou.com/guide

整合

1、創(chuàng)建表

CREATE TABLE user (
    id BIGINT(20) PRIMARY KEY NOT NULL COMMENT '主鍵',
    user_name VARCHAR(30) DEFAULT NULL COMMENT '姓名',
    age INT(11) DEFAULT NULL COMMENT '年齡',
    email VARCHAR(50) DEFAULT NULL COMMENT '郵箱',
    create_time DATETIME DEFAULT NULL COMMENT '創(chuàng)建時(shí)間',
        update_time DATETIME DEFAULT NULL COMMENT '修改時(shí)間'
)  ENGINE=INNODB CHARSET=UTF8;

初始化數(shù)據(jù)


INSERT INTO user (id, user_name, age, email,create_time,update_time) VALUES
(1, 'Jone', 18, 'test1@baomidou.com',now(),now()),
(2, 'Jack', 20, 'test2@baomidou.com',now(),now()),
(3, 'Tom', 28, 'test3@baomidou.com',now(),now()),
(4, 'Sandy', 21, 'test4@baomidou.com',now(),now()),
(5, 'Billie', 24, 'test5@baomidou.com',now(),now());

now()函數(shù)獲取當(dāng)前時(shí)間

2、創(chuàng)建springboot項(xiàng)目并導(dǎo)入依賴

image.png

修改mysql版本為5.1.47

  <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
            <scope>runtime</scope>
        </dependency>

MyBatis-Plus依賴:

 <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus-boot-starter</artifactId>
                <version>3.2.0</version>
    </dependency>

官網(wǎng)最新版本已經(jīng)為: 3.3.2,使用Boot版本為2.2.4,不使用最新的,哈哈。

3、配置

1、在 application.yml 配置文件中添加數(shù)據(jù)庫的相關(guān)配置:

server:
  port: 1998
spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/test?useSSL=false&useUnicode=true&characterEncoding=utf-8
    username: root
    password: 980402
    driver-class-name: com.mysql.jdbc.Driver

mysql5跟mysql8在連接的時(shí)候會(huì)有點(diǎn)不同:
(1)、mysql 5 驅(qū)動(dòng)不同 com.mysql.jdbc.Driver
(2)、mysql 8 驅(qū)動(dòng)不同com.mysql.cj.jdbc.Driver、需要增加時(shí)區(qū)的配置 serverTimezone=GMT+8

4、編碼

1、編寫實(shí)體類 User.java

//生成getter,setter等函數(shù)
@Data
//生成全參數(shù)構(gòu)造函數(shù)
@AllArgsConstructor
//生成無參構(gòu)造函數(shù)
@NoArgsConstructor
public class User {
    private Long id;
    private String userName;
    private Integer age;
    private String email;
    private Date createTime;
    private Date updateTime;
}

2、編寫Mapper接口

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.gongj.mybatisplus.entity.User;
//繼承基本的類 BaseMapper
public interface UserMapper extends BaseMapper<User> {
}

3、在 Spring Boot 啟動(dòng)類中添加 @MapperScan 注解,掃描 Mapper 所在目錄

@SpringBootApplication
@MapperScan("com.gongj.mybatisplus.mapper")
public class MybatisPlusApplication {

    public static void main(String[] args) {
        SpringApplication.run(MybatisPlusApplication.class, args);
    }

}
image.png

5、測(cè)試

5.1、測(cè)試查詢?nèi)?/h4>
@SpringBootTest
class MybatisPlusApplicationTests {
@Autowired
    UserMapper userMapper;
    @Test
    void testSelect(){
        // 參數(shù)是一個(gè) Wrapper ,條件構(gòu)造器,這里我們先不用 null 查詢?nèi)坑脩?        List<User> users = userMapper.selectList(null);
        users.forEach(System.out::println);
    }
}
image.png

5.2、在yml配置sql日志

mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

再次查詢


image.png

5.3測(cè)試添加

@Test
    void testInsert() {
        User user = new User();
        user.setUserName("gongj");
        user.setEmail("19908488818@163.com");
        user.setAge(23);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        int insert = userMapper.insert(user);//幫我們自動(dòng)生成id
        System.out.println("id=" + user.getId());   //id會(huì)自動(dòng)回填
    }
image.png

MyBatisPlus主鍵策略:

public enum IdType {
    AUTO(0),  //數(shù)據(jù)庫id自增
    NONE(1), // 未設(shè)置主鍵id
    INPUT(2),  // 用戶輸入id
    //以下三種只有當(dāng)插入對(duì)象ID 為空,才自動(dòng)填充。
    ID_WORKER(3),  // 默認(rèn)的全局唯一id
    UUID(4),  //全局唯一uuid
    ID_WORKER_STR(5); //字符串全局唯一ID ID_WORKER的字符串表示方式
}

修改默認(rèn)的主鍵策略
在實(shí)體類字段上 加上注解:@TableId(type = xxx)

@TableId(type = IdType.AUTO)
private Long id;

修改了主鍵策略為自增,數(shù)據(jù)庫字段一定要是自增!


image.png

5.4測(cè)試自動(dòng)填充

在上面添加操作中我們是手動(dòng)給創(chuàng)建時(shí)間、修改時(shí)間賦值,能不能自動(dòng)化完成呢? MyBatisPlus提供了這樣的配置。
1、自定義實(shí)現(xiàn)類 MyMetaObjectHandler

@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    // 插入時(shí)的填充策略
    @Override
    public void insertFill(MetaObject metaObject) {
        this.setFieldValByName("createTime",new Date(),metaObject);
        this.setFieldValByName("updateTime",new Date(),metaObject);
    }

    // 更新時(shí)的填充策略
    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("updateTime",new Date(),metaObject);
    }
}

2、實(shí)體類字段屬性增加注解

    @TableField(fill = FieldFill.INSERT)
    private Date createTime;
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private Date updateTime;

填充類型:

public enum FieldFill {
    /**
     * 默認(rèn)不處理
     */
    DEFAULT,
    /**
     * 插入填充字段
     */
    INSERT,
    /**
     * 更新填充字段
     */
    UPDATE,
    /**
     * 插入和更新填充字段
     */
    INSERT_UPDATE
}

再次測(cè)試添加操作:

@Test
    void testInsert() {
        User user = new User();
        user.setUserName("gongj2");
        user.setEmail("19908488818@163.com");
        user.setAge(223);
        int insert = userMapper.insert(user);//幫我們自動(dòng)生成id
        System.out.println("id=" + user.getId());
    }
image.png

效果杠杠的。

5.5修改測(cè)試

 @Test
    void testUpdate() {
        User user = new User();
        user.setId(1276042184872341506L);
        user.setUserName("ggggggggggg");
        user.setAge(555);
        int insert = userMapper.updateById(user);
        System.out.println("id=" + user.getId());
    }
image.png

所有的sql都是自動(dòng)幫你動(dòng)態(tài)配置的!也可以看到updateTime的自動(dòng)填充也生效了。

6、樂觀鎖

當(dāng)要更新一條記錄的時(shí)候,希望這條記錄沒有被別人更新

樂觀鎖實(shí)現(xiàn)方式:
1、取出記錄時(shí),獲取當(dāng)前version
2、更新時(shí),帶上這個(gè)version
3、執(zhí)行更新時(shí), set version = newVersion where version = oldVersion
4、如果version不對(duì),就更新失敗

6.1數(shù)據(jù)庫增加version字段!

image.png

6.2實(shí)體類增加version屬性并加注解!

   @Version
    private Integer version;

6.3插件配置

@Configuration
public class MybatisPlusConfig {

    /**
     * 樂觀鎖插件
     */
    @Bean
    public OptimisticLockerInterceptor optimisticLockerInterceptor() {
        return new OptimisticLockerInterceptor();
    }
}

6.4、測(cè)試修改

    @Test
    void testVserion(){
    // 1、查詢用戶信息
     User user = userMapper.selectById(1276056567094583298L);
     user.setAge(55);
     user.setUserName("yuanj");
     //user 就是前臺(tái)傳入的值
     userMapper.updateById(user);
    }
image.png

我們注意一下update的sql語句

 UPDATE user SET create_time=?, update_time=?, user_name=?, version=?, email=?, age=? WHERE id=? AND version=?

version做為了修改條件。


image.png

特別說明:

  • 支持的數(shù)據(jù)類型只有:int,Integer,long,Long,Date,Timestamp,LocalDateTime
  • 整數(shù)類型下 newVersion = oldVersion + 1
  • newVersion 會(huì)回寫到 entity
  • 僅支持 updateById(id)update(entity, wrapper) 方法
  • update(entity, wrapper) 方法下, wrapper 不能復(fù)用!!!

7、查詢

7.1、根據(jù)id查詢

@Test
    void selectById(){
      
        userMapper.selectById(1276056567094583298L);
    }

7.2、根據(jù)id批量查詢

 @Test 
    public void selectBatchIds(){
        // 根據(jù)id批量查詢
        List<User> users = userMapper.selectBatchIds(Arrays.asList(1276056567094583298L, 5, 3));
     }

7.3、根據(jù)Map 自定義條件查詢

 @Test
    public void selectByMap(){
        // 根據(jù)Map 自定義條件查詢
        Map map = new HashMap();
        map.put("user_name","yuanj");
        List<User> users = userMapper.selectByMap(map);
    }

8、分頁查詢

1、配置攔截器組件

     /**
     * 分頁插件
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }

2、編碼

  @Test
    public void selectPage(){
       
        Page<User> userPage = new Page<>();
        userPage.setCurrent(1L);  //當(dāng)前是第幾頁 默認(rèn)為1
        userPage.setSize(2);  //每頁大小
        IPage<User> userIPage = userMapper.selectPage(userPage, null);

        System.out.println("當(dāng)前頁" + userIPage.getCurrent());  //當(dāng)前頁
        System.out.println("總頁數(shù)" + userIPage.getPages()); //總頁數(shù)
        System.out.println("返回?cái)?shù)據(jù)" + userIPage.getRecords());  //返回?cái)?shù)據(jù)
        System.out.println("每頁大小" + userIPage.getSize());  //每頁大小
        System.out.println("滿足符合條件的條數(shù)" + userIPage.getTotal());  //滿足符合條件的條數(shù)
        System.out.println("下一頁" + userPage.hasNext());   //下一頁
        System.out.println("上一頁" + userPage.hasPrevious());  //上一頁
    }

9、刪除

刪除分為物理刪除和邏輯刪除。

1、物理刪除

1.1根據(jù)id物理刪除

@Test
    void deleteById(){
        //根據(jù)id物理刪除
        int i = userMapper.deleteById(1L);
    }
image.png

1.2根據(jù)id物理批量刪除

 @Test
    void deleteBatchIds(){
        //批量刪除
        int i = userMapper.deleteBatchIds(Arrays.asList(2L, 4L));
        System.out.println(i);  //受影響的行數(shù)為2
    }

2、邏輯刪除

2.1、在數(shù)據(jù)表中增加一個(gè) deleted 字段

image.png

2.2、實(shí)體類中增加屬性并加上注解

  @TableLogic
    private Integer deleted;

2.3、測(cè)試

再次執(zhí)行刪除操作

@Test
    void deleteById(){
        //根據(jù)id刪除
        int i = userMapper.deleteById(3L);
        System.out.println(i);
    }
image.png

可以看到sql是修改語句。


image.png

2.4執(zhí)行查詢:

   @Test
    void testSelect(){
        // 參數(shù)是一個(gè) Wrapper ,條件構(gòu)造器,這里我們先不用 null 查詢?nèi)坑脩?        List<User> users = userMapper.selectList(null);
        users.forEach(System.out::println);
    }
image.png

可以看到id為3的數(shù)據(jù)已經(jīng)查詢不出來了。

10、條件構(gòu)造器

1、查詢創(chuàng)建時(shí)間在2020-6-15到2020-7-5并且郵箱不等于空并不等于null的數(shù)據(jù)

@Test
    void select1(){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        //查詢創(chuàng)建時(shí)間在2020-6-15到2020-7-5并且郵箱不等于空并不等于null的數(shù)據(jù)
        queryWrapper.between("create_time","2020-06-01","2020-07-05").ne("email","").isNotNull("email");
        List<User> users = userMapper.selectList(queryWrapper);
        users.forEach((System.out::println));
    }
 Preparing: SELECT id,deleted,create_time,update_time,user_name,version,email,age FROM 
user WHERE deleted=0 AND (create_time BETWEEN ? AND ? AND 
email <> ? AND email IS NOT NULL)

2、模糊查詢

 @Test
    void select2(){
        //模糊查詢
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.likeLeft("user_name","J");   //左模糊  %J
        List<User> users = userMapper.selectList(queryWrapper);
        users.forEach((System.out::println));
    }

3、子查詢并根據(jù)id倒序

 @Test
    void select3(){
        //子查詢并根據(jù)id倒序
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.inSql("id","select id from user where id < 4");
        queryWrapper.orderByDesc("id");
        List<User> users = userMapper.selectList(queryWrapper);
        users.forEach((System.out::println));
    }
 Preparing: SELECT id,deleted,create_time,update_time,user_name,version,email,age 
FROM user WHERE deleted=0 AND (id IN (select id from user where id < 4)) 
ORDER BY id DESC

4、分頁多條件查詢

//分頁多條件查詢
    @Test
    void select4(){
        //查詢年齡大于等于28歲的信息并分頁
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge("age",28);
        Page<User> userPage = new Page<>();
        userPage.setCurrent(1L);
        userPage.setSize(2);
        IPage<User> userIPage = userMapper.selectPage(userPage, queryWrapper);
        List<User> records = userIPage.getRecords();
        records.forEach((System.out::println));
    }
SELECT COUNT(1) FROM user WHERE deleted = 0 AND (age >= ?)

 SELECT id,deleted,create_time,update_time,user_name,version,email,age,cid 
FROM user WHERE deleted=0 AND (age >= ?) LIMIT ?,?

11、關(guān)聯(lián)查詢

1、新建一個(gè)card表

CREATE TABLE card (
    cid BIGINT(20) PRIMARY KEY NOT NULL COMMENT '主鍵',
    cname VARCHAR(30) DEFAULT NULL COMMENT '卡片名'
)  ENGINE=INNODB CHARSET=UTF8;
INSERT INTO card (cid,cname) VALUES
(1,'健身卡'),
(2,'KTV卡'),
(3,'校園卡')

2、在user表中創(chuàng)建cid列

image.png

并將user表和card表關(guān)聯(lián)起來


image.png

3、編碼

3.1、card實(shí)體
//生成getter,setter等函數(shù)
@Data
//生成全參數(shù)構(gòu)造函數(shù)
@AllArgsConstructor
//生成無參構(gòu)造函數(shù)
@NoArgsConstructor
public class Card {
    private Long cid;
    private String cname;
}
3.2、user實(shí)體

加入cid和cards屬性

 private Long cid;

private List<Card> cards;
3.3、UserMapper新增方法
 List<User> userJoinCard();
3.4、UserMapper.xml
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.gongj.mybatisplus.mapper.UserMapper">
<resultMap id="userJoinCardMap" type="com.gongj.mybatisplus.entity.User">

    <id column="id" property="id"/>
    <result property="userName" column="user_name"></result>
    <result property="age" column="age"></result>
    <result property="createTime" column="create_time"></result>
    <result property="updateTime" column="update_time"></result>
    <result property="email" column="email"></result>
    <result property="email" column="email"></result>
    <result property="version" column="version"></result>
    <result property="deleted" column="deleted"></result>
    <result property="cid" column="cid"></result>
    <collection property="cards" column="cid" ofType="com.gongj.mybatisplus.entity.Card">
        <id column="cid" property="cid"></id>
        <result column="cname" property="cname"></result>
    </collection>
</resultMap>

<select id="userJoinCard" resultMap="userJoinCardMap">
select u.id,u.user_name,u.email,u.age,u.version,u.create_time,u.update_time,u.deleted,u.cid,c.cid,c.cname from user u left join card c on u.cid = c.cid
</select>
</mapper>

resultMap包含的元素:

<resultMap id="唯一的標(biāo)識(shí)" type="映射的pojo對(duì)象">
  <id column="表的主鍵字段" jdbcType="字段類型" property="映射pojo對(duì)象的主鍵屬性" />
  <result column="表的一個(gè)字段" jdbcType="字段類型" property="映射到pojo對(duì)象的一個(gè)屬性"/>
  <association property="pojo的一個(gè)對(duì)象屬性" javaType="pojo關(guān)聯(lián)的pojo對(duì)象">
    <id column="關(guān)聯(lián)pojo對(duì)象對(duì)應(yīng)表的主鍵字段" jdbcType="字段類型" property="關(guān)聯(lián)pojo對(duì)象的主席屬性"/>
    <result  column="任意表的字段" jdbcType="字段類型" property="關(guān)聯(lián)pojo對(duì)象的屬性"/>
  </association>
  <!-- 集合中的property須為oftype定義的pojo對(duì)象的屬性-->
  <collection property="pojo的集合屬性" ofType="集合中的pojo對(duì)象">
    <id column="集合中pojo對(duì)象對(duì)應(yīng)的表的主鍵字段" jdbcType="字段類型" property="集合中pojo對(duì)象的主鍵屬性" />
    <result column="可以為任意表的字段" jdbcType="字段類型" property="集合中的pojo對(duì)象的屬性" />  
  </collection>
</resultMap>

如果collection標(biāo)簽是使用嵌套查詢,格式如下:
注意:<collection>標(biāo)簽中的column:要傳遞給select查詢語句的參數(shù),如果傳遞多個(gè)參數(shù),格式為column= ” {參數(shù)名1=表字段1,參數(shù)名2=表字段2} ;

<collection column="傳遞給嵌套查詢語句的字段參數(shù)" property="pojo對(duì)象中集合屬性" ofType="集合屬性中的pojo對(duì)象" select="嵌套的查詢語句" > 
</collection>

https://www.cnblogs.com/kenhome/p/7764398.html

3.5、調(diào)用
 @Test
    void joinQuery(){
        List<User> users = userMapper.userJoinCard();
        users.forEach((System.out::println));
    }
image.png

可以看到就是一條關(guān)聯(lián)查詢sql,然后在resultMap中進(jìn)行字段映射。

3.6、collection的另外一種寫法,也可以叫分布查詢

CardMapper

public interface CardMapper extends BaseMapper<Card> {

    public Card selectById();
}

CardMapper.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.gongj.mybatisplus.mapper.CardMapper">

    <resultMap id="CardMap" type="com.gongj.mybatisplus.entity.Card">
        <id column="cid" property="cid"></id>
        <result column="cname" property="cname"></result>
    </resultMap>

    <select id="selectById" resultMap="CardMap">
        select * from card where cid = #{cid}
    </select>
</mapper>

userMapper

  List<User> userJoinCard2();

UserMapper.xml

     <resultMap id="userJoinCardMap2" type="com.gongj.mybatisplus.entity.User">
        <id column="id" property="id"/>
        <result property="userName" column="user_name"></result>
        <result property="age" column="age"></result>
        <result property="createTime" column="create_time"></result>
        <result property="updateTime" column="update_time"></result>
        <result property="email" column="email"></result>
        <result property="email" column="email"></result>
        <result property="version" column="version"></result>
        <result property="deleted" column="deleted"></result>
        <result property="cid" column="cid"></result>
        <collection property="cards" column="cid" ofType="com.gongj.mybatisplus.entity.Card" select="com.gongj.mybatisplus.mapper.CardMapper.selectById">

        </collection>
    </resultMap>
    <select id="userJoinCard2" resultMap="userJoinCardMap2">
        select u.id,u.user_name,u.email,u.age,u.version,u.create_time,u.update_time,u.deleted,u.cid from user u
    </select>

select="com.gongj.mybatisplus.mapper.CardMapper.selectById"使用內(nèi)嵌查詢。
調(diào)用

   @Test
    void joinQuery2(){
        List<User> users = userMapper.userJoinCard2();
        users.forEach((System.out::println));
    }
image.png

image.png

11、關(guān)聯(lián)查詢并分頁

UserMapper

 List<User> userJoinCardPage(Page page);

UserMapper.xml

    <select id="userJoinCardPage" resultMap="userJoinCardMap2">
        select u.id,u.user_name,u.email,u.age,u.version,u.create_time,u.update_time,u.deleted,u.cid from user u
    </select>

測(cè)試

    @Test
    void joinQueryPage(){
        Page<User> userPage = new Page<>();
        userPage.setSize(1);
        userPage.setCurrent(1);
        List<User> users = userMapper.userJoinCardPage(userPage);
        users.forEach((System.out::println));
    }
image.png

還是挺簡(jiǎn)單的,只需要傳入一個(gè)Page對(duì)象就可以了。

補(bǔ)充1:

我們發(fā)現(xiàn)如果去調(diào)用單表查詢的結(jié)果,也就是自帶的查詢,就會(huì)出現(xiàn)異常。

 @Test
    void testSelect(){
        // 參數(shù)是一個(gè) Wrapper ,條件構(gòu)造器,這里我們先不用 null 查詢?nèi)坑脩?        List<User> users = userMapper.selectList(null);
        users.forEach(System.out::println);
    }
image.png

那怎么解決呢?在實(shí)體類的屬性上加入注解,exist 默認(rèn)為true。

@TableField(exist = false)
    private List<Card> cards;

注解加在bean屬性上,表示當(dāng)前屬性不是數(shù)據(jù)庫的字段,但在項(xiàng)目中必須使用,這樣在新增、查詢等使用bean的時(shí)候,mybatis-plus就會(huì)忽略這個(gè),不會(huì)報(bào)錯(cuò)。

補(bǔ)充2:多表分頁條件查詢

    //多表分頁查詢
    @Test
    void joinQueryPage3(){
//查詢年齡大于等于20、名稱以j開頭的信息并分頁
        Page<User> userPage = new Page<>();
        userPage.setSize(2);
        userPage.setCurrent(1);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge("age",20);
        queryWrapper.likeRight("user_name","j");
        IPage<User> userIPage = userMapper.userJoinCardPageQuery2(userPage,queryWrapper);
        System.out.println("當(dāng)前頁" + userIPage.getCurrent());  //當(dāng)前頁
        System.out.println("總頁數(shù)" + userIPage.getPages()); //總頁數(shù)

        System.out.println("每頁大小" + userIPage.getSize());  //每頁大小
        System.out.println("滿足符合條件的條數(shù)" + userIPage.getTotal());  //滿足符合條件的條數(shù)
        System.out.println("返回?cái)?shù)據(jù)====");  //返回?cái)?shù)據(jù)
        userIPage.getRecords().forEach((System.out::println));
    }
執(zhí)行結(jié)果:
當(dāng)前頁1
總頁數(shù)1
每頁大小2
滿足符合條件的條數(shù)1
返回?cái)?shù)據(jù)====
User(id=2, userName=Jack, age=20, email=test2@baomidou.com, createTime=Sat Oct 31 07:50:19 CST 2020, updateTime=Sat Oct 31 07:50:19 CST 2020, version=0, deleted=0, cid=1, cards=[Card(cid=1, cname=健身卡)])

UserMapper

    IPage<User> userJoinCardPageQuery2(Page page, @Param(Constants.WRAPPER)QueryWrapper<User> queryWrapper);

UserMapper.xml

  <select id="userJoinCardPageQuery2" resultMap="userJoinCardMap2">
        <!--帶上${ew.customSqlSegment 就可以實(shí)現(xiàn)查詢-->
        select u.id,u.user_name,u.email,u.age,u.version,u.create_time,u.update_time,u.deleted,u.cid from user u
        ${ew.customSqlSegment}
    </select>

執(zhí)行的sql

SELECT COUNT(1) FROM user u WHERE (age >= ? AND user_name LIKE ?)

select u.id,u.user_name,u.email,u.age,u.version,u.create_time,u.update_time,u.deleted,u.cid from user u WHERE (age >= ? AND user_name LIKE ?) LIMIT ?,?

select * from card where cid = ?
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容