【快速学习---MySql之动态sql】

文章介绍了MyBatis中动态SQL的使用,特别是`where`标签如何根据条件构建查询语句,避免无效的条件过滤。`foreach`标签则用于循环遍历集合,构造IN查询,适用于批量处理。示例代码展示了在实际开发中如何根据用户输入灵活构建查询条件。
摘要由CSDN通过智能技术生成

提示:本文用于学习记录,个人强化理解


前言

动态SQL就是指根据不同的条件生成不同的SQL语句


一、where

//下面的sql语句的作用是根据学生的年龄和名称查询学生的所有信息
<select id="queryStudentList" parameterType="StudentQueryVO" resultType="StudentCustom">
        SELECT * FROM student WHERE old=#{student.old} and name like '%${student.name}%'
</select>

上述代码在实际开发中总是用于分页查询语句中,例如某页面的查询功能(用户可根据姓名,编号,性别等进行输入查询),因此就会出现用户可能仅仅只输入某一个参数或者同时输入多个参数的情况,上面的sql语句就明显存在着不足。这里就需要使用关于where的动态sql。

动态sql–where
实际开发需求:前端搜索界面,用户进行输入,参数个数类型不限,进行查询
动态sql实战代码如下:

    <!--查询列表信息-->
    <select id="jiangJunQueryGoodsManageSetInfoList" parameterType="com.yinhai.team10.jiangjun.vo.JiangJunGoodsManageSetInfoVo" resultMap="goodsManageResult">
        select
        distinct
        po.product_id,
        po.product_name,
        py.spe_id,
        po.brand_id,
        pr.sup_name,
        pd.brand_name,
        po.product_type,
        py.min_unit,
        py.multi_unit,
        py.min_unit_section,
        py.multi_unit_section
        from productinfo po
        left join productbrand pd on pd.brand_id = po.brand_id
        left join productspecify py on py.product_id = po.product_id
        left join productsupplier pr on pr.sup_id = po.sup_id
        <where>

            <if test="productId != null">
                and product_id = #{productId}
            </if>

            <if test="productName != null and productName != ''">
                and product_name like concat('%',#{productName},'%')
            </if>

            <if test="supId != null and supId != ''">
                and po.sup_id = #{supId}
            </if>

            <if test="brandId != null and brandId != ''">
                and po.brand_id = #{brandId}
            </if>

            <if test="productType != null and productType != ''">
                and product_type = #{productType}
            </if>
            
            </where>

上述sql语句比较长,但是总的作用就是:对产品Id进行精确查询,产品名称进行模糊查询,以及商家Id,品牌Id,产品型号进行查询。用户可以在前端界面随意输入,都能进行查询。

二、foreach

// 查询出id为1、2、3的学生信息
SELECT * FROM student WHERE (id=1 OR id=2 OR id=3)

其实foreach主要就是来代替where后面的部分进行循环查询

StudentMapper

List<Student>qureyStudent(List<Integer> id);
StudentMapper.xml

<select id="qureyStudent" resultType="int" resultMap="studentMap">
        select * from student where id in
     <foreach collection="list" item="id" open="(" close=")" separator=",">
         #{id}
     </foreach>
</select>

foreach表达式
collection属性: 必填,指定参数入参类型 列表(list) 、数组(array)、HashMap(map)
item属性:起名字,给集合中单个元素起名称
open属性:开始字符串
close属性:结束字符串
separator属性:分割符
index属性:索引的属性名,在集合数组下值为当前的索引值

在实际开发中主要用于进行批量处理,理解起来也不是很困难。

总结

总的来说这些简单的使用起来也不是很困难,在开发过程中不会写的时候,可以先写成我们熟悉的sql语句,然后在慢慢的改写成,拼装成所能达到需求的动态sql!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值