mybatis動態sql selective不傳條件 where多余報錯


<select id="selectSelective" resultMap="BaseResultMap"
		parameterType="com.wjh.bean.TUser">
		select
		<include refid="Base_Column_List" />
		from t_user
		where
		<trim suffixOverrides="and">
			<if test="username != null">
				username = #{username,jdbcType=VARCHAR} and
			</if>
			<if test="password != null">
				password = #{password,jdbcType=VARCHAR} and
			</if>
			<if test="photoname != null">
				photoname = #{photoname,jdbcType=VARCHAR} and
			</if>
			<if test="sex != null">
				sex = #{sex,jdbcType=BIT} and
			</if>
			<if test="age != null">
				age = #{age,jdbcType=INTEGER} and
			</if>
			<if test="roleid != null">
				roleid = #{roleid,jdbcType=INTEGER} and
			</if>
			<if test="phone != null">
				phone = #{phone,jdbcType=VARCHAR} and
			</if>
	&lt;/trim&gt;
&lt;/select&gt;

默認生成的是這樣,當沒有傳遞任何一個where后面的條件的時候就會報錯
試了
好幾種

<trim prefixOverrides="where" suffixOverrides="and">
      <if test="payStatus != null" >
        where pay_status = #{payStatus,jdbcType=INTEGER} and,
      </if>
 </trim>

   
   
  
  
          

解決方案:
轉載:https://www.cnblogs.com/qq1141100952com/p/10478047.html

mybatis動態拼接條件的技巧 where 1=1 或者where標簽
/**
* 根據輸入的學生信息進行條件檢索
* 1. 當只輸入用戶名時, 使用用戶名進行模糊檢索;
* 2. 當只輸入郵箱時, 使用性別進行完全匹配
* 3. 當用戶名和性別都存在時, 用這兩個條件進行查詢匹配的用
* @param student
* @return
*/

<select id="selectByStudentSelective" resultMap="BaseResultMap" parameterType="com.homejim.mybatis.entity.Student">
    select
    <include refid="Base_Column_List" />
    from student
    where 1=1
    <if test="name != null and name !=''">
      and name like concat('%', #{name}, '%')
    </if>
    <if test="sex != null">
      and sex=#{sex}
    </if>
  </select>

   
   
  
  
          

mybatis動態拼接條件的技巧:

技巧一:where 1=1 ,此時,就可以根據name,sex是否為空就可以查詢了

技巧二:放在where標簽里面

<select id="selectByStudentSelective" resultMap="BaseResultMap" parameterType="com.homejim.mybatis.entity.Student">
    select
    <include refid="Base_Column_List" />
    from student
 < where>
    <if test="name != null and name !=''">
      and name like concat('%', #{name}, '%')
    </if>
    <if test="sex != null">
      and sex=#{sex}
    </if>

</where>

</select>

動態更新(判斷是否為空)

<update id="updateByPrimaryKeySelective" parameterType="com.homejim.mybatis.entity.Student">
    update student
    <set>
      <if test="name != null">
        `name` = #{name,jdbcType=VARCHAR},
      </if>
      <if test="phone != null">
        phone = #{phone,jdbcType=VARCHAR},
      </if>
      <if test="email != null">
        email = #{email,jdbcType=VARCHAR},
      </if>
      <if test="sex != null">
        sex = #{sex,jdbcType=TINYINT},
      </if>
      <if test="locked != null">
        locked = #{locked,jdbcType=TINYINT},
      </if>
      <if test="gmtCreated != null">
        gmt_created = #{gmtCreated,jdbcType=TIMESTAMP},
      </if>
      <if test="gmtModified != null">
        gmt_modified = #{gmtModified,jdbcType=TIMESTAMP},
      </if>
    </set>
    where student_id = #{studentId,jdbcType=INTEGER}

   
   
  
  
          
原文地址:https://blog.csdn.net/flower_CSDN/article/details/100022000


免責聲明!

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



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