动态sql

作者: 咸鱼有梦想呀 | 来源:发表于2019-02-27 18:20 被阅读0次

一、动态sql

mybatis核心对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接、组装。

if判断

  • 需求
    用户信息综合查询列表和用户信息查询列表总数这两个statement的定义使用动态sql。
    对查询条件进行判断,如果输入参数不为空才进行查询条件拼接。

  • 实现

    <select id="findUserByinfo" parameterType="entity.UserQuerVo" resultType="entity.UserCustom">
        SELECT * FROM user
        <!--<where>可以自动去掉条件中的第一个and -->
        <where>
            <if test="userCustom.username != null">
                <if test="userCustom.username != null and userCustom.username != ''">
                    and username = #{userCustom.username}
                </if>
                <if test="userCustom.sex != null and userCustom.sex != ''">
                    and sex like "%${userCustom.sex}%"
                </if>
            </if>
        </where>
    </select>
运行结果

sql片段

  • 需求
    将上边实现的动态sql判断代码块抽取出来,组成一个sql片段。其它的statement中就可以引用sql片段。方便程序员进行开发。

  • 实现
    userMapper.xml

<!--动态sql片段
    id:sql片段唯一标识

    基于单表来定义sql片段,这样的话这个sql片段可重用性才高,在sql片段可重用性才高
    在sql片段不要包括where
    -->
    <sql id="qusery_user_where">
        <if test="userCustom.username != null">
            <if test="userCustom.username != null and userCustom.username != ''">
                and username = #{userCustom.username}
            </if>
            <if test="userCustom.sex != null and userCustom.sex != ''">
                and sex like "%${userCustom.sex}%"
            </if>
        </if>
    </sql>


    <select id="findUserByinfo" parameterType="entity.UserQuerVo" resultType="entity.UserCustom">
    SELECT * FROM user
    <where>
        <!--sql片段引用-->
      <include refid="qusery_user_where"></include>
    </where>

foreach
向sql传递数组或List,mybatis使用foreach解析

  • 需求
    在用户查询列表和查询总数的statement中增加多个id输入查询

userMapper.xml

 <!--输入list集合 foreach遍历-->
    <select id="findUserList" parameterType="entity.UserQuerVo" resultType="entity.UserCustom">
        SELECT * FROM user WHERE
        <!--collection:指定输入对象中集合属性
item:每次遍历生成对象
open:开始遍历时拼接串
close:结束遍历时拼接串
separator:遍历两个对象中需要拼接的串
-->
            <foreach collection="ids" item="id" open="(" close=")" separator="or">
                <!--每次遍历需要拼接的串-->
                id = #{id}
            </foreach>
    </select>

UserDaoMapper.java

public UserCustom findUserList(UserQuerVo userQuerVo) throws Exception;

测试

@Test
    public void findUserList() throws Exception {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserDaoMapper userDaoMapper = sqlSession.getMapper(UserDaoMapper.class);
        UserQuerVo userQuerVo = new UserQuerVo();
        List ids = new ArrayList();
        ids.add(1);
        userQuerVo.setIds(ids);
       UserCustom userList =  userDaoMapper.findUserList(userQuerVo);
        System.out.println(userList);
    }

相关文章

  • MyBatis学习:动态sql

    1.动态sql 动态sql是mybatis中的一个核心,什么是动态sql?动态sql即对sql语句进行灵活操作,通...

  • 第十三章 使用动态SQL(一)

    第十三章 使用动态SQL(一) 动态SQL简介 动态SQL是指在运行时准备并执行的SQL语句。在动态SQL中,准备...

  • 第八章 动态SQL

    动态SQL中的元素介绍 动态SQL有什么作用 MyBatis提供了对SQL语句动态组装的功能 动态SQL中的元素 ...

  • 关于Mybatis的一些问题讨论

    Mybatis动态sql是做什么的?都有哪些动态sql?简述一下动态sql的执行原理 动态sql的用途 Mybat...

  • MyBatis5-动态 SQL

    动态 SQL 什么是动态 SQL 就是动态的对 SQL 进行组装 拼接. : 可以自动去...

  • geoserver动态颜色参数样式、动态sql配置实现

    geoserver动态颜色参数样式、动态sql配置实现 动态颜色参数样式、动态sql 访问方式: http://l...

  • 强大的动态SQL

    1 动态SQL# 那么,问题来了: 什么是动态SQL? 动态SQL有什么作用? 传统的使用JDBC的方法,相信大家...

  • MyBatis:动态 SQL

    1. 动态 SQL 简而言之,动态 SQL 就是在 Mapper 中使用分支、循环等逻辑。常见的动态 SQL 元素...

  • 无标题文章

    ### 一、简答题 #### 1、Mybatis动态sql是做什么的?都有哪些动态sql?简述一下动态sql的执行...

  • 五,MyBatis动态SQL

    一,含义:动态SQL是指根据参数数据动态组织SQL的技术 二,动态SQL的应用场景:比如淘宝搜索的时候,可以动态的...

网友评论

      本文标题:动态sql

      本文链接:https://www.haomeiwen.com/subject/yuafuqtx.html