mybatis 动态sql

5 篇文章 0 订阅
4 篇文章 0 订阅

mybatis 动态sql

标签
if --> 使用OGNL表达式, 能直接调用方法(包括静态方法),能使用 三元运算符, 能做逻辑判断
choose(when,otherwise)
foreach
trim(where,set)

if 标签

exmaple

<!--
	test ----- OGNL 表达式,特殊字符需要使用转义字符
	含义: 满足 条件才会拼接 if 标签中的sql
	注意:OGNL 字符串和数字做判断时 会自动把字符串转为 数字,example "1" == 1
-->
 <select id="select" paramType="User" resultType="user">
        select * from account where 1=1
        <if test="id != null and id !=' ' " >
        	and id = #{id}
        </if>
         <if test="name != null and name.trim() !=' ' " >
        	and name = #{name}
        </if>
    </select>

where 标签

<!--
	where ----- 用来取代 where 关键字
	作用: 使用where 标签后,在拼接SQL时 会自动 去掉 第一个 and 或 or 关键字
-->
 <select id="select" paramType="User" resultType="user">
        select * from account
 	<where> 
        <if test="id != null and id !=' ' " >
        	and id = #{id}
        </if>
         <if test="name != null and name.trim() !=' ' " >
        	and name = #{name}
        </if>
    </where>
    </select>

set 标签

<!--
	set----- 用来取代 set关键字
	作用: 使用set标签后,在拼接SQL时 会自动 去掉 最后一个 逗号 ,
-->
 <update id="update" paramType="User">
        update account
 	<set> 
        <if test="id != null and id !=' ' "  >
        	id = #{id},
        </if>
         <if test="name != null and name.trim() !=' ' " >
        	name = #{name},
        </if>
    </set>
    </update>

Trim 标签

<!--
	trim ----- 自定义字符串截取规则
	作用:  给字符串增加或删除前缀 或者增加删除后缀
	prefix: trim标签中字符串拼接完成后, 给拼接后的字符串增加前缀
	prefixOverrides: 前缀覆盖,去掉拼接后的字符串的指定前缀字符
	suffix: 后缀,给拼接后的字符串增加后缀
	suffixOverrides: 后缀覆盖,去掉拼接后的字符串的指定后缀字符
-->
 <select id="select" paramType="User" resultType="user">
        select * from account
 	<trim prefix="where" suffixOverrides="and"> 
        <if test="id != null and id !=' ' " >
        	id = #{id} and
        </if>
         <if test="name != null and name.trim() !=' ' " >
        	name = #{name} and 
        </if>
    </trim>
    </select>

choose 分支判断

<!--
	choose ----- 分支   类似if else
	when : 类似 if  else标签 , 只要有 一个 when 标签满足条件,其他的when 标签就不会执行
	othserwise:表示不满足条件时的默认选项
-->
 <select id="select" paramType="User" resultType="user">
        select * from account
 	<choose> 
        <when test="id != null and id !=' ' " >
        	id = #{id} and
        </when >
         <otherwise test="name != null and name.trim() !=' ' " >
        	name = #{name} and 
        </otherwise>
 	</choose> 
    </select>

foreach 标签

<!--
	foreach ----- 循环遍历   类似 for
	collection: 要遍历的集合
					list 类型的参数会特殊处理封装在map 中, map 的key 就叫list
	item:当前遍历出的元素
	separator: 每个元素之间的分隔符
	open: 所有遍历结果拼接后的字符串 加一个前缀
	cloase:所有遍历结果拼接后的字符串 加一个后缀
	index:索引, 遍历list 时 index 是索引,item 就是当前索引对应的元素
							遍历map 是 index 是key item 是value
-->
 <select id="select" resultType="user">
        select * from account 
 	<foreach collection="list" open= " where id in ( " close=")" separator="," item="temp"> 
      	#(temp)
 	</foreach > 
</select>

批量插入 oracle得用其他方式

<insert id="insert" paramType="list">
	insert into account values
	<foreach collection="list" separator="," item="temp">
		(#{temp.name}, #{temp.password}, #{temp.money})
	</foreach>
</insert>

bind 标签

模糊查询: 可以使用 concat(’%’, #{value}, ‘%’)

<!--
	bind:绑定
	作用:可以将OGNL 表达式的值绑定到一个变量中,方便后来引用整个变量
-->
 <select id="select" resultType="user">
 	<bind name = "name" value=" '%' + lastname + '%' ">
        select * from account where name like #{name}
</select>

sql 和 include标签

<!--
	sql:抽取通用的 sql 片段
	include:引用外部的 sql 片段
	include: 还可以自定义一些property ,SQL 标签内部就能使用自定义属性 include-property, 取值 ${prop},不能使用#{prop}
-->
<sql id="selectAll">
	select * from ${tableName}
</sql>
<select id = "select"  resultType = "Account">
	<include refid="selectAll" ></include> where id = #{id}
</select>

mybatis 两个默认参数

  • _parameter :代表整个参数
    单个参数: _parameter 就是整个参数
    多个参数: 参数会被封装到map中, _parameter 就是代表整个map
  • _databaseId: 如果配置了dataBaseIdProvider 标签 ,_databaseId 就是当前数据库的别名

OGNL 表达式

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值