mybatis映射文件模板mapper.xml格式


1、定義基礎的映射

對象DO與數據庫字段間的映射

<resultMap id="UserResult" type="UserDO">
<result property="id" column="id" jdbcType="BIGINT"/>
<result property="gmtCreate" column="gmt_create" jdbcType="TIMESTAMP"/>
<result property="gmtModified" column="gmt_modified" jdbcType="TIMESTAMP"/>
<result property="userName" column="user_name" jdbcType="VARCHAR"/>
<result property="deleted" column="deleted" jdbcType="DECIMAL"/>
</resultMap>

2、定義sql語句字段模版

字符類型判斷null和' ',其他類型只判null
Integer,Long之類的類型傳0,如果是id要加判斷''或者判斷是否等於0,這樣對於0值,會直接跳過判斷語句
如果是狀態類型的數值,只要判null,不然會過濾掉0這個條件判斷

    <sql id="baseColumns">
    //sql語句前置DO對象字段模版
    id,
    gmt_create,
    gmt_modified,
    user_name,
    deleted
   </sql>

<sql id="insertColumns">
  //插入sql語句后置傳入對象模版
    #{id},
    now(),
    now(),
    #{userName},
    #{deleted}
</sql>

<sql id="batchInsertColumns">
      /批量插入sql語句后置傳入對象模版,配合foreach使用
    #{item.id},
    now(),
now(),
    #{item.userName},
    #{item.deleted}
</sql>

<!--動態更新列-->
<sql id="updateColumns">
  //sql語句更新列模版
    <if test="userName != null and userName != ''">,
        user_name = #{userName},
    </if>
  <if test="deleted != null">,
        deleted = #{deleted}
    </if>
</sql>

<!--批量動態更新列-->
<sql id="batchUpdateColumns">
  //sql語句批量更新列模版
    <if test="item.userName != null and item.userName != ''">,
        user_name = #{item.userName}
    </if>
</sql>

<sql id="whereParam">
  //sql語句查詢條件模版
    <where>
        <if test="id != null and id != ''">
          //基礎模版
            AND id = #{id}
        </if>
        <if test="start != null">
          //日期格式模版
            <![CDATA[
            AND  gmt_create >= #{start} ]]>
        </if>
        <if test="end != null">
          // <![CDATA[]]> 不讓xml編譯內部語句
            <![CDATA[
            AND  gmt_create <= #{end} ]]>
        </if>
        
        <if test="userName != null and userName != ''">
          //模糊查詢模版
            AND user_name like CONCAT('%',#{userName},'%')
        </if>
         
       <if test="userName != null and userName != ''">
          //模糊查詢使用bind標簽 https://www.cnblogs.com/youmingDDD/p/9435422.html
          <bind name="userNameLike" value=" '%' + userName + '%' "/>
            and user_name like  #{userNameLike}
        </if>
        
        
    </where>
</sql>

//基礎實例

<insert id="insert" parameterType="UserDO">
    insert into sx_user (<include refid="baseColumns"/>)
    values (<include refid="insertColumns"/>)
</insert>

<insert id="batchInsert" parameterType="java.util.List">
    INSERT INTO sx_user (<include refid="baseColumns"/>)
    VALUES
    <foreach collection="list" item="item" index="index" separator=",">
        (<include refid="batchInsertColumns"/>)
    </foreach>
</insert>

<update id="updateById" parameterType="UserDO">
    update sx_user set gmt_modified=now(),
    <include refid="updateColumns"/>
    where id = #{id}
</update>

<update id="batchUpdateById" parameterType="java.util.List">
    <foreach collection="list" item="item" index="index" open="" close="" separator=";">
        update sx_user set gmt_modified=now(),
        <include refid="batchUpdateColumns"/>
        where id = #{item.id}
    </foreach>
</update>

<select id="countPageByCondition" parameterType="QueryCondition" resultType="java.lang.Long">
    SELECT COUNT(*)
    FROM sx_user
    <include refid="whereParam"/>
</select>
<select id="selectList" resultMap="UserResult">
    select
    <include refid="baseColumns"/>
    from sx_user
    where id = #{id}
</select>

<select id="listByIds" resultMap="UserResult">
    select
    <include refid="baseColumns"/>
    from sx_user
    where id IN
    <foreach item="item" index="index" collection="array" open="(" separator="," close=")">
      //in中多個參數拼接
        <!-- 如果直接傳String ids (case:1,2,3,4) 
            <foreach collection="ids .split(',')" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
         -->
        #{item}
    </foreach>
</select>

<delete id="deleteById">
    delete from sx_user  where id = #{id}
</delete>

參考學習:https://blog.csdn.net/lu1024188315/article/details/78758943

    update table
    <trim prefix="set" suffixOverrides=",">
        <trim prefix="user_name = case" suffix="end,">
            <foreach collection="list" item="item" index="index">
                <if test="item.userName!=null">
                    when id=#{item.id} then #{item.userName}
                </if>
            </foreach>
        </trim>
        
        <trim prefix="phone = case" suffix="end,">
            <foreach collection="list" item="item" index="index">
                when id=#{item.id} then ''
            </foreach>
        </trim>
        <trim prefix="num = case" suffix="end,">
            <foreach collection="list" item="item" index="index">
                when id=#{item.id} then null
            </foreach>
        </trim>
        
    </trim>
    where
    <foreach collection="list" separator="or" item="item" index="index">
        id=#{item.id}
    </foreach>
</update>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM