mybatis-plus 使用笔记

1.QueryWrapper

@Test
public void testQueryWrapper() {
//查询条件构造器(方式一)
QueryWrapper<BannerItem> wrapper = new QueryWrapper<>();
wrapper.eq("banner_id", id);
List<BannerItem> bannerItems = bannerItemMapper.selectList(wrapper);

//我们可以引入lambda,避免在代码中写类似的于banner_id的硬编码(方式二)
LambdaQueryWrapper<BannerItem> wrapper = new QueryWrapper<BannerItem>().lambda();
wrapper.eq(BannerItem::getBannerId, id);
List<BannerItem> bannerItems = bannerItemMapper.selectList(wrapper);

//方式三
LambdaQueryWrapper<BannerItem> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(BannerItem::getBannerId, id);
List<BannerItem> bannerItems = bannerItemMapper.selectList(wrapper);
}

2.updateById更新说明

根据id更新不为null的数据
实例:

TShopGoods t=new TShopGoods();
t.setId(id);
t.setTotalMum(tShopGoods.getTotalMum());
tShopGoodsService.updateById(t);

输出sql为:
在这里插入图片描述

3.UpdateWrapper

UpdateWrapper<SysUser> updateWrapper=new UpdateWrapper<>();
updateWrapper.eq("user_id",1);
updateWrapper.set("create_time",new Date());
boolean update = userService.update(updateWrapper);
System.out.println(update);

在这里插入图片描述

4.Wrappers.lambdaQuery

List<TPromotion> tPromotions = tPromotionMapper.selectList(Wrappers.lambdaQuery(tPromotion));
if (tPromotions.isEmpty()){
    tPromotion.setCreateBy(SecurityUtils.getUsername());
    tPromotion.setCreateTime(new Date());
    this.save(tPromotion);
}

5.多对一查询,使用association 查询

(1)实体

package com.dcqq.mode2.box.domain;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;

/**
 * @date 2024-07-13
 */
@Data
public class Mode2BoxPateItem{
    private static final long serialVersionUID = 1L;

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

    private String delFlag;
    
    private Long boxPeriodId;

    private Long boxCommodityId;
    
    @TableField(exist = false)
    private Mode2BoxCommodity boxCommodity;
}

(2)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.dcqq.mode2.box.mapper.Mode2BoxPateItemMapper">
    
    <resultMap id="pateItem" type="com.dcqq.mode2.box.domain.Mode2BoxPateItem">
        <id property="id" column="sid"/>
        <result property="delFlag" column="sDelFlag"/>
        <result property="boxPeriodId" column="sBoxPeriodId"/>
        <result property="boxCommodityId" column="sBoxCommodityId"/>
        <association property="boxCommodity" javaType="com.dcqq.mode2.box.domain.Mode2BoxCommodity">
            <id property="id" column="tid"/>
            <result property="delFlag" column="tDelFlag"/>
            <result property="commodityName" column="tCommodityName"/>
        </association>
    </resultMap>

    <select id="selectPateItemList" resultMap="pateItem">
        SELECT
            a.id sid,
            a.del_flag sDelFlag,
            a.box_period_id sBoxPeriodId,
            a.box_commodity_id sBoxCommodityId,
            b.id tid,
            b.del_flag tDelFlag,
            b.commodity_name tCommodityName
        FROM
            mode2_box_pate_item a
                LEFT JOIN mode2_box_commodity b ON a.box_commodity_id = b.id
    </select>
</mapper>

6.一对多查询(子查询),使用collection查询

(1)实体

public class Post {
    private Integer id;
    private String title;
    private List<Comment> comments; // 假设一个帖子有多个评论
    // getters and setters
}
 
public class Comment {
    private Integer id;
    private String content;
    // getters and setters
}

(2)xml

<mapper namespace="com.example.mapper.PostMapper">
    <resultMap id="PostWithCommentsMap" type="Post">
        <id property="id" column="post_id"/>
        <result property="title" column="title"/>
        <collection property="comments" ofType="Comment" 
            select="selectCommentsByPostId" column="post_id"/>
    </resultMap>
 
    <select id="selectPostWithComments" resultMap="PostWithCommentsMap">
        SELECT id AS post_id, title FROM post WHERE id = #{postId}
    </select>
 
    <select id="selectCommentsByPostId" resultType="Comment">
        SELECT id, content FROM comment WHERE post_id = #{post_id}
    </select>
</mapper>

collection多个传参写法:
column=“{workId=planning_work_id,deptId=dept_id}”

7.获取唯一对象

@Override
public TProductOrder getBySn(String sn){
    //return tProductOrderMapper.selectOne(new QueryWrapper<TProductOrder>().eq("sn",sn).eq("del_flag","0"));
    return tProductOrderMapper.selectOne(new LambdaQueryWrapper<TProductOrder>().eq(TProductOrder::getSn, sn).eq(TProductOrder::getDelFlag, "0"));
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值