【MyBatis】动态SQL的基本用法


动态SQL

mybatis动态SQL,采用的基于OGNL表达式来操作的。根据表达式的不同进行动态的SQL的拼接、组装。

  • 动态SQL标签:
    if标签where标签trim标签(where、set)foreach标签

1、if标签

if标签通常用于where语句中,通过判断参数值来决定是否使用某个查询条件条件,它也经常用于update语句中判断是否更新某一个字段,还可以在insert语句中用来判断是否插入某个字段的值

  • 接口文件:
public interface Student23Mapper {

    public List<Student23> selectStudent(Student23 student23);
    
}
  • XML文件:
<?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:命名空间,接口文件的全路径-->
<mapper namespace="com.tulun.inteface.Student23Mapper">
<!--
        if表达式
        一般使用是放在where条件后面
        判断参数是否传递使用if test属性(必填)为true或者false
        test使用OGNL表达式处理,返回true则进入到if标签的SQL,返回为false则不会进入if标签
    -->
    <select id="selectStudent" parameterType="student23" resultType="com.tulun.pojo.Student23">
        select * from Student where 1=1
        <if test="SID != null and SID != 0">
            and SID = #{SID}
        </if>
        <if test="Sage != null">
            and Sage = #{Sage}
        </if>
    </select>
</mapper>

在这里插入图片描述

public class Student23MapperTest {
    SqlSessionFactory sessionFactory = null;
    /**
     * 在编写测试方法之前,经常会在执行测试之前需要创建一些全局对象,单例对象
     * 在运行用例即@Test注解之前运行
     * @throws IOException
     */
    @Before
    public void before() throws IOException {
        //获取全局配置文件
        String path = "mybatis-config.xml";
        //通过mybatis提供共的Resources类来获取配置文件流
        InputStream inputStream = Resources.getResourceAsStream(path);

        //获取SQLSessionFactory对象
        sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

    }

    @Test
    public void selectStudent()throws IOException {
        //获取会话工厂
        SqlSession sqlSession = sessionFactory.openSession();

        //通过反射机制来后去到mapper的实例,getMapper的参数是接口名
        Student23Mapper student23Mapper = sqlSession.getMapper(Student23Mapper.class);

        //SID和Sage都传递
        Student23 student23 = new Student23();
        student23.setSID(1);
        student23.setSage("21");
        student23Mapper.selectStudent(student23);
        //select * from Student where 1=1 and SID = ? and Sage = ?

        //SID传递
        Student23 student231 = new Student23();
        student231.setSID(1831050097);
        student23Mapper.selectStudent(student231);
        //select * from Student where 1=1 and SID = ?

        //Sage传递
        Student23 student232 = new Student23();
        student232.setSage("21");
        student23Mapper.selectStudent(student232);
        //select * from Student where 1=1 and Sage = ?

        //不传递参数
        Student23 student233 = new Student23();
        student23Mapper.selectStudent(student233);
        // select * from Student where 1=1

        sqlSession.close();
    }
 }

2、where标签

where标签作用:如果该标签包含的元素有返回值,就插入一个where,如果where后面的字符串是以and或者or开头的且无参数传递,则将他们去除。

<select id="selectStudent" parameterType="student23" resultType="com.tulun.pojo.Student23">
    select * from Student
    <where>
        <if test="SID != null and SID != 0">
            and SID = #{SID}
        </if>
        <if test="Sage != null">
            and Sage = #{Sage}
        </if>
    </where>
</select>

在这里插入图片描述
where表达式一般和if表达式一块使用,如果条件一个都不满足,则不拼接where条件;如果有一个或者多个if表达式,where会直接拼接在SQL上,并且紧随where的表达式的and或者or会被忽略掉。

3、foreach标签

需要根据SID批量的查询student信息
select * from Student where SID in(1,2,3,4,5);

  • 接口文件:
public List<Student23> selectStudentByIds(List<Integer> id);
  • XML文件:
    <select id="selectStudentByIds"  resultType="com.tulun.pojo.Student23">
        select * from Student where SID in
        <foreach collection="list"  item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </select>
  • test
    @Test
    public void selectStudentByIds()throws IOException{
        //获取会话工厂
        SqlSession sqlSession = sessionFactory.openSession();

        //通过反射机制来后去到mapper的实例,getMapper的参数是接口名
        Student23Mapper student23Mapper = sqlSession.getMapper(Student23Mapper.class);

        ArrayList<Integer> id = new ArrayList<>();
        id.add(1);
        id.add(2);
        id.add(3);
        student23Mapper.selectStudentByIds(id);
        //select * from Student where SID in ( ? , ? , ? ) 
    }

在这里插入图片描述
foreach标签属性解释:

  • foreach表达式 collection属性,必填,指定的入参的类型
    列表:list
    数组:array
    hashmap:map
  • item属性:起名字,给集合中单个元素亲名称
  • open属性:开始的字符串
  • close属性:结束的字符串
  • separator属性:数据之间的分隔符

这里就暂时介绍这些,其他标签基本用法请关注后续更新!

 

 
 
 
 

总结

通过动态SQL可以避免在Java代码中处理繁琐的业务逻辑,通过将大量的判断写入到MyBatis的映射层可以极大程度上提高我们的逻辑应变能力。当有一般的业务逻辑改动时,通常只需要在映射层通过动态SQL即可实现。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值