05_MyBatis动态SQL学习笔记

参考学习网站

动态sql目的是为了解决查询条件不确定而存在的,条件判断使用的表达式为OGNL 表达式,常用的动态SQL有:<if> <where> <choose> <foreach>

注意

在 mapper 的动态 SQL 中若出现大于号(>)、小于号(<)、大于等于号(>=),小于等于号(<=)等符号,最好将其转换为实体符号。否则,XML 可能会出现解析出错问题。

特别是对于小于号(<),在 XML 中是绝对不能出现的。否则,一定出错。

<if> 标签

为了解决两个条件均未做设定的情况,在 where 后添加了一个“1=1”的条件。这样就不至于两个条件均未设定而出现只剩下一个 where,而没有任何可拼接的条件的不完整 SQL 语句。

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.lusifer.mybatis.dao.DynamicStudentDao">
    <!-- if -->
    <select id="selectByIf" resultType="com.lusifer.mybatis.entity.Student">
        SELECT
            id,
            name,
            age,
            score
        FROM
            student
        WHERE 1 = 1
        <if test="name != null and name != ''">
            AND name LIKE concat('%', #{name}, '%')
        </if>
        <if test="age != null and age > 0">
            AND age > #{age}
        </if>
    </select>
</mapper>

<where> 标签

<if/> 标签的中存在一个比较麻烦的地方:需要在 where 后手工添加1=1 的子句。因为,若 where 后的所有 <if/> 条件均为 false,而 where 后若又没有 1=1 子句,则 SQL 中就会只剩下一个空的 whereSQL 出错。所以,在 where 后,需要添加永为真子句 1=1,以防止这种情况的发生。但当数据量很大时,会严重影响查询效率。

where标签是为了解决前and问题.

<!-- where-->
<select id="selectByWhere" resultType="com.lusifer.mybatis.entity.Student">
    SELECT
        id,
        name,
        age,
        score
    FROM
      student
    <where>
        <if test="name != null and name != ''">
            AND name LIKE concat('%', #{name}, '%')
        </if>
        <if test="age != null and age > 0">
            AND age > #{age}
        </if>
    </where>
</select>


<choose> 标签

该标签中只可以包含 ,可以包含多个 与一个 。它们联合使用,完成 Java 中的开关语句 switch…case 功能。

本例要完成的需求是,若姓名不空,则按照姓名查询;若姓名为空,则按照年龄查询;若没有查询条件,则没有查询结果。

<!-- choose -->
<select id="selectByChoose" resultType="com.lusifer.mybatis.entity.Student">
    SELECT
        id,
        name,
        age,
        score
    FROM
      student
    <where>
        <choose>
            <when test="name != null and name != ''">
                AND name LIKE concat('%', #{name}, '%')
            </when>
            <when test="age != null and age > 0">
                AND age > #{age}
            </when>
            <otherwise>
                AND 1 != 1
            </otherwise>
        </choose>
    </where>
</select>

<foreach> 标签

  • <foreach/> 标签用于实现对于数组与集合的遍历。对其使用,需要注意:
  • collection 表示要遍历的集合类型,这里是数组,即 array。
  • opencloseseparator 为对遍历内容的 SQL 拼接。
package shop.tbzj.mapper;
import shop.tbzj.entity.Order;
import java.util.List;

public interface OrderMapper {

    public void insertOrder(Order order);

    public List<Order> findOrderByIds(List<Long> ids);

    public List<Order> findOrderByArrayIds(Long[] ids);

}

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="shop.tbzj.mapper.OrderMapper">
    <insert id="insertOrder" useGeneratedKeys="true" keyProperty="id">
          insert into tb_order(name,price)values(#{name},#{price});
    </insert>


    <select id="findOrderByIds" resultType="shop.tbzj.entity.Order">
        select * from tb_order
            <if test="list !=null and list.size !=0">
                WHERE id IN
                <foreach collection="list" open="(" close=")" item="id" separator=",">
                    #{id}
                </foreach>
            </if>
    </select>

    <select id="findOrderByArrayIds" parameterType="long"  resultType="shop.tbzj.entity.Order">
        select * from tb_order
        <if test="array !=null and array.length !=0">
            WHERE id IN
            <foreach collection="array" open="(" close=")" item="id" separator=",">
                #{id}
            </foreach>
        </if>
    </select>


</mapper>

注意传递的参数如果是List<Long>ids 那么collection就需要些list

如果传递的参数为Long[] ids 那么collection就需要些array

package shop.tbzj.test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import shop.tbzj.entity.Order;
import shop.tbzj.mapper.OrderMapper;

import java.util.ArrayList;
import java.util.List;


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring-context.xml")
public class TestOrder {

    @Autowired
    private OrderMapper orderMapper;

    @Test
    public void insertOrder() {
        Order order = new Order();
        order.setName("tom2");
        order.setPrice(22.3);

        orderMapper.insertOrder(order);

        System.out.println(order.toString());
    }

    @Test
    public void findOrderByIds() {
        List<Long> ids = new ArrayList<Long>();
        ids.add(4L);
        ids.add(5L);
        List<Order> orders = orderMapper.findOrderByIds(ids);
        for (Order order : orders) {
            System.out.println(order);
        }

    }

    @Test
    public void findOrderByIds2() {
        Long[] ids = {4L, 5L};
        List<Order> orders = orderMapper.findOrderByArrayIds(ids);
        for (Order order : orders) {
            System.out.println(order);
        }

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值