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

MyBatis-Plus聯(lián)表查詢以及分頁(yè)代碼實(shí)例

 更新時(shí)間:2023年06月08日 09:43:43   作者:李長(zhǎng)淵哦  
在開發(fā)中遇到了一個(gè)問(wèn)題,需要進(jìn)行聯(lián)表查詢并進(jìn)行分頁(yè),因?yàn)椴幌胱约簛?lái)寫分頁(yè),所以還是依靠MybatisPlus來(lái)實(shí)現(xiàn)想要的功能,下面這篇文章主要給大家介紹了關(guān)于MyBatis-Plus聯(lián)表查詢以及分頁(yè)的相關(guān)資料,需要的朋友可以參考下

一、準(zhǔn)備工作

mybatis-plus作為mybatis的增強(qiáng)工具,它的出現(xiàn)極大的簡(jiǎn)化了開發(fā)中的數(shù)據(jù)庫(kù)操作,但是長(zhǎng)久以來(lái),它的聯(lián)表查詢能力一直被大家所詬病。一旦遇到left join或right join的左右連接,你還是得老老實(shí)實(shí)的打開xml文件,手寫上一大段的sql語(yǔ)句。

直到前幾天,偶然碰到了這么一款叫做mybatis-plus-join的工具(后面就簡(jiǎn)稱mpj了),使用了一下,不得不說(shuō)真香!徹底將我從xml地獄中解放了出來(lái),終于可以以類似mybatis-plus中QueryWrapper的方式來(lái)進(jìn)行聯(lián)表查詢了,話不多說(shuō),我們下面開始體驗(yàn)。

  • mapper繼承MPJBaseMapper (必選)
  • service繼承MPJBaseService (可選)
  • serviceImpl繼承MPJBaseServiceImpl (可選)

1、數(shù)據(jù)庫(kù)結(jié)構(gòu)以及數(shù)據(jù)

CREATE TABLE `op_product` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `type` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
INSERT INTO `test_yjdsns`.`op_product`(`id`, `type`) VALUES (1, '蘋果');
CREATE TABLE `op_product_info` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `product_id` int(11) NOT NULL,
  `name` varchar(255) DEFAULT NULL,
  `price` decimal(10,2) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
INSERT INTO `test_yjdsns`.`op_product_info`(`id`, `product_id`, `name`, `price`) VALUES (1, 1, '蘋果13', 8.00);
INSERT INTO `test_yjdsns`.`op_product_info`(`id`, `product_id`, `name`, `price`) VALUES (2, 1, '蘋果15', 9.00);

2、依賴

<dependency>
    <groupId>com.github.yulichang</groupId>
    <artifactId>mybatis-plus-join</artifactId>
    <version>1.2.4</version>
</dependency>
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.5.1</version>
</dependency>

3、配置類讓mybatis-plus-join在DataScopeSqlInjector中生效

/**
 * @author licy
 * @description
 * @date 2022/10/20
 */
@Configuration
public class MybatisPlusConfig {
    /**
     * 新增分頁(yè)攔截器,并設(shè)置數(shù)據(jù)庫(kù)類型為mysql
     *
     * @return
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        //分頁(yè)插件
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }
    /**
     * sql注入
     */
    @Bean
    @Primary
    public MySqlInjector myLogicSqlInjector() {
        return new MySqlInjector();
    }
}

修改DataScopeSqlInjector中的繼承類為:MPJSqlInjector

public class MySqlInjector extends MPJSqlInjector {
    @Override
    public List<AbstractMethod> getMethodList(Class<?> mapperClass) {
        //將原來(lái)的保持
        List<AbstractMethod> methodList = super.getMethodList(mapperClass);
        //多表查詢sql注入 從連表插件里移植過(guò)來(lái)的
        methodList.add(new SelectJoinOne());
        methodList.add(new SelectJoinList());
        methodList.add(new SelectJoinPage());
        methodList.add(new SelectJoinMap());
        methodList.add(new SelectJoinMaps());
        methodList.add(new SelectJoinMapsPage());
        return methodList;
    }
}

4、啟動(dòng)類排除MPJSqlInjector.class

@SpringBootApplication(exclude = {MPJSqlInjector.class})

載入自定義配置類

@Configuration

@MapperScan可以選擇tk下的路徑

import tk.mybatis.spring.annotation.MapperScan;

二、代碼

1、實(shí)體類

/**
 * @author licy
 * @description
 * @date 2022/10/25
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName("op_product")
public class OpProduct implements Serializable {
    private static final long serialVersionUID = -3918932563888251866L;
    @TableId(value = "ID", type = IdType.AUTO)
    private Long id;
    @TableField("TYPE")
    private String type;
}
/**
 * @author licy
 * @description
 * @date 2022/10/25
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName("op_product_info")
public class OpProductInfo implements Serializable {
    private static final long serialVersionUID = 4186082342917210485L;
    @TableId(value = "ID", type = IdType.AUTO)
    private Long id;
    @TableField("PRODUCT_ID")
    private Long productId;
    @TableField("NAME")
    private String name;
    @TableField("PRICE")
    private Double price;
}
/**
 * @author licy
 * @description
 * @date 2022/10/25
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ProductDTO implements Serializable {
    private static final long serialVersionUID = -2281333877153304329L;
    private Long id;
    private String type;
    private String name;
    private Double price;
}

2、Mapper

/**
 * @author licy
 * @description
 * @date 2022/10/26
 */
public interface OpProductInfoMapper extends MPJBaseMapper<OpProductInfo> {
}
/**
 * @author licy
 * @description
 * @date 2022/10/25
 */
public interface OpProductMapper extends MPJBaseMapper<OpProduct> {
}

3、Service

Mapper接口改造完成后,我們把它注入到Service中,雖然說(shuō)我們要完成3張表的聯(lián)表查詢,但是以O(shè)pProduct作為主表的話,那么只注入這一個(gè)對(duì)應(yīng)的OpProductMapper就可以,非常簡(jiǎn)單。

public interface OpProductService extends MPJBaseService<OpProduct> {
    List<ProductDTO> queryAllProduct();
}
@Service
@Slf4j
@AllArgsConstructor
public class OpProductServiceImpl extends MPJBaseServiceImpl<OpProductMapper, OpProduct> implements OpProductService {
    @Resource
    private OpProductMapper opProductMapper;
    @Override
    public List<ProductDTO> queryAllProduct() {
        MPJLambdaWrapper mpjLambdaWrapper = new MPJLambdaWrapper<ProductDTO>()
                .selectAll(OpProduct.class)//查詢表1的全部字段
                .selectAll(OpProductInfo.class)//查詢表2的全部字段
                .leftJoin(OpProductInfo.class, OpProductInfo::getProductId, OpProduct::getId);//左查詢表2條件為表二的productId=表一的id
        List<ProductDTO> list = opProductMapper.selectJoinList(ProductDTO.class, mpjLambdaWrapper);
        return list;
    }
}

4、測(cè)試

@SpringBootTest
@Slf4j
public class MybatisJoinTests {
    @Autowired
    private OpProductService opProductService;
    @Test
    void test1() {
        List<ProductDTO> productDTOS = opProductService.queryAllProduct();
        log.info(productDTOS.toString());
    }
}

5、結(jié)果

三、分頁(yè)查詢

1、MPJLambdaWrapper幾個(gè)方法

接下來(lái)的MPJLambdaWrapper就是構(gòu)建查詢條件的核心了,看一下我們?cè)谏厦嬗玫降膸讉€(gè)方法:

  • selectAll():查詢指定實(shí)體類的全部字段
  • select():查詢指定的字段,支持可變長(zhǎng)參數(shù)同時(shí)查詢多個(gè)字段,但是在同一個(gè)select中只能查詢相同表的字段,所以如果查詢多張表的字段需要分開寫
  • selectAs():字段別名查詢,用于數(shù)據(jù)庫(kù)字段與接收結(jié)果的dto中屬性名稱不一致時(shí)轉(zhuǎn)換
  • leftJoin():左連接,其中第一個(gè)參數(shù)是參與聯(lián)表的表對(duì)應(yīng)的實(shí)體類,第二個(gè)參數(shù)是這張表聯(lián)表的ON字段,第三個(gè)參數(shù)是參與聯(lián)表的ON的另一個(gè)實(shí)體類屬性

除此之外,還可以正常調(diào)用mybatis-plus中的各種原生方法,文檔中還提到,默認(rèn)主表別名是t,其他的表別名以先后調(diào)用的順序使用t1、t2、t3以此類推。

和mybatis-plus非常類似,除了LamdaWrapper外還提供了普通QueryWrapper的寫法,舉例代碼:

public void getOrderSimple() {
     List<xxxxxDto> list = xxxxxMapper.selectJoinList(xxxxx.class,
     new MPJQueryWrapper<xxxxx>()
      .selectAll(xxxxx.class)
      .select("t2.unit_price","t2.name as product_name")
      .select("t1.name as user_name")
      .leftJoin("t_user t1 on t1.id = t.user_id")
      .leftJoin("t_product t2 on t2.id = t.product_id")
      .eq("t.status", "3")
    );
    log.info(list.toString());
}

或者

        MPJLambdaWrapper mpjLambdaWrapper = new MPJLambdaWrapper<ProductDTO>()
                .selectAll(OpProduct.class)//查詢表1的全部字段
                .selectAs(OpProductInfo::getId,"ProductInfoId")//起別名
                .selectAs(OpProductInfo::getName,ProductDTO::getName)//起別名
                .selectAs(OpProductInfo::getPrice,ProductDTO::getPrice)//起別名
                .leftJoin(OpProductInfo.class, OpProductInfo::getProductId, OpProduct::getId);//左查詢表2條件為表二的productId=表一的id
        List<ProductDTO> list = opProductMapper.selectJoinList(ProductDTO.class, mpjLambdaWrapper);
        return list;

2、分頁(yè)代碼舉例

    public IPage<ProductDTO> queryPageProduct(Integer pageNo, Integer pageCount) {
        MPJLambdaWrapper mpjLambdaWrapper = new MPJLambdaWrapper<ProductDTO>()
                .selectAll(OpProduct.class)//查詢表1的全部字段
                .selectAll(OpProductInfo.class)//查詢表2的全部字段
                .leftJoin(OpProductInfo.class, OpProductInfo::getProductId, OpProduct::getId);//左查詢表2條件為表二的productId=表一的id
        IPage<ProductDTO> page = opProductMapper.selectJoinPage(new Page<ProductDTO>(pageNo, pageCount), ProductDTO.class, mpjLambdaWrapper);
        return page;
    }

總結(jié)

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

相關(guān)文章

  • 一篇文章帶你了解Java 中序列化與反序列化

    一篇文章帶你了解Java 中序列化與反序列化

    這篇文章主要介紹了Java 序列化與反序列化(Serialization),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-07-07
  • java實(shí)現(xiàn)在pdf模板的指定位置插入圖片

    java實(shí)現(xiàn)在pdf模板的指定位置插入圖片

    這篇文章主要為大家詳細(xì)介紹了java如何實(shí)現(xiàn)在pdf模板的指定位置插入圖片,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-10-10
  • springboot發(fā)送郵件功能的實(shí)現(xiàn)代碼

    springboot發(fā)送郵件功能的實(shí)現(xiàn)代碼

    發(fā)郵件是一個(gè)很常見的功能,在java中實(shí)現(xiàn)需要依靠JavaMailSender這個(gè)接口,今天通過(guò)本文給大家分享springboot發(fā)送郵件功能的實(shí)現(xiàn)代碼,感興趣的朋友跟隨小編一起看看吧
    2021-07-07
  • AOP在SpringBoot項(xiàng)目中的使用場(chǎng)景解讀

    AOP在SpringBoot項(xiàng)目中的使用場(chǎng)景解讀

    本文介紹如何使用AOP在不同場(chǎng)景下對(duì)方法執(zhí)行前進(jìn)行邏輯校驗(yàn),包括對(duì)整個(gè)包下、特定控制器下以及特定注解修飾的方法進(jìn)行校驗(yàn),通過(guò)自定義注解和切面實(shí)現(xiàn),展示了AOP的靈活性和強(qiáng)大功能
    2026-01-01
  • IDEA運(yùn)行SpringBoot項(xiàng)目的超詳細(xì)步驟截圖

    IDEA運(yùn)行SpringBoot項(xiàng)目的超詳細(xì)步驟截圖

    在當(dāng)前的開發(fā)中Spring Boot開發(fā)框架已經(jīng)成為主流,下面這篇文章主要給大家介紹了關(guān)于IDEA運(yùn)行SpringBoot項(xiàng)目的超詳細(xì)步驟截圖,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2022-11-11
  • Spring?Boot請(qǐng)求處理之常用參數(shù)注解使用教程

    Spring?Boot請(qǐng)求處理之常用參數(shù)注解使用教程

    這篇文章主要給大家介紹了關(guān)于Spring?Boot請(qǐng)求處理之常用參數(shù)注解使用的相關(guān)資料,文中通過(guò)圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2022-03-03
  • Java中的for循環(huán)結(jié)構(gòu)及實(shí)例

    Java中的for循環(huán)結(jié)構(gòu)及實(shí)例

    這篇文章主要介紹了Java中的for循環(huán)結(jié)構(gòu)及實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • java自定義類加載器如何實(shí)現(xiàn)類隔離

    java自定義類加載器如何實(shí)現(xiàn)類隔離

    這篇文章主要介紹了java自定義類加載器如何實(shí)現(xiàn)類隔離問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • 如何集成swagger2構(gòu)建Restful API

    如何集成swagger2構(gòu)建Restful API

    這篇文章主要介紹了如何集成swagger2構(gòu)建Restful API,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-11-11
  • JAVA多線程之中斷機(jī)制stop()、interrupted()、isInterrupted()

    JAVA多線程之中斷機(jī)制stop()、interrupted()、isInterrupted()

    這篇文章主要介紹了JAVA多線程之中斷機(jī)制stop()、interrupted()、isInterrupted()的相關(guān)資料,需要的朋友可以參考下
    2016-05-05

最新評(píng)論

巧家县| 土默特左旗| 阿克| 故城县| 西乌珠穆沁旗| 河曲县| 普洱| 文水县| 陆河县| 莱州市| 商城县| 冷水江市| 塔河县| 封开县| 宜丰县| 夏河县| 铜山县| 将乐县| 射阳县| 英山县| 苍梧县| 武胜县| 洪雅县| 尼木县| 汉源县| 屏山县| 乌恰县| 聊城市| 黔江区| 乐业县| 武夷山市| 中山市| 天柱县| 武隆县| 古田县| 邵阳市| 徐闻县| 安顺市| 漳州市| 界首市| 莆田市|