MyBatis映射文件深入

本文详细介绍了MyBatis中动态SQL的使用,包括<if>标签实现条件判断和<foreach>标签进行循环拼接SQL,以及如何通过SQL片段抽取来实现SQL的复用。通过实例展示了如何在映射文件中配置这些元素,以提高代码的可读性和维护性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

1.动态sql语句

1.1动态sql语句概述

1.2动态SQL之

1.3动态SQL之

2.SQL片段抽取

3.小结


1.动态sql语句

1.1动态sql语句概述

Mybatis的映射文件中,有些sql语句是比较简单的,有些时候业务逻辑复杂时,SQL是动态变化的。

1.2动态SQL之<if>

根据实体类的不同取值,使用不同的SQL语句来进行查询,比如在id如果不为空时可以根据id查询,如果username不为空时还要加入用户名作为条件。。。这种情况在我们的多条件组合查询中经常会碰到。

<?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.caoyan.mapper.UserMapper">
    <select id="findByCondition" parameterType="user" resultType="user">
        select * from user
        <where>
            <if test="id!=0">
                and id=#{id}
            </if>
            <if test="username!=null">
                and username=#{username}
            </if>
            <if test="password!=null">
                and password=#{password}
            </if>
        </where>
    </select>
</mapper>

1.3动态SQL之<foreach>

循环执行sql的拼接操作,例如:SELECT * FROM USER WHERE ID IN (1,2,5)

    <select id="findByIds" parameterType="list" resultType="user">
        select * from user
        <where>
            <foreach collection="list" open="id in(" close=")" item="id" separator=",">
                #{id}
            </foreach>
        </where>
    </select>
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import com.caoyan.domain.User;
import com.caoyan.mapper.UserMapper;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

public class MapperTest {
    @Test
    public void test1() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        // 模拟条件user
        User condition = new User();
        // condition.setId(1);
        // condition.setUsername("zhangsan");
        // condition.setPassword("123");
        // List<User> users = userMapper.findByCondition(condition);
        // System.out.println(users);
        // 模拟ids的数据
        List<Integer> ids = new ArrayList<Integer>();
        ids.add(1);
        ids.add(3);
        List<User> userList = userMapper.findByIds(ids);
        System.out.println(userList);
    }
}

2.SQL片段抽取

Sql中可将重复的SQL提取出来,使用时用include引用即可,最终达到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.caoyan.mapper.UserMapper">
    <!-- 抽取sql片段简化编写 -->
    <sql id="selectUser">select * from user</sql>
    <select id="findByCondition" parameterType="user" resultType="user">
        <include refid="selectUser"></include>
        <where>
            <if test="id!=0">
                and id=#{id}
            </if>
            <if test="username!=null">
                and username=#{username}
            </if>
            <if test="password!=null">
                and password=#{password}
            </if>
        </where>
    </select>
    <select id="findByIds" parameterType="list" resultType="user">
        <include refid="selectUser"></include>
        <where>
            <foreach collection="list" open="id in(" close=")" item="id" separator=",">
                #{id}
            </foreach>
        </where>
    </select>
</mapper>

3.小结

MyBatis映射文件配置:

  • <select>:查询
  • <insert>:插入
  • <update>:修改
  • <delete>:删除
  • <where>:where条件
  • <if>:if判断
  • <foreach> :循环
  • <sql>:sql片段抽取
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Caoyy686868

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

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

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

打赏作者

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

抵扣说明:

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

余额充值