mybatis -- 动态sql增删改

版权声明:[ws - 兮的博客] - 空间专属,未经声明不得私自转载 https://blog.csdn.net/qq_41463655/article/details/82321076
<!-- ============================ 动态查询 =================================== -->
<!-- 
   <where>用于动态条件组合查询,可以去掉where后的第一个and
 -->
<select id="selectPersonByCondition" parameterType="qc" resultMap="BaseResultMap">
   select * from person t
   <where>
      <if test="name != null">
         t.name like '%${name}%'
      </if>
      <if test="gender != null">
         and t.gender = #{gender}
      </if>
      <if test="personAddr != null">
         and t.person_addr like '%${personAddr}%'
      </if>
      <if test="birthday != null">
         <![CDATA[
            and t.birthday < #{birthday}
         ]]>
      </if>
   </where>
</select>



<!-- ============================ 动态修改 =================================== -->
<!-- 
   <SET>能处理掉最后一个逗号,不要忘记加上t.person_id = #{personId},
   t.person_id = #{personId}:无其他属性修改,防止sql拼接出错
 -->
<update id="dynamicUpdate" parameterType="person">
   update person t 
   <set>
      t.person_id = #{personId},
      <if test="name != null">
         t.name = #{name},
      </if>
      <if test="gender != null">
         t.gender = #{gender},
      </if>
      <if test="personAddr != null">
         t.person_addr = #{personAddr},
      </if>
      <if test="birthday != null">
         t.birthday = #{birthday}
      </if>
   </set>
   where t.person_id = #{personId}
</update>



<!-- ============================ 多个Id 查询 =================================== -->
<!--                  
   (1,2,3)
   map.put("ids", Integer[])

   foreach遍历集合来组装sql
   collection:map中集合的key
   open:以某种字符开始
   close:以某种字符结束
   item:集合中的元素
   separator:以某种字符分隔
   index:当前遍历到的索引号
 -->
<select id="selectPersonByIn" parameterType="map" resultMap="BaseResultMap">
   select * from person t where t.person_id in 
   <foreach collection="ids" open="(" close=")" item="personId" separator="," index="index">
      #{personId}
   </foreach>
</select>


<!-- ============================ 多条数据删除 =================================== -->
<delete id="deleteBatch" parameterType="map">
   delete from person where person_id in
   <foreach collection="ids" open="(" close=")" item="personId" separator="," index="index">
      #{personId}
   </foreach>
</delete>



<!-- ============================ 多条数据 添加 =================================== -->
<!--                              太多内存溢出(分批)
   map.put("personList", List<Person> list)
   insert into person(id, name)values(1, 'zhansan'),(2, 'lisi'),...
   selectKey : 返回主建Id
 -->
<insert id="insertBatch" parameterType="map">  
   <selectKey keyProperty="personId" order="AFTER" resultType="int">
      select LAST_INSERT_ID()
   </selectKey>
   insert into person (person_id, name, gender, person_addr, birthday)
   values
   <foreach collection="personList" separator="," item="person">
      (#{person.personId}, #{person.name}, #{person.gender}, #{person.personAddr}, #{person.birthday})
   </foreach>
</insert>


------------------------ 增删查 - 内存溢出处理方法 ----------------
    if(i%100 == 0){
      map.put("personList", pList);
      session.insert("com.rl.mapper.PersonMapper.insertBatch", map);
      pList.clear();
   }
}   //循环外
map.put("personList", pList);
session.insert("com.rl.mapper.PersonMapper.insertBatch", map);

猜你喜欢

转载自blog.csdn.net/qq_41463655/article/details/82321076