欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

前言

記錄下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

實(shí)現(xiàn)需求:

根據(jù)product - id查詢product實(shí)例及其關(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) {
    	// 實(shí)現(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對(duì)象用于出參
     * @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ù)庫(kù)字段
     * id 判斷相等的值
     */
    QueryWrapper<ProductItem> wrapper = new QueryWrapper<>();
    wrapper.eq("product_id", id);
    List<ProductItem> productItems = productItemMapper.selectList(wrapper);

    return new ProductWithItemsVo(product, productItems);
}

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

QueryWrapper 的lambada寫(xiě)法

@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);
    }

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

LambadaQueryWrapper 的使用

  • LambadaQueryWrapper 用于Lambda語(yǔ)法使用的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);
}

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

LambdaQueryChainWrapper 的鏈?zhǔn)秸{(diào)用

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

        /**
         * 鏈?zhǔn)秸{(diào)用
         */
        List<ProductItem> productItems =
                new LambdaQueryChainWrapper<>(productItemMapper)
                        .eq(ProductItem::getProductId, id)
                        .list();

        return new ProductWithItemsVo(product, productItems);
    }

如上代碼,通過(guò)鏈?zhǔn)秸{(diào)用查詢出當(dāng)前product實(shí)例及其關(guān)聯(lián)的product_item

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

相關(guān)文章

  • Java實(shí)現(xiàn)Token工具類進(jìn)行登錄和攔截

    Java實(shí)現(xiàn)Token工具類進(jìn)行登錄和攔截

    在應(yīng)用的登錄時(shí)需要生成token進(jìn)行驗(yàn)證,并放入信息,之后的話可以直接使用瀏覽器的session進(jìn)行登錄,本文就來(lái)利用java編寫(xiě)一個(gè)token工具類,可以很方便的生成和解析token,感興趣的可以了解下
    2023-12-12
  • 利用Java搭建個(gè)簡(jiǎn)單的Netty通信實(shí)例教程

    利用Java搭建個(gè)簡(jiǎn)單的Netty通信實(shí)例教程

    這篇文章主要給大家介紹了關(guān)于如何利用Java搭建個(gè)簡(jiǎn)單的Netty通信,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Java具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05
  • Java垃圾回收之復(fù)制算法詳解

    Java垃圾回收之復(fù)制算法詳解

    今天小編就為大家分享一篇關(guān)于Java垃圾回收之復(fù)制算法詳解,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2018-10-10
  • Java 類加載過(guò)程與類加載器詳細(xì)介紹

    Java 類加載過(guò)程與類加載器詳細(xì)介紹

    這篇文章主要介紹了Java 類加載過(guò)程與類加載器詳細(xì)介紹,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下
    2022-07-07
  • java使用集合實(shí)現(xiàn)通訊錄功能

    java使用集合實(shí)現(xiàn)通訊錄功能

    這篇文章主要為大家詳細(xì)介紹了java使用集合實(shí)現(xiàn)通訊錄功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-02-02
  • 詳解使用IntelliJ IDEA新建Java Web后端resfulAPI模板

    詳解使用IntelliJ IDEA新建Java Web后端resfulAPI模板

    這篇文章主要介紹了詳解使用IntelliJ IDEA新建Java Web后端resfulAPI模板,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-08-08
  • 如何解決java獲取時(shí)間相差8小時(shí)的問(wèn)題

    如何解決java獲取時(shí)間相差8小時(shí)的問(wèn)題

    最近使用new date()獲取的時(shí)間會(huì)和真實(shí)的本地時(shí)間相差8小時(shí)。本文就詳細(xì)的來(lái)介紹一下解決java獲取時(shí)間相差8小時(shí)的問(wèn)題,感興趣的可以了解一下
    2021-09-09
  • Java設(shè)計(jì)模式模板方法(Template)原理解析

    Java設(shè)計(jì)模式模板方法(Template)原理解析

    這篇文章主要介紹了Java設(shè)計(jì)模式模板方法(Template)原理解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-11-11
  • IDEA2021常用優(yōu)化設(shè)置步驟圖解

    IDEA2021常用優(yōu)化設(shè)置步驟圖解

    本文分步驟給大家講解IDEA2021常用優(yōu)化設(shè)置技巧,非常不錯(cuò),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2021-09-09
  • shiro與spring?security用自定義異常處理401錯(cuò)誤

    shiro與spring?security用自定義異常處理401錯(cuò)誤

    這篇文章主要介紹了shiro與spring?security用自定義異常處理401錯(cuò)誤,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11

最新評(píng)論