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>

		</trim>
	</select>

默认生成的是这样,当没有传递任何一个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}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值