mapper.xml案例

2018-07-10 一个查询的结果当作另一个查询条件 in () 的参数

 select * from sys_user where classify_id in (select id from sys_ele_user where status = '1' and is_default = '1')

2018-07-10 批量新增,入参为实体类的list集合

  <!--批量新增-->
  <insert id="batchInsert"  parameterType="java.util.List">
    insert into sys_user (ID, CLASSIFY_ID, CLASSIFY_CODE, 
      CLASSIFY_NAME, CODE, NAME, 
      SHORTCODE, SHORTNAME, WHOLENAME, 
       CREATOR_ID, CREATOR, 
      CREATIONTIME, MODIFIER_ID, MODIFIER, 
      MODIFIEDTIME, REMARK)
    values 
     <foreach collection="list" item="item" index="index" separator="," >  
      (#{item.id,jdbcType=VARCHAR}, #{item.classifyId,jdbcType=VARCHAR}, #{item.classifyCode,jdbcType=VARCHAR}, 
      #{item.classifyName,jdbcType=VARCHAR}, #{item.code,jdbcType=VARCHAR}, #{item.name,jdbcType=VARCHAR}, 
      #{item.shortcode,jdbcType=VARCHAR}, #{item.shortname,jdbcType=VARCHAR}, #{item.wholename,jdbcType=VARCHAR}, 
      #{item.creatorId,jdbcType=VARCHAR}, #{item.creator,jdbcType=VARCHAR}, 
      #{item.creationtime,jdbcType=TIMESTAMP}, #{item.modifierId,jdbcType=VARCHAR}, #{item.modifier,jdbcType=VARCHAR}, 
      #{item.modifiedtime,jdbcType=TIMESTAMP}, #{item.remark,jdbcType=VARCHAR})
      </foreach>
  </insert>


实际sql:insert into 表(字段1,字段2,字段3,...) values(a1,a2,a3),(b1,b2,b3),(c1,c2,c3);要一一对应。

2018-07-10 批量更新sql,入参为实体类的集合list

<!--批量保存修改的内容-->
  <update id="batchUpdateByPrimaryKeySelective" parameterType="java.util.List" >
    <foreach collection="list" item="item" index="index" open="" close=";" separator=";">
    update sys_ele_user
    <set >
      <if test="item.code != null" >
        CODE = #{item.code,jdbcType=VARCHAR},
      </if>
      <if test="item.name != null" >
        NAME = #{item.name,jdbcType=VARCHAR},
      </if>
      <if test="item.shortcode != null" >
        SHORTCODE = #{item.shortcode,jdbcType=VARCHAR},
      </if>
      <if test="item.shortname != null" >
        SHORTNAME = #{item.shortname,jdbcType=VARCHAR},
      </if>
      <if test="item.wholename != null" >
        WHOLENAME = #{item.wholename,jdbcType=VARCHAR},
      </if>
      <if test="item.modifierId != null" >
        MODIFIER_ID = #{item.modifierId,jdbcType=VARCHAR},
      </if>
      <if test="item.modifier != null" >
        MODIFIER = #{item.modifier,jdbcType=VARCHAR},
      </if>
      <if test="item.remark != null" >
        REMARK = #{item.remark,jdbcType=VARCHAR},
      </if>
      MODIFIEDTIME = sysdate(),
      VERSION = VERSION + 1 
    </set>
     where ID = #{item.id,jdbcType=VARCHAR} and  VERSION=#{item.version,jdbcType=INTEGER} 
    </foreach>
  </update>

2018-07-10 按多个条件批量删除,注意括号!

   <!--根据classifyId,parentId,id批量删除-->
  <delete id="batchDeleteByClassifyIdAndOrgIdAndId" parameterType="java.util.List">
    delete  from  sys_user  where (CLASSIFY_ID,ID,ORG_ID) in (
    <foreach collection="list" item="item" index="index"  separator=",">
      (#{item.classifyId,jdbcType=VARCHAR},#{item.id,jdbcType=VARCHAR},#{item.orgId,jdbcType=VARCHAR})
     </foreach>  
     )  
  </delete>

2018-07-10 一个语句执行多个sql

<!--复制完成后执行本脚本完成parentid更新-->
  <update id="updateChildParentId">
    update sys_ele_user sem1,sys_ele_user sem2,sys_ele_user sem3
    set sem1.PARENT_ID=sem3.ID
    where sem1.REMARK=sem2.ID   
    and sem2.PARENT_ID = sem3.REMARK;

    update sys_ele_user set REMARK=null where REMARK is not null
  </update>

2018-07-10 choose,when,otherwise
1.<choose><when><otherwise>配合使用是等价于java中的if(){}else if(){}...
2.按照官方文档给的示例,最后的是需要存在的,但是经过测试,即使最后的没有写,Mybatis也不会发生任何异常。而是将所有表数据返回。这是一种极为不推荐的做法。实际生产环境下,如果该表的数据量非常大,这条语句被执行,就是一个巨大的坑!
3.再特别说明一下,请注意,每一个等号后面的参数都带有单引号。这是这种用法必须必须必须有的。否则,就是直接抛异常!
4.,这里有另外一种情况那就是,判断null的键值对。具体写法如下:


<select id="findUserInfoByOneParam" parameterType="Map" resultMap="UserInfoResult">
        select * from userinfo 
        <choose>
            <when test="department!=null">
                where department=#{department}
            </when>
            <when test="position!=null">
                where position=#{position}
            </when>
        </choose>
    </select> 

下面是个相结合的案例:


<select id="findUserInfoByOneParam" parameterType="Map" resultMap="UserInfoResult">
        select * from userinfo 
        <choose>
            <when test="searchBy=='department'">
                where department=#{department}
            </when>
            <when test="searchBy=='position'">
                where position=#{position}
            </when>
            <otherwise>
                where gender=#{gender}
            </otherwise>
        </choose>
        <if test="d != null and d.id != null">
            AND department = #{d.id}
        </if>

2018-07-10 case,when,then的用法

select id,code,name,description,
            case when infoitem_type='text' then '字符' 
                 when infoitem_type='number' then '数字'
                 when infoitem_type='date' then '日期' 
                 when infoitem_type='money' then '金额' 
                 when infoitem_type='float' then '浮点型数字'
                 when infoitem_type='list' then '列结构' 
                 when infoitem_type='tree' then '树结构' 
            end infoItemType,
            max_length maxLength,max_precision maxPrecision,
            case when is_com='1' then '是' when is_com='0' then '否' end isCom,
            dataelement_code dataElementCode,
            case when is_null='1' then '不能为空' when is_null='0' then '可以为空' end isNull,
            case when status='1' then '正常' when status='0' then '停用' end 
            status,version
        from fa_sys_user

2018-07-10 根据入参查询的结果当作insert的参数新增

   <insert id="downloadTemplate">
   INSERT SYS_USER (ID, CODE, NAME, PARENT_ID, PARENT_CODE CREATOR_ID, CREATOR, CREATIONTIME, MODIFIER_ID, MODIFIER, MODIFIEDTIME, REMARK) 

    SELECT #{uuid}, CODE, NAME, #{parentTemplateId} AS PARENT_ID,PARENT_CODECREATOR_ID, CREATOR, CREATIONTIME, MODIFIER_ID, MODIFIER, MODIFIEDTIME,REMARK FROM  FA_SYS_CARDTEMPLATE 

    WHERE ORG_ID = #{parentOrgId} AND id = #{parentTemplateId} AND IS_RELEASE = '1'
   </insert>

2018-07-10 如何在一个mapper.xml中封装两个实体类,将查询的数据给这两个实体类?关键是mapper.xml开始的声明。
这里写图片描述

猜你喜欢

转载自blog.csdn.net/zhanglf02/article/details/80987905