跳至主要內容

mybatis

日常积累模板代码javamybatis约 431 字大约 1 分钟

常用xml

<!-- 模糊查询 -->
<if test="name != null and name != ''">
  and o.name like concat('%', #{name,jdbcType=VARCHAR},'%')
</if>

<!-- mybatis 循环查询 -->
<if test="name != null">
  <choose>
    <when test="nameIds != null and nameIds.size() > 0">
        <foreach collection="nameIds" open="(" close=")" separator="," item="item">
          #{item,jdbcType=BIGINT}
        </foreach>
    </when>
    
    <otherwise>
            ('')
    </otherwise>
  </choose>
</if>

<if test = "nameIds != null and nameIds.size() > 0 ">
  and r.id in
  <foreach collection="nameIds" item="id" index="index" open="(" close=")" separator=",">
    #{id}
  </foreach>
</if>

<!-- 排序 -->
<choose>
  <when test="query.sortField == null">
    order by r.id desc
  </when>

  <!-- 0-app排序-->
  <otherwise>
    <choose>
      <when test="query.sortField == 0">
          order by (case r.state when 2 then 0 else 1 end) asc, r.id asc
      </when>
    </choose>
  </otherwise>
</choose>

<!-- 时间跨度 -->
<if test="createTimeStart != null">
  and a.create_time <![CDATA[ >= ]]> #{createTimeStart,jdbcType=TIMESTAMP}
</if>
     
<if test="createTimeEnd != null">
  and a.create_time <![CDATA[ <= ]]> #{createTimeStart,jdbcType=TIMESTAMP}
</if>

<!-- 批量插入 -->
<insert id="insertList">
  INSERT INTO demo( id, standard_price, bottom_price, modifer, modify_time )VALUES
  <foreach collection="list" item="element" index="index" separator=",">
    (
      #{element.id,jdbcType=BIGINT},
      #{element.standardPrice,jdbcType=DECIMAL},
      #{element.bottomPrice,jdbcType=DECIMAL},
      #{element.modifer,jdbcType=VARCHAR},
      #{element.modifyTime,jdbcType=TIMESTAMP}
    )
  </foreach>
</insert>

<!-- 批量更新 -->
<update id="updateBatch" parameterType="java.util.List">
    <!--@mbg.generated-->
    update demo
    <trim prefix="set" suffixOverrides=",">
      <trim prefix="company_id = case" suffix="end,">
        <foreach collection="list" index="index" item="item">
          when id = #{item.id,jdbcType=BIGINT} then #{item.companyId,jdbcType=BIGINT}
        </foreach>
      </trim>
      <trim prefix="project_id = case" suffix="end,">
        <foreach collection="list" index="index" item="item">
          when id = #{item.id,jdbcType=BIGINT} then #{item.projectId,jdbcType=BIGINT}
        </foreach>
      </trim>
    </trim>
    where id in
    <foreach close=")" collection="list" item="item" open="(" separator=", ">
      #{item.id,jdbcType=BIGINT}
    </foreach>
</update>

MyBatis Plus 查询方法

函数名说明
eq=
ne<>
gt>
ge>=
lt<
le<=
betweenbetween
notBetweennot between
likelike %key%
notLikenot like %key%
likeLeftlike %key
likeRightlike key%
isNullis null
isNotNullis not null
inin
notInnot in
inSqlin (sql语句)
notInSqlnot in (sql语句)
groupBygroup by
orderByAscorder by 字段 asc
orderByDescorder by 字段 desc
orderByorder by 字段 asc/desc
havinghaving(sql语句)
ora or b
anda and b
apply拼接sql,消除sql注入风险
last无视优化规则,直接拼接到sql的最后(有sql注入的风险)
existsexists(sql语句)
notExistsnot exists(sql语句)
nested正常嵌套,不带and或者or