過剰がエラーを通らないMyBatisの動的SQLの選択条件

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

   
   

デフォルトで生成された場合には、あるエラーのバックされるであろう任意の状態のない転送がない場合
しようとした
いくつかの

<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の1 = 1またはラベル
/ **
*検索条件は、学生情報に入力されに従って
1. *入力のみユーザ名、ユーザ名ファジー検索;
*のみ入力2.メールボックス一致する正確なセックスを使用して
* 3.ユーザー名と性別が存在する場合、これらの二つの条件を持つクエリのマッチ
* @param学生
* @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の:

ヒント:= 1、この時間は、あなたが名前を付けることができ、セックスを照会することができます空であります

スキルII:ラベル内側に

<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

おすすめ

転載: www.cnblogs.com/jpfss/p/12156673.html