MyBatis之动态SQL

一、需求分析

MyBatis框架的动态SQL,能根据不同的条件拼接SQL语句,还能确保不能遗漏必要的空格、标点符号等,功能非常强大。

1.if元素

当客户名称不为空,根据客户名称进行客户筛选; 当客户职业不为空,根据客户职业进行客户筛选。

2.choose、when、otherwise元素

当客户名称不为空,则只根据客户名称进行客户筛选;当客户名称为空,而客户职业不为空,则只根据客户职业进行客户筛选。当客户名称和客户职业都为空,则要求查询出所有电话不为空的客户信息。

3.where、trim元素

实现“拼接”功能。

4.set元素

实现动态更新功能。

5.foreach元素

迭代List类型、数组类型、Map类型

二、资料、视频教程

资料下载链接

03 MyBatis之动态SQL

三、编码实现

1、搭建环境
1)数据库环境

mybatis数据库,t_customer表

2)引入依赖

pom.xml文件

3)数据库连接的配置文件

src/main/resources,数据库连接的配置文件db.properties。

4)MyBatis核心配置文件

src/main/resources,MyBatis的核心配置文件mybatis-config.xml

5)数据封装类

src/main/java,新建com.sw.pojo包,新建Customer类

public class Customer {
    private Integer id;
    private String username;
    private String jobs;
    private String phone;
    //get、set
    //tostring
}

6)mapper接口

src/main/java创建com.sw.mapper包,新建CustomerMapper接口。

7)映射文件

src/main/resources目录下,新建com/sw/mapper目录,CustomerMapper.xml

2、if元素

当客户名称不为空,根据客户名称进行客户筛选; 当客户职业不为空,根据客户职业进行客户筛选。

1)修改接口CustomerMapper

List<Customer> getListByIf(Customer customer);

2)修改映射文件CustomerMapper.xml

    <select id="getListByIf" parameterType="Customer" resultType="Customer">
        select * from t_customer where 1=1
        <if test="username != null and username!=''">
            and username like concat('%',#{username},'%')
        </if>
        <if test="jobs != null and jobs!=''">
            and jobs = #{jobs}
        </if>
    </select>

3)测试

    public void testGetListByIf() {
        Customer customer = new Customer();
//        customer.setUsername("j");
        customer.setJobs("worker");
        List<Customer> customerList = customerMapper.getList(customer);
        for (Customer c:customerList) {
            System.out.println(c);
        }
    }
3、choose、when、otherwise元素

当客户名称不为空,则只根据客户名称进行客户筛选;当客户名称为空,而客户职业不为空,则只根据客户职业进行客户筛选。当客户名称和客户职业都为空,则要求查询出所有电话不为空的客户信息。

1)修改接口CustomerMapper

List<Customer> getListByChoose(Customer customer);

2)修改映射文件CustomerMapper.xml

    <select id="getListByChoose" parameterType="Customer" resultType="Customer">
        select * from t_customer where 1=1
        <choose>
            <when test="username != null and username!=''">
                and username like concat('%',#{username},'%')
            </when>
            <when test="jobs != null and jobs!=''">
                and jobs = #{jobs}
            </when>
            <otherwise>
                and phone is not null and phone != ''
            </otherwise>
        </choose>
    </select>

3)测试

4、where、trim元素

修改接口CustomerMapper,getListByChoose方法和getListByChoose方法

5、set元素

只更新需要更新的字段

1)修改接口CustomerMapper

int updateOne(Customer customer);

2)修改映射文件CustomerMapper.xml

    <update id="updateOne" parameterType="Customer">
        update  t_customer
        <set>
            <if test="username != null and username!=''">
                username = #{username}
            </if>
            <if test="jobs != null and jobs!=''">
                jobs = #{jobs}
            </if>
            <if test="phone != null and phone!=''">
                phone = #{phone}
            </if>
        </set>
        where id = #{id}
    </update>

3)测试

6、foreach元素
1)迭代List类型

根据所给id查找员工列表

(1)修改接口CustomerMapper

List<Customer> getListByIdList(List<Integer> ids);

(2)修改映射文件CustomerMapper.xml

    <select id="getListByIdList" parameterType="java.util.List" resultType="Customer">
        SELECT * FROM t_customer WHERE id in
        <foreach collection="list" index="index" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>

(3)测试

2)迭代数组类型

根据所给id查找员工列表

(1)修改接口CustomerMapper

List<Customer> getListByIdArray(Integer[] ids);

(2)修改映射文件CustomerMapper.xml

    <select id="getListByIdArray" parameterType="java.util.Arrays" resultType="Customer">
        SELECT * FROM t_customer WHERE id in
        <foreach collection="array" index="index" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>

(3)测试

Integer[] ids = new Integer[2];
3)迭代Map类型

根据id(多个)和jobs(单个)查找员工列表

(1)修改接口CustomerMapper

List<Customer> getListByMap(Map<String,Object> map);

(2)修改映射文件CustomerMapper.xml

    <select id="getListByMap" parameterType="java.util.Map" resultType="Customer">
        SELECT * FROM t_customer
        <where>
            <if test="jobs != null and jobs!=''">
                and jobs = #{jobs}
            </if>
            <if test="ids != null">
                and id in
                <foreach collection="ids" index="index" item="id" open="(" separator="," close=")">
                    #{id}
                </foreach>
            </if>
        </where>
    </select>

(3)测试

        Map<String,Object> map = new HashMap<String, Object>();
        List<Integer> ids = new ArrayList<Integer>();
        ids.add(1);
        ids.add(3);
        map.put("ids",ids);
        map.put("jobs","farmer");

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

勇 士 Teacher

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值