本文整理自网络,侵删。
1.Mybatis动态sql
MyBatis 的强大特性之一便是它的动态 SQL。如果你有使用 JDBC 或其它类似框架的经验,你就能体会到根据不同条件拼接 SQL 语句的痛苦。例如拼接时要确保不能忘记添加必要的空格,还要注意去掉列表最后一个列名的逗号。利用动态 SQL 这一特性可以彻底摆脱这种痛苦。
虽然在以前使用动态 SQL 并非一件易事,但正是 MyBatis 提供了可以被用在任意 SQL 映射语句中的强大的动态 SQL 语言得以改进这种情形。
动态 SQL 元素和 JSTL 或基于类似 XML 的文本处理器相似。在 MyBatis 之前的版本中,有很多元素需要花时间了解。MyBatis 3 大大精简了元素种类,现在只需学习原来一半的元素便可。MyBatis 采用功能强大的基于 OGNL 的表达式来淘汰其它大部分元素。
2.常见的动态sql标签
2.1 if
在现实的工作场景中,我们通常需要按照不同的维度对数据进行查询。比如我们
通过员工管理系统要查询一个name 为”Tom”的人,在大一点的公司可能有几个name都为”Tom”的同事并且他们有可能分部在不同的部门,而在小点的公司可能只有一个人根本就不用按部门来过滤,这个时候我们可以通过传参来控制我们的过滤条件如下:
/** * @Description employee的dao层代码 * @Author xiaoqx <Javxuan@163.com> * @Version V1.0.0 * @Since 2017/11/26 */ public interface EmployeeMapper { List<Employee> selectEmployeeList(Employee employee); } <select id="selectEmployeeList" resultType="com.worldly.config.entity.Employee" databaseId="mysql"> select * from t_emp e where <if test="name!=null and name!=''"> e.emp_name=#{name,jdbcType=VARCHAR} </if> <if test="dep!=null"> and e.emp_dep=#{dep.id,jdbcType=INTEGER} </if> </select>
配合一个“_databaseId”变量的 databaseIdProvider 可用于动态代码中,这样就可以根据不同的数据库厂商构建特定的语句。比如下面的例子:
<insert id="insert"> <selectKey keyProperty="id" resultType="int" order="BEFORE"> <if test="_databaseId == 'oracle'"> select seq_users.nextval from dual </if> <if test="_databaseId == 'db2'"> select nextval for seq_users from sysibm.sysdummy1" </if> </selectKey> insert into users values (#{id}, #{name}) </insert>
2.2 where
我们可以想象一下如果我们只要按部门编号查询某个部门的同事时,生成的sql 语句会是怎么样的? 很容易得出结论,最终生成的sql 就会如下:
执行后将会报sql语法错误。我们可以用另外一个动态标签来解决这个问题:<select id="selectEmployeeList" resultType="com.worldly.config.entity.Employee" databaseId="mysql"> select * from t_emp e <where> <if test="name!=null and name!=''"> and e.emp_name=#{name,jdbcType=VARCHAR} </if> <if test="dep!=null"> and e.emp_dep=#{dep.id,jdbcType=INTEGER} </if> </where> </select>
只要将sql放入where动态标签内,至少有一个条件符合的时候,才会插入where语句并且会将条件语句前的 and 去掉。
2.3 trim
常用的属性:
prefix=”where”//给第一符合条件的语句 加上前缀where
prefixOverrides=”and” //将最后一条语句的 前缀and 覆盖
suffix=”and” //给第一符合条件的语句 加上后缀 and
suffixOverrides=”and”//将最后一条语句的后缀 and 覆盖
当我们把条件语句重新排版一下如下:
<select id="selectEmployeeList" resultType="com.worldly.config.entity.Employee" databaseId="mysql"> select * from t_emp e <where> <if test="name!=null and name!=''"> e.emp_name=#{name,jdbcType=VARCHAR} and </if> <if test="dep!=null"> and e.emp_dep=#{dep.id,jdbcType=INTEGER} and </if> </where> </select>
然后运行,结果如下:发现 动态where 标签只会去除 条件语句的第一个and ,这时候动态where就解决不了这个问题了,就有了一个新的动态标签trim
动态xml代码
<select id="selectEmployeeList" resultType="com.worldly.config.entity.Employee" databaseId="mysql"> select * from t_emp e //表示给第一个符合条件的语句前加 where,把最后一个语句的suffixOverrides="and" 指定的and 覆盖掉 <trim prefix="where" suffixOverrides="and"> <if test="name!=null and name!=''"> e.emp_name=#{name,jdbcType=VARCHAR} and </if> <if test="dep!=null"> e.emp_dep=#{dep.id,jdbcType=INTEGER} and </if> </trim> </select>
2.4 set
类似的用于动态更新语句的解决方案叫做 set。set 元素可以用于动态包含需要更新的列,而舍去其它的。比如:
<update id="updateAuthorIfNecessary"> update Author <set> <if test="username != null">username=#{username},</if> <if test="password != null">password=#{password},</if> <if test="email != null">email=#{email},</if> <if test="bio != null">bio=#{bio}</if> </set> where id=#{id} </update>
这里,set 元素会动态前置 SET 关键字,同时也会删掉无关的逗号,因为用了条件语句之后很可能就会在生成的 SQL 语句的后面留下这些逗号。(译者注:因为用的是“if”元素,若最后一个“if”没有匹配上而前面的匹配上,SQL 语句的最后就会有一个逗号遗留)
相关阅读 >>
mysql5.7.14下载安装图文教程及mysql数据库语句入门大全
sql server 2008 处理隐式数据类型转换在执行计划中的增强
c#连接sqlserver数据库、插入数据、从数据库获取时间示例
更多相关阅读请进入《sql》频道 >>
数据库系统概念 第6版
本书主要讲述了数据模型、基于对象的数据库和XML、数据存储和查询、事务管理、体系结构等方面的内容。