Mybatis

  1. Mybatis
    1. 1,ResultMap association
    2. 2,Choose … when
    3. 3,case when else end
    4. 4,传入多参数类型list和String
    5. 5,sql递归调用

Mybatis

官网

1,ResultMap association

javaType:是java实体类型
property:是该实体类型中对应的属性名
column:数据库查询对应的列名

<resultMap id="OrderVoResult" type="com.v0710.domain.vo.OrderVo">
    <id column="id" property="id"/>
    <result column="order_no" property="orderNo"/>
    <result column="create_time" property="createTime"/>
    <association property="consumeUser" javaType="com.v0710.domain.entity.AppUser">
        <id column="consumeUser_id" property="id"/>
        <result column="consume_name" property="nickName"/>
        <result column="user_type" property="userType"/>
        <result column="phone" property="phone"/>
    </association>
</resultMap>

association 也可返回 List 如下

<resultMap id="projectCycleInfo" type="com.v0710.domain.vo.ProjectfoVo">
    <result property="projectId" column="project_id"/>
    <result property="projectName" column="project_name"/>
    <result property="buildCompany" column="build_company"/>
    <association property="fileSimpleVos" resultMap="fileSimpleVo">
    </association>
</resultMap>

<resultMap id="fileSimpleVo" type="com.v0710.domain.vo.FileSimpleVo">
    <result column="file_name" property="fileName"/>
    <result column="file_path" property="filePath"/>
</resultMap>

<select id="getProjectCycleInfo" resultMap="projectCycleInfo">
</select>

2,Choose … when

choose 相当于 switch

​ when 相当于 case

​ otherwise 相当于 default

<choose>
    <when test="mechanicUid!=null">
        LEFT JOIN mgp_app_user m on #{mechanicUid} = m.id
    </when>
    <otherwise>
        LEFT JOIN mgp_app_user m on  mgp_order.mechanic_uid  = m.id
    </otherwise>
</choose>

3,case when else end

case when 条件1 then 取值1 else 不满足条件的取值 end

case函数只返回第一个符合条件的值,剩下的Case部分将会被自动忽略

update table  
set 字段1 = case     
    when 条件1 then 值1       
    when 条件2 then 值2      
    else 值3      
    end     
where    …… 
select 字段1, 字段2,       
    case 字段3     
    when 值1 then 新值       
    when 值2 then 新值      
    end as 重新命名字段3的名字       
from table      
where ……      
order by ……  

4,传入多参数类型list和String

方法一

把所有参数封装到map

public void deleteBooks(Map<String, Object> map);
map.put("title", "1hdhd");
map.put("templist",list);
<select id="dynamicForeach3Test" resultType="Blog">
         select * from t_blog where title like "%"#{title}"%" and id in
          <foreach collection="ids" index="index" item="item" open="(" separator="," close=")">
               #{item.id}
          </foreach>
</select>

方法二

public void deleteBooks(@Param("partnerId")long partnerId ,@Param("templist")List<CPPartnerBookSet> list);
<delete id="deleteBooks">
        delete from cp.tbl_cp_partner_books
        where partner_id = #{partnerId} 
        and book_id in
        <foreach collection="templist" index="index" item="item" open="(" separator="," close=")">
            #{item.bookId}
        </foreach>
</delete>

5,sql递归调用

image-20211129113344410


日夜颠倒头发少 ,单纯好骗恋爱脑 ,会背九九乘法表 ,下雨只会往家跑 ,搭讪只会说你好 ---- 2050781802@qq.com

×

喜欢就点赞,疼爱就打赏

相册 说点什么