MyBatis条件查询总结

1.if条件语句

<!--  if(判断参数) - 将实体类不为空的属性作为where条件 -->  
    <select id="getStudentList_if" resultMap="resultMap_studentEntity" parameterType="liming.student.manager.data.model.StudentEntity">  
        SELECT ST.STUDENT_ID,  
               ST.STUDENT_NAME,  
               ST.STUDENT_SEX,  
               ST.STUDENT_BIRTHDAY,  
               ST.STUDENT_PHOTO,  
               ST.CLASS_ID,  
               ST.PLACE_ID  
          FROM STUDENT_TBL ST   
         WHERE  
        <if test="studentName !=null ">  
            ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName, jdbcType=VARCHAR}),'%')  
        </if>  
        <if test="studentSex != null and studentSex != '' ">  
            AND ST.STUDENT_SEX = #{studentSex, jdbcType=INTEGER}  
        </if>  
        <if test="studentBirthday != null ">  
            AND ST.STUDENT_BIRTHDAY = #{studentBirthday, jdbcType=DATE}  
        </if>  
        <if test="classId != null and classId!= '' ">  
            AND ST.CLASS_ID = #{classId, jdbcType=VARCHAR}  
        </if>  
        <if test="classEntity != null and classEntity.classId !=null and classEntity.classId !=' ' ">  
            AND ST.CLASS_ID = #{classEntity.classId, jdbcType=VARCHAR}  
        </if>  
        <if test="placeId != null and placeId != '' ">  
            AND ST.PLACE_ID = #{placeId, jdbcType=VARCHAR}  
        </if>  
        <if test="placeEntity != null and placeEntity.placeId != null and placeEntity.placeId != '' ">  
            AND ST.PLACE_ID = #{placeEntity.placeId, jdbcType=VARCHAR}  
        </if>  
        <if test="studentId != null and studentId != '' ">  
            AND ST.STUDENT_ID = #{studentId, jdbcType=VARCHAR}  
        </if>   
    </select> 

2.choose (when otherwise)

<!--  choose(判断参数) - 按顺序将实体类 User 第一个不为空的属性作为:where条件 -->  
<select id="getUserList_choose" resultMap="resultMap_user" parameterType="com.yiibai.pojo.User">  
    SELECT *  
      FROM User u   
    <where>  
        <choose>  
            <when test="username !=null ">  
                u.username LIKE CONCAT(CONCAT('%', #{username, jdbcType=VARCHAR}),'%')  
            </when >  
            <when test="sex != null and sex != '' ">  
                AND u.sex = #{sex, jdbcType=INTEGER}  
            </when >  
            <when test="birthday != null ">  
                AND u.birthday = #{birthday, jdbcType=DATE}  
            </when >  
            <otherwise>  
            </otherwise>  
        </choose>  
    </where>    
</select>  
<select id="dynamicChooseTest" parameterType="Blog" resultType="Blog">
        select * from t_blog where 1 = 1 
        <choose>
            <when test="title != null">
                and title = #{title}
            </when>
            <when test="content != null">
                and content = #{content}
            </when>
            <otherwise>
                and owner = "owner1"
            </otherwise>
        </choose>
</select>

4.in的用法

    <select id="getNewListByLabelID" resultMap="BaseResultMap" parameterType="arraylist">
        SELECT
        <include refid="Base_Column_List"/>
        FROM tb_problem
        WHERE id IN
        <foreach collection="ids" index="index" item="id" separator="," close=")" open="(">
            #{id}
        </foreach>
    </select>

还可以这样

<select id="queryAllOpenProduct" parameterType="com.tims.open.domain.OpenProductQueryCondition"
    resultType="com.tims.open.domain.OpenProduct">
    SELECT
         *
    FROM
        product_db.product p
    WHERE
        p.isvalid = 1
    <if test="list != null">
        <foreach collection="list" index="index" item="item" separator="," open="AND p.id IN (" close=")">
               #{item}
        </foreach>
    </if>
</select>
//查询condition类
Public OpenProductQueryCondition{
    private Integer productId;  
    private List<Integer> list;
}

5.模糊查询

<!-- ******************** 模糊查询的常用的3种方式:********************* -->
    <select id="getUsersByFuzzyQuery" parameterType="User" resultType="User">
        select <include refid="columns"/> from users
        <where>
            <!--
                方法一: 直接使用 % 拼接字符串 
                注意:此处不能写成  "%#{name}%" ,#{name}就成了字符串的一部分,
                会发生这样一个异常: The error occurred while setting parameters,
                应该写成: "%"#{name}"%",即#{name}是一个整体,前后加上%
            -->
            <if test="name != null">
                name like "%"#{name}"%"
            </if>
            <!--方法二: 使用concat(str1,str2)函数将两个参数连接 -->
            <if test="phone != null">
                and phone like concat(concat("%",#{phone}),"%")
            </if>
            <!--方法三: 使用 bind 标签,对字符串进行绑定,然后对绑定后的字符串使用 like 关键字进行模糊查询 -->
            <if test="email != null">
                <bind name="pattern" value="'%'+email+'%'"/>
                and email like #{pattern}
            </if>
        </where>
    </select>

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis是一款优秀的ORM框架,它支持多条件查询。实现多条件查询的方式是利用MyBatis的特色——动态SQL。动态SQL允许我们根据不同的条件来动态构建SQL语句,从而实现多条件查询的功能。 总结来说,我们可以使用MyBatis的动态SQL特性来构建多条件查询。通过在MyBatis的核心配置文件中定义查询语句,并使用占位符来设置查询条件,我们可以根据需要动态生成查询语句。例如,在UserMapper.xml文件中,我们可以使用<select>元素来定义多条件查询语句,并使用占位符来设置条件。 在测试代码中,我们可以创建一个封装查询条件的对象QueryVo,并设置相应的属性来指定查询条件。然后,将QueryVo对象作为参数传递给查询方法,即可完成多条件查询。 需要注意的是,我们可以通过设置<select>元素的resultType属性来指定查询结果的输出类型。这样,查询结果将会被映射到指定的类型中,方便我们进行后续的处理。 以上就是使用MyBatis进行多条件查询的基本方法和步骤。通过动态SQL和合适的配置,我们可以根据不同的条件进行灵活的查询操作。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [java配合MyBatis条件查询及动态SQL](https://download.csdn.net/download/ybb_ymm/87916420)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Mybatis_多条件查询](https://blog.csdn.net/hxy1625309592/article/details/113527256)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值