mybatis dynamic sql selective does not pass condition where redundant error reporting

Keywords: Mybatis

Links to the original text: https://www.cnblogs.com/qq1141100952com/p/10478047.html
<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>

		</trim>
	</select>

By default, it generates an error when no condition behind where is passed.
Tried.
Several kinds

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

Solution:
Reprinted: https://www.cnblogs.com/q1141100952 com/p/10478047.html

where 1=1 or where tag
/**
* Conditional Retrieval Based on Input Student Information
* 1. When only the user name is input, the user name is used for fuzzy retrieval.
* 2. When only inputting mailboxes, use gender to match perfectly
* 3. Use these two conditions to match queries when both user name and gender exist
* @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>

Skills of mybatis dynamic stitching conditions:

Tip 1: where 1=1, at this point, you can query by name, sex is empty or not.

Tip 2: Put it in the where tag

<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>

Dynamic update (to determine whether it is empty)

<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}

Posted by eyedol on Fri, 04 Oct 2019 21:24:03 -0700