• Mybatis映射文件中动态sql语句


    目录

    Mybatis映射文件深入

    动态sql语句

    官方文档中动态sql

    动态SQL之if

    测试示例if

    动态sql之foreach

    测试示例foreach

     sql片段的抽取

    Mybatis映射文件深入知识小结


    Mybatis映射文件深入

    动态sql语句

    概述:Mybatais的映射文件中,前面我们的SQL都是比较简单的,有时候业务逻辑复杂时,我们的sql时动态变化的,此时在其那面学习的sql就不能满足要求了

    官方文档中动态sql

    MyBatis 的强大特性之一便是它的动态 SQL。如果你有使用 JDBC 或其他类似框架的经验,你就能体会到根据不同条件拼接 SQL 语句有多么痛苦。拼接的时候要确保不能忘了必要的空格,还要注意省掉列名列表最后的逗号。利用动态 SQL 这一特性可以彻底摆脱这种痛苦。

    通常使用动态 SQL 不可能是独立的一部分,MyBatis 当然使用一种强大的动态 SQL 语言来改进这种情形,这种语言可以被用在任意的 SQL 映射语句中。

    动态 SQL 元素和使用 JSTL 或其他类似基于 XML 的文本处理器相似。在 MyBatis 之前的版本中,有很多的元素需要来了解。MyBatis 3 大大提升了它们,现在用不到原先一半的元素就可以了。MyBatis 采用功能强大的基于 OGNL 的表达式来消除其他元素。

    • if
    • choose (when, otherwise)
    • trim (where, set)
    • foreach

    动态SQL之if

    测试示例if

    UserMapper接口下

    1. package com_Mybatis_sql.mapper;
    2. import com_Mybatis_sql.pojo.User;
    3. import java.util.List;
    4. public interface UserMapper {
    5. public List findByCondition(User user);
    6. }

     UserMapper2.xml文件下

    1. "1.0" encoding="UTF-8" ?>
    2. mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    3. <mapper namespace="com_Mybatis_sql.mapper.UserMapper">
    4. <select id="findByCondition" parameterType="user" resultType="user">
    5. select *from user
    6. -- 用where标签保住等价于where 1=1,有条件就进入
    7. <where>
    8. <if test="id!=0">
    9. and id=#{id}
    10. if>
    11. <if test="username!=null">
    12. and username=#{username}
    13. if>
    14. <if test="password!=null">
    15. and password=#{password}
    16. if>
    17. where>
    18. select>
    19. mapper>

    test测试下

    1. public class MapperTest {
    2. @Test
    3. public void test1() throws IOException {
    4. InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
    5. SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
    6. SqlSession sqlSession = sqlSessionFactory.openSession();
    7. UserMapper mapper = sqlSession.getMapper(UserMapper.class);
    8. //模拟条件user
    9. User user=new User();
    10. user.setId(1);
    11. user.setUsername("zhangsan");
    12. user.setPassword("123");
    13. List userList = mapper.findByCondition(user);
    14. System.out.println(userList);
    15. }
    16. }

    运行结果

     当没有写条件时,原来的sql语句就等价于select *from user

    1. <select id="findByIds" parameterType="list" resultType="user">
    2. select *from user
    3. <where>
    4. <foreach collection="list" open="id in(" close=")" item="id" separator=",">
    5. #{id}
    6. foreach>
    7. where>
    8. select>

     这样的动态查询无论是有没有条件或者是有多个条件都能查询到

    动态sql之foreach

    循环执行sql的拼接操作,例如::select *from user where id in(1,2,3)

    测试示例foreach

    UserMapper接口下

    1. public interface UserMapper {
    2. public List findByIds(List ids);
    3. }

    配置文件UserMapper2.xml配置文件下

    1. <select id="findByIds" parameterType="list" resultType="user">
    2. select *from user
    3. <where>
    4. <foreach collection="list" open="id in(" close=")" item="id" separator=",">
    5. #{id}
    6. foreach>
    7. where>
    8. select>

    MapperTest测试类下

    1. public class MapperTest {
    2. @Test
    3. public void test2() throws IOException {
    4. InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
    5. SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
    6. SqlSession sqlSession = sqlSessionFactory.openSession();
    7. UserMapper mapper = sqlSession.getMapper(UserMapper.class);
    8. //模拟ids的数据
    9. List ids=new ArrayList();
    10. ids.add(1);
    11. ids.add(2);
    12. List byIds = mapper.findByIds(ids);
    13. System.out.println(byIds);
    14. }
    15. }

     运行结果

     sql片段的抽取

    sql中可将重复的sql提取出来,使用include引用即可,最终达到sql重用的目的

    1. <sql id="selectUser"> select *from usersql>
    2. <select id="findByIds" parameterType="list" resultType="user">
    3. <include refid="selectUser">include>
    4. <where>
    5. <foreach collection="list" open="id in(" close=")" item="id" separator=",">
    6. #{id}
    7. foreach>
    8. where>
    9. select>

    Mybatis映射文件深入知识小结