MyBatis—SQL语句的动态拼接
SQL的动态拼接
if标签 where标签 choose when otherwise标签 set标签
trim标签 bind标签 sql和include标签 foreach标签
LogMapper.xml
1.if标签:
test中写判断条件 参数直接paramN或者别名
特点:
只要成立就拼接在Sql语句中,都成立就全部都拼接
注意:
where子句中加上1=1来规避and的风险
test中写判断条件 参数直接paramN或者别名
特点:
只要成立就拼接在Sql语句中,都成立就全部都拼接
注意:
where子句中加上1=1来规避and的风险
<select id="selg" resultType="log"> select * from log where 1=1 <if test="param1!=null and param1!=''"> and outno=#{param1} </if> <if test="param2!=null and param2!=''"> and inno=#{param2} </if> </select> |
2.where标签:
特点:
会自动的给Sql语句添加where关键字,并将第一个and去除。
特点:
会自动的给Sql语句添加where关键字,并将第一个and去除。
<select id="selw" resultType="log"> select * from log <where> <if test="param1!=null and param1!=''"> and outno=#{param1} </if> <if test="param2!=null and param2!=''"> and inno=#{param2} </if> </where> </select> |
3.choose when otherwise标签
特点:
条件只要有一个成立,其他的就不会再判断了。
如果没有成立的条件则默认执行otherwise中的内容
特点:
条件只要有一个成立,其他的就不会再判断了。
如果没有成立的条件则默认执行otherwise中的内容
<select id="selc" resultType="log"> select * from log <where> <choose> <when test="param1!=null and param1!=''"> and outno=#{param1} </when> <when test="param2!=null and param2!=''"> and inno=#{param2} </when> <otherwise> and 1=1 </otherwise> </choose> </where> </select> |
4.set标签:
产生一个set关键字,自动去除最后一个逗号。
注意:
在判断条件中最后保持有一个永远成立的条件。避免sql错误。
产生一个set关键字,自动去除最后一个逗号。
注意:
在判断条件中最后保持有一个永远成立的条件。避免sql错误。
<update id="upA"> update account <set> <if test="aname!=null and aname!=''"> aname=#{aname}, </if> <if test="money !=null and money !=''"> money=#{money}, </if> <if test="ano !=null and ano !=''"> ano=#{ano}, </if> </set> where ano=#{ano} </update> |
5.trim标签:
prefix:在trim的内容前添加指定的内容
prefixOverrides在trim的内容前去除指定的内容
suffix:在trim的内容后添加指定的内容
suffixOverrides:在trim的内容后去除指定的内容
注意:
先去除后添加
添加内容会默认添加一个空格。
prefix:在trim的内容前添加指定的内容
prefixOverrides在trim的内容前去除指定的内容
suffix:在trim的内容后添加指定的内容
suffixOverrides:在trim的内容后去除指定的内容
注意:
先去除后添加
添加内容会默认添加一个空格。
<update id="upT" parameterType="account"> update account <trim prefix="$" prefixOverrides="" suffix="" suffixOverrides=""> <if test="ano !=null and ano !=''"> ano=#{ano}, </if> <if test="aname!=null and aname!=''"> aname=#{aname}, </if> <if test="money !=null and money !=''"> money=#{money}, </if> </trim> where ano=#{ano} </update> |
6.bind标签:
name:参数名
value:表达式,注意字符串拼接按照变量方式进行拼接
例如:
<bind name="money" value="'$'+money"/>
给参数重新赋值
name:参数名
value:表达式,注意字符串拼接按照变量方式进行拼接
例如:
<bind name="money" value="'$'+money"/>
给参数重新赋值
<update id="upB" parameterType="account"> <bind name="money" value="money+100"/> update account <trim prefix="set" suffixOverrides=","> <if test="ano !=null and ano !=''"> ano=#{ano}, </if> <if test="aname!=null and aname!=''"> aname=#{aname}, </if> <if test="money !=null and money !=''"> money=#{money}, </if> </trim> where ano=#{ano} </update> |
7.sql和include标签:
sql标签:在外部声明公用SQL语句
id
include标签:引入声明的公共SQL语句
refid:
优点:便于SQL的整体修改
缺点:难于阅读
sql标签:在外部声明公用SQL语句
id
include标签:引入声明的公共SQL语句
refid:
优点:便于SQL的整体修改
缺点:难于阅读
<select id="selA" resultType="account"> select <include refid="mysql"></include> from account </select> <sql id="mysql"> ano,aname,apwd,money </sql> |
8.foreach标签:
collection:要遍历的集合对象
item:记录每次遍历的结果
open:在结果的左边添加内容
separator:结果和结果之间的内容
close:在最后添加的内容
collection:要遍历的集合对象
item:记录每次遍历的结果
open:在结果的左边添加内容
separator:结果和结果之间的内容
close:在最后添加的内容
<select id="selF" parameterType="list" resultType="account"> select * from account where ano in <foreach collection="list" item="item" open="(" separator="," close=")"> #{item} </foreach> </select> <insert id="inF"> insert into log values <foreach collection="list" item="log" separator=","> (#{log.outno},#{log.inno},#{log.money}) </foreach> </insert> |
以上是mapper.xml里边的代码
LogMapper.java (和LogMapper.xml一样名字的接口interface)
Test.java
木子璇总结时刻:欢迎小伙伴们提出建议哦,如有错误,望大神指出哦,谢谢啦。