承接国内外服务器租用托管、定制开发、网站代运营、网站seo优化托管接单、网站代更新,新老站点皆可!!咨询QQ:3787320601

SQL开发知识:Mybatis4 之Mybatis动态sql的实现代码

管理员 2023-06-29 08:03:06 互联网圈 12 ℃ 0 评论 6834字 收藏

SQL开发知识:Mybatis4 之Mybatis动态sql的实现代码

1.甚么是动态SQL

传统的使用JDBC的方法,相信大家在组合复杂的的SQL语句的时候,需要去拼接,略不注意哪怕少了个空格,都会致使毛病。Mybatis的动态SQL功能正是为了解决这类问题, 其通过 if, choose, when, otherwise, trim, where, set, foreach标签,可组合成非常灵活的SQL语句,从而提高开发人员的效力。

SQL语句不固定, 会根据前台用户的操作而进行变化的SQL语句, 可以被称之为动态SQL. 在MyBatis中, 提供了一组标签, 用于方便的实现动态SQL, 不需要通过java代码拼接字符串了.
###2.动态sql中的标签

1. <if>

用于条件判断, test属性表示判断结果, 要求是一个boolean.

2.<where>

用于保护where子句, 通常配合一起使用. 以下功能:
a)当没有条件时, 不会创建WHERE关键字;
b)当有条件时, 会自动生成WHERE关键字;
c)会自动去掉第一个条件的and/or关键字.

3.<choose><when><otherwise>

功能类似于switch…case…default, 表示多分支判断, 只能成立一个条件

<mapper namespace="com.bjsxt.mapper.UserMapper">
 <select id="selByCondition" resultType="user">
 select * from tb_user
 <where>
  <if test="id != null">
  and id=#{id}
  </if>
  <if test="username != null and username != ''">
  and username=#{username}
  </if>
  <if test="age != null">
  and age &lt;&gt; #{age}
  </if>
  <choose>
  <when test="birthday != null and birthday != ''">
   and birthday = #{birthday}
  </when>
  <otherwise>
   and birthday is null
  </otherwise>
  </choose>
 </where>
 </select>
</mapper>

4.<bind>

对参数进行加工, 通经常使用于模糊查询给参数加通配符

<select id="sel2" resultType="user">
 <include refid="base_sql" />
 <where>
 <if test="realname != null and realname != ''">
  <bind name="realname" value="'%' + realname + '%'"/>
  and realname like #{realname}
 </if>
 </where>
</select>

5.<include>

配合使用, 用于提取通用sql语句片断, 用于援用SQL片断

<sql id="base_sql">
 select
 id, username, password, realname, age, birthday, reg_time regTime
 from tb_user
</sql>
<select id="sel2" resultType="user">
 <include refid="base_sql" />
 <where>
 <if test="realname != null and realname != ''">
  <bind name="realname" value="'%' + realname + '%'"/>
  and realname like #{realname}
 </if>
 </where>
</select>

6.<set>

用于保护update语句中的set子句, 特点是可以删除过剩的逗号

<update id="upd">
 update
 tb_user
 <set>
 <if test="username != null and username != ''">
  username=#{username},
 </if>
 <if test="age != null">
  age=#{age}
 </if>
 </set>
 where
 id=#{id}
</update>

7.<foreach>

遍历集合(数组, List, Set, Map), 通经常使用于in操作或批量新增. 属性简介:

a)collection: 要遍历的集合

b)item: 迭代项

c)open: 以甚么字符开头

d)close: 以甚么字符结束

e)separator: 多个迭代项之间的分隔符

<delete id=”delBatch”>
delete from tb_user
<where>
id in
<foreach collection=”ids” item=”id” open=”(” close=”)” separator=”,”>
#{id}
</foreach>
</where>
</delete>

8.<trim>

在语句的前落后行追加和去除指定的字符.

<insert id=”insBatch”>
insert into tb_user values
<foreach collection=”users” item=”user” separator=”,”>
<trim prefix=”(” prefixOverrides=”,” suffix=”)” suffixOverrides=”,”>
,default, #{user.username}, #{user.password}, #{user.realname}, #{user.age}, #{user.birthday}, now(),
</trim>
</foreach>
</insert>

知识点补充:静态sql与动态sql有甚么区分

SQL 语句从编译和运行的角度可以分为两种,静态 SQL和 动态 SQL,这两种 SQL 在使用方式、运行机制和性能表现等方面各有特点 :

静态 SQL:静态 SQL 语句一般用于嵌入式 SQL 利用中,在程序运行前,SQL 语句一定要是肯定的,例如 SQL 语句中触及的列名和表名一定要是存在的。静态 SQL 语句的编译是在利用程序运行前进行的,编译的结果会存储在数据库内部。而后程序运行时,数据库将直接履行编译好的 SQL 语句,下降运行时的开消。

动态 SQL:动态 SQL 语句是在利用程序运行时被编译和履行的,例如,使用 DB2 的交互式工具 CLP 访问数据库时,用户输入的 SQL 语句是不肯定的,因此 SQL 语句只能被动态地编译。动态 SQL 的利用较多,常见的 CLI 和 JDBC 利用程序都使用动态 SQL。

静态sql:语句类型在编程时候一定要是肯定好的。比如

select * from employee where empno=’abc’
select * from employee where empno=’12’

都一定要是肯定的,唯一可以变化的是abc的值。

动态sql:语句类型可以在运行期间指定,比如clp就是最典型的动态sql程序,你可以输入任何命令。

静态sql的存取路径是在运行前就肯定好的,而动态sql的存取路径是在运行时动态生成的。因此生成的存取计划相对更优,但斟酌到生成存取路径的开消,有可能利用程序的运行时间相对会比静态sql长些。

总结

到此这篇关于SQL开发知识:SQL开发知识:SQL开发知识:Mybatis4 之Mybatis动态sql的实现代码的文章就介绍到这了,更多相关mybatis动态sql内容请搜索之前的文章或继续浏览下面的相关文章希望大家以后多多支持!

文章来源:丸子建站

文章标题:SQL开发知识:Mybatis4 之Mybatis动态sql的实现代码

https://www.wanzijz.com/view/60644.html

X

截屏,微信识别二维码

微信号:weimawl

(点击微信号复制,添加好友)

打开微信