记一次 MyBatis常用到的动态sql

一、SQL的动态拼接标签

if标签
where标签
choose when otherwise标签
set标签
trim标签
bind标签
sql和include标签 foreach标签

二、if标签:

test中写判断条件 参数直接paramN或者别名 特点: 只要成立就拼接在Sql语句中,都成立就全部都拼接 注意: where子句中加上1=1来规避and的风险

<select id="uid" resultType="UserEntity">
select * from UserEntity where 1=1
<if test="param1!=null and param1!=''">
and outno=#{param1}
</if>
<if test="param2!=null and param2!=''">
and inno=#{param2}
</if>
</select>

条件模糊查以及查询时间段内数据

<select id="uid" resultType="UserEntity">
	select * from UserEntity where 1=1
	<if test="param1!=null and param1!=''">
		and outno=#{param1}
	</if>
	<if test="param2!=null and param2!=''">
		and inno LIKE CONCAT('%',#{param2 },'%' )
	</if>
	<if test="effectiveTime !=null and effectiveTime !=''">   
		and begin_time <= #{effectiveTime}    
		//effectiveTime 是封的查询条件类的查询字段  and end_time >= #{effectiveTime}     
		//begin_time,end_time 对应数据库字段。
	</if>
</select>

三、where标签:

特点:会自动地给Sql语句添加where关键字,并将第一个and去除。

<select id="uid" resultType="UserEntity">
    select * from UserEntity
    <where>
    	<if test="param1!=null and param1!=''">
   			and outno=#{param1}
    	</if>
    	<if test="param2!=null and param2!=''">
    		and inno=#{param2}
    	</if>
    </where>
</select>

四、choose when otherwise标签

特点:条件只要有一个成立,其他的就不会再判断了。如果没有成立的条件则默认执行otherwise中的内容

<select id="uid" resultType="UserEntity">
	select * from UserEntity
	<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>

五、set标签:

产生一个set关键字,自动去除最后一个逗号。
注意:
在判断条件中最后保持有一个永远成立的条件。避免sql错误。

<update id="uid">
	update accountTable 
	<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>

六、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>

七、bind标签:

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>

八、sql和include标签:

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>

九、foreach标签:

构造IN条件语句时需要对集合进行遍历,这时候可以使用foreach元素。foreach会去掉多余","。
若集合为空,则不会执行foreach元素中的操作,但此时会多出"in"关键字,报错。
item:集合中的元素
index:元素所在集合的下标,迭代map时,index是键
collection:集合的类型,可选值list,array,除此之外还可以是@Param(“name”)、Map中的key、类的成员变量 open、
close:在首、尾拼接的字符
separator:每个元素间的分隔符
注: foreach标签支持List、Set、Map、Array等的遍历
迭代数组或者List、Set集合时,index是迭代次数,item是本次迭代获取的元素;
迭代Map(或Map.Entry对象的集合)时,index是键,item是值
collection属性介绍
传入单参数且是List时,collection=“list”
传入单参数且是Array时,collection=“array”
传入单参数且是Set时,需使用@Param注解,同下
传入单参数且使用@Param(“name”)时,collection=“name”,即和@Param注解的value属性值相同,此时list、array无效
传入多参数且封装成Map时,如map.put(“ids”, Arrays.asList(1, 2)),此时collection=“ids”
传入多参数且封装成类时,如User类中有成员变量List roleIds,此时collection=“roleIds”;
若User类中有成员变量Role role,Role类中有成员变量prilIds,此时collection=“role.prilIds”

<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>

以后继续增加……


版权声明:本文为llwy1428原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。