Mybatis learning: Mybatis dynamic SQL

MyBatis dynamic SQL

Dynamic SQL means Business Process splicing SQL statements dynamically based on different criteria

if statement is
a.if typically used where clause, is determined by parameter determination execution condition screening
test for checking the parameters
when the condition is automatically deleted if and

 <select id="selectByParams" parameterType="map" resultType="user">
    select * from user
    <where>
      <if test="name != null and name.length()>0" >and name=#{name}</if> #先进行null判断,再对空字符串判断
    <where>
  </select>

. b using the Update if update
scenarios: update does not have the original value is updated to become empty or null
SET modification value

<update id="updateByPrimaryKeySelective" parameterType="RecruitmentConfBanner">
        UPDATE conf_banner t
        <set>
            <if test="bannerName != null">
                t.banner_name = #{bannerName},
            </if>
        </set>
        where t.banner_id = #{bannerId}
    </update>

. C using the Insert if
scenarios: If the incoming value is not empty, the value is passed; if it is empty, the default value of the database.
values should be used if both sides

    <insert id="insert">
        insert into sys_user(
          <if test="userEmail !=null and userEmail != ''">
              user_email,
          </if>
        )
        VALUES
        (
        <if test="userEmail != null and userEmail !=''">
            user_email = #{userEmail},
        </if>
    </insert>

choose Usage
scenarios: implements the else ... IF
the WHERE 1 = 1 never really play a role to return all results
where 1 = 2 does not return any results

<select id="findUserInfoByOneParam" parameterType="Map" resultMap="UserInfoResult">
        select * from userinfo
               where 1=1
        <choose>
            <when test="searchBy=='department'">
                and department=#{department}
            </when>
            <when test="searchBy=='position'">
                and position=#{position}
            </when>
            <otherwise>
                and 1=2
            </otherwise>
        </choose>
    </select>  

where, set, trim usage

  • where if there where results are returned, will be behind the string is the beginning of the AND and OR, automatically beginning the AND / OR delete
   <where>
      <if test="name != null and name.length()>0" >and name=#{name}</if> #先进行null判断,再对空字符串判断
    <where>
  • If you have to return the result set will remove the last comma
  <update>
    update user
    <set>
      <if test="name != null and name.length()>0">name = #{name},</if>
      <if test="gender != null and gender.length()>0">gender = #{gender},</if>
    </set>
    where id = #{id}
  </update>
  • where and functional set of labels can be used to achieve the trim tag
   where功能的实现 
   <trim prefix="WHERE" prefixoverride="AND |OR">
    <if test="name != null and name.length()>0"> AND name=#{name}</if>
  </trim>

    set功能的实现
  <trim prefix="set" suffixoverride="," suffix=" where id = #{id} ">
    <if test="name != null and name.length()>0"> name=#{name} , </if>
  </trim>

foreach implemented in a set of
scenario: select an object within a certain range id
parameter interface is required here set Long [] idArray

<select id="dynamicForeachTest" parameterType="java.util.List" resultType="Blog">
 select * from t_blog where id in 
 <foreach collection="list" index="index" item="item" open="(" separator="," close=")"> 
 #{item} 
 </foreach> 
 </select>

bind usage
scenario: create a variable and bind it to the context, grammar solve the problem caused by switching database

     <!-- List<Employee> getEmpsTestInnerParameter(Employee employee); -->
      <select id="getEmpsTestInnerParameter" resultType="com.hand.mybatis.bean.Employee">
          <!-- bind:可以将OGNL表达式的值绑定到一个变量中,方便后来引用这个变量的值 -->
          <bind name="bindeName" value="'%'+eName+'%'"/> eName是employee中一个属性值
          SELECT * FROM emp
          <if test="_parameter!=null">
            where ename like #{bindeName}
          </if>
      </select>

Guess you like

Origin blog.csdn.net/weixin_40990818/article/details/85651710