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

Mybatis-Plus 條件構(gòu)造器 QueryWrapper 的基本用法

 更新時間:2021年09月08日 08:27:44   作者:Maggieq8324  
這篇文章主要介紹了Mybatis-Plus - 條件構(gòu)造器 QueryWrapper 的使用,通過實例代碼給大家介紹了查詢示例代碼及實現(xiàn)需求,代碼簡單易懂,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

前言

記錄下Mybatis-Plus中條件構(gòu)造器Wrapper 的一些基本用法。

查詢示例

表結(jié)構(gòu)

CREATE TABLE `product` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `title` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
  `create_time` datetime DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=35 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

CREATE TABLE `product_item` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `title` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
  `product_id` int(10) unsigned NOT NULL,
  `create_time` datetime DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

實現(xiàn)需求:

根據(jù)product - id查詢product實例及其關(guān)聯(lián)的product_item,如下:

基礎(chǔ)代碼

ProductController.java

@GetMapping("/{id}")
public ProductWithItemsVo getWithItems(@PathVariable Integer id) {
   return productService.getWithItems(id);
}

ProductService.java

public interface ProductService {
    ProductWithItemsVo getWithItems(Integer id);
}

ProductServiceImpl.java

@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {
	@Autowired
    private ProductItemMapper productItemMapper;

	@Override
    public ProductWithItemsVo getWithItems(Integer id) {
    	// 實現(xiàn)代碼
    }
}

mapper

@Repository
public interface ProductMapper extends BaseMapper<Product> {

}

@Repository
public interface ProductItemMapper extends BaseMapper<ProductItem> {

}

model

@Getter
@Setter
@TableName("product")
public class Product {

    private Integer id;

    private String title;

    @JsonIgnore
    private Date createTime;

}

@Getter
@Setter
@TableName("product_item")
public class ProductItem {

    private Integer id;

    private Integer productId;

    private String title;

    @JsonIgnore
    private Date createTime;
}

vo出參

@Data
@NoArgsConstructor
public class ProductWithItemsVo {

    private Integer id;

    private String title;

    List<ProductItem> items;

	/**
     * 構(gòu)造ProductWithItemsVo對象用于出參
     * @param product
     * @param items
     */
    public ProductWithItemsVo(Product product, List<ProductItem> items) {
        BeanUtils.copyProperties(product, this);
        this.setItems(items);
    }
}

QueryWrapper 的基本使用

@Override
public ProductWithItemsVo getWithItems(Integer id) {
    Product product = this.getById(id);
    if (Objects.isNull(product)) {
        System.out.println("未查詢到product");
        return null;
    }

    /**
     * wrapper.eq("banner_id", id)
     * banner_id 數(shù)據(jù)庫字段
     * id 判斷相等的值
     */
    QueryWrapper<ProductItem> wrapper = new QueryWrapper<>();
    wrapper.eq("product_id", id);
    List<ProductItem> productItems = productItemMapper.selectList(wrapper);

    return new ProductWithItemsVo(product, productItems);
}

如上代碼,通過條件構(gòu)造器QueryWrapper查詢出當前product實例及其關(guān)聯(lián)的product_item

QueryWrapper 的lambada寫法

@Override
    public ProductWithItemsVo getWithItems(Integer id) {
        Product product = this.getById(id);
        if (Objects.isNull(product)) {
            System.out.println("未查詢到product");
            return null;
        }

        QueryWrapper<ProductItem> wrapper = new QueryWrapper<>();
        /**
         * lambda方法引用
         */
        wrapper.lambda().eq(ProductItem::getProductId, id);
        List<ProductItem> productItems = productItemMapper.selectList(wrapper);

        return new ProductWithItemsVo(product, productItems);
    }

如上代碼,通過條件構(gòu)造器QueryWrapperlambda方法引用查詢出當前product實例及其關(guān)聯(lián)的product_item

LambadaQueryWrapper 的使用

  • LambadaQueryWrapper 用于Lambda語法使用的QueryWrapper
  • 構(gòu)建LambadaQueryWrapper 的方式:
 /**
   * 方式一
   */
  LambdaQueryWrapper<ProductItem> wrapper1 = new QueryWrapper<ProductItem>().lambda();
  wrapper1.eq(ProductItem::getProductId, id);
  List<ProductItem> productItems1 = productItemMapper.selectList(wrapper1);

  /**
   * 方式二
   */
  LambdaQueryWrapper<ProductItem> wrapper2 = new LambdaQueryWrapper<>();
  wrapper2.eq(ProductItem::getProductId, id);
  List<ProductItem> productItems2 = productItemMapper.selectList(wrapper2);

完整代碼

@Override
public ProductWithItemsVo getWithItems(Integer id) {
    Product product = this.getById(id);
    if (Objects.isNull(product)) {
        System.out.println("未查詢到product");
        return null;
    }

    LambdaQueryWrapper<ProductItem> wrapper = new LambdaQueryWrapper<>();
    wrapper.eq(ProductItem::getProductId, id);
    List<ProductItem> productItems = productItemMapper.selectList(wrapper);

    return new ProductWithItemsVo(product, productItems);
}

如上代碼,通過條件構(gòu)造器LambdaQueryWrapper查詢出當前product實例及其關(guān)聯(lián)的product_item

LambdaQueryChainWrapper 的鏈式調(diào)用

@Override
    public ProductWithItemsVo getWithItems(Integer id) {
        Product product = this.getById(id);
        if (Objects.isNull(product)) {
            System.out.println("未查詢到product");
            return null;
        }

        /**
         * 鏈式調(diào)用
         */
        List<ProductItem> productItems =
                new LambdaQueryChainWrapper<>(productItemMapper)
                        .eq(ProductItem::getProductId, id)
                        .list();

        return new ProductWithItemsVo(product, productItems);
    }

如上代碼,通過鏈式調(diào)用查詢出當前product實例及其關(guān)聯(lián)的product_item

到此這篇關(guān)于Mybatis-Plus - 條件構(gòu)造器 QueryWrapper 的使用的文章就介紹到這了,更多相關(guān)Mybatis-Plus 條件構(gòu)造器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 快速解決idea @Autowired報紅線問題

    快速解決idea @Autowired報紅線問題

    這篇文章主要介紹了快速解決idea @Autowired報紅線問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • 關(guān)于weblogic部署Java項目的包沖突問題的解決

    關(guān)于weblogic部署Java項目的包沖突問題的解決

    這篇文章主要介紹了關(guān)于weblogic部署Java項目的包沖突問題的解決,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-01-01
  • 新手也能看懂的SpringBoot異步編程指南(簡單易懂)

    新手也能看懂的SpringBoot異步編程指南(簡單易懂)

    這篇文章主要介紹了新手也能看懂的SpringBoot異步編程指南(簡單易懂),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • 如何解決Idea斷點調(diào)試亂跳的問題

    如何解決Idea斷點調(diào)試亂跳的問題

    這篇文章主要介紹了如何解決Idea斷點調(diào)試亂跳的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • http協(xié)議進階之Transfer-Encoding和HttpCore實現(xiàn)詳解

    http協(xié)議進階之Transfer-Encoding和HttpCore實現(xiàn)詳解

    這篇文章主要給大家介紹了http協(xié)議之Transfer-Encoding和HttpCore實現(xiàn)的相關(guān)資料,文中介紹的非常詳細,相信對大家具有一定的參考價值,需要的朋友們下面來一起看看吧。
    2017-04-04
  • Java獲取json數(shù)組對象的實例講解

    Java獲取json數(shù)組對象的實例講解

    下面小編就為大家分享一篇Java獲取json數(shù)組對象的實例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-03-03
  • SpringCloud Config分布式配置中心使用教程介紹

    SpringCloud Config分布式配置中心使用教程介紹

    springcloud config是一個解決分布式系統(tǒng)的配置管理方案。它包含了 client和server兩個部分,server端提供配置文件的存儲、以接口的形式將配置文件的內(nèi)容提供出去,client端通過接口獲取數(shù)據(jù)、并依據(jù)此數(shù)據(jù)初始化自己的應(yīng)用
    2022-12-12
  • 微信小程序與Java后端接口交互

    微信小程序與Java后端接口交互

    本文主要介紹了微信小程序與Java后端接口交互,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • Spring gateway配置Spring Security實現(xiàn)統(tǒng)一權(quán)限驗證與授權(quán)示例源碼

    Spring gateway配置Spring Security實現(xiàn)統(tǒng)一權(quán)限驗證與授權(quán)示例源碼

    這篇文章主要介紹了Spring gateway配置Spring Security實現(xiàn)統(tǒng)一權(quán)限驗證與授權(quán),本文通過示例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-07-07
  • Spring詳細講解@Autowired注解

    Spring詳細講解@Autowired注解

    @Autowired注解可以用在類屬性,構(gòu)造函數(shù),setter方法和函數(shù)參數(shù)上,該注解可以準確地控制bean在何處如何自動裝配的過程。在默認情況下,該注解是類型驅(qū)動的注入
    2022-06-06

最新評論

青铜峡市| 宿州市| 体育| 汝南县| 西宁市| 台中县| 张掖市| 谢通门县| 泾川县| 乡城县| 建始县| 苍溪县| 石门县| 武鸣县| 平度市| 东辽县| 乌兰县| 长葛市| 茂名市| 乌兰浩特市| 汶川县| 宜兴市| 绥德县| 章丘市| 广元市| 门头沟区| 博爱县| 资中县| 伊宁县| 鹿泉市| 依兰县| 濉溪县| 沾化县| 怀来县| 信丰县| 封开县| 炎陵县| 渭源县| 蒙自县| 南川市| 监利县|