zoukankan      html  css  js  c++  java
  • 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}

  • 相关阅读:
    I.MX6 mkuserimg.sh 使用
    【HighCharts系列教程】五、版权属性——Credits
    【HighCharts系列教程】四、颜色属性——colors
    【HighCharts系列教程】三、图表属性——chart
    【HighCharts系列教程】二、Highcharts结构及API文档
    【HighCharts系列教程】一、认识Highcharts
    higncharts 编辑Highcharts.com链接
    higncharts 去掉Highcharts.com链接
    Highcharts一些属性
    如何用easyui+JAVA 实现动态拼凑datagrid表格
  • 原文地址:https://www.cnblogs.com/qq1141100952com/p/10478047.html
Copyright © 2011-2022 走看看