MyBatis中SQL写法总结

最近MyBatis使用较多,在这里简单总结一下MyBatis的sql写法

说简单⼀点mybatis就是写原⽣sql,官⽅都说了 mybatis 的动态sql语句是基于OGNL表达式的。可以⽅便的在 sql 语句中实现某些逻辑. 总体说来mybatis 动态SQL 语句主要有以下⼏类:

  1. if 语句 (简单的条件判断)
  2. choose (when,otherwize) ,相当于java 语⾔中的 switch ,与 jstl 中的choose 很类似.
  3. trim (对包含的内容加上 prefix,或者 suffix 等,前缀,后缀)
  4. where (主要是⽤来简化sql语句中where条件判断的,能智能的处理 and or ,不必担⼼多余导致语法误)
  5. set (主要⽤于更新时)
  6. foreach (在实现 mybatis in 语句查询时特别有⽤)

一、MyBatis – if 语句

< select id="dynamicIfTest" parameterType="Blog" resultType="Blog">
	select *  from t_blog  where 1 = 1
	<if test="title != null">
		and title =  #{title}
	</if>
	<if test="content != null">
		and content =  #{content}
	</if>
	<if test="owner != null">
		and owner =  #{owner}
	</if>
</ select>

if语句十分好理解,只有提供title,content,owener,这三个参数,才会返回满足这些条件的所有结果,这是一个非常有用的功能,如果使用JDBC就需要拼SQL语句,是非常麻烦的,相比较而言,MyBatis提供的这种动态Sql就非常的简单

二、MyBatis --choose 语句

相当于java 语⾔中的 switch ,与 jstl 中的choose 很类似.

<select id="dynamicChooseTest" parameterType="Blog" resultType="Blog">
	select *  from t_blog where 1 = 1
	<choose>
		<when test="title != null">
			and title =  #{title}
		</when>
		<when test="content != null">
			and content =  #{content}
		</when>
		<otherwise>
			and owner = "owner1"
		</otherwise>
	</choose>
</select>

when元素表⽰当when中的条件满⾜的时候就输出其中的内容,跟JAVA中的switch效果差不多的是按条件的顺序,当when中有条件满⾜的时候,就会跳出choose,即所有的whenotherwise条件中,只有个会输出,当所有的我很条件都不满⾜的时候就输出otherwise中的内容。所以上述语句的意思⾮常简单,当title!=null的时候就输出and titlte = #{title},不再往下判断条件,当title为空且content!=null的时候就出and content = #{content},当所有条件都不满⾜的时候就输出otherwise中的内容。

三、MyBatis – trim

对包含的内容加上 prefix,或者 suffix 等,前缀,后缀

<select id="dynamicTrimTest" parameterType="Blog" resultType="Blog">
	select *  from t_blog
	<trim prefix="where" prefixOverrides="and |or">
		<if test="title != null">
			title =  #{title}
		</if>
		<if test="content != null">
			and content =  #{content}
		</if>
		<if test="owner != null">
			or owner =  #{owner}
		</if>
	</trim>
</select>

后缀,与之对应的属性是prefixsuffix;可以把包含内容的⾸部某些内容覆盖,即忽略,也可以把尾的某些内容覆盖,对应的属性是prefixOverridessuffixOverrides;正因为trim有这样的功能,所以我们可以⾮常简单的利⽤trim来代替where元素的功能。

四、MyBatis – where

主要是⽤来简化sql语句中where条件判断的,能智能的处理 and or 条件

<select id="dynamicWhereTest" parameterType="Blog" resultType="Blog">
	select *  from t_blog
	<where>
		<if test="title != null">
			title =  #{title}
		</if>
		<if test="content != null">
			and content =  #{content}
		</if>
		<if test="owner != null">
			and owner =  #{owner}
		</if>
	</where>
</ select>

where元素的作⽤是会在写⼊where元素的地⽅输出⼀个where,另外⼀个好处是你不需要考虑where素⾥⾯的条件输出是什么样⼦的,MyBatis会智能的帮你处理,如果所有的条件都不满⾜那么MyBatis就查出所有的记录,如果输出后是and 开头的,MyBatis会把第⼀个and忽略,当然如果是or开头的MyBatis也会把它忽略;此外,在where元素中你不需要考虑空格的问题,MyBatis会智能的帮你加上。像上述⼦中,如果title=null, ⽽content != null,那么输出的整个语句会是select * fromt_blog where content = {content},⽽不是select * from t_blog where and content = #{content},因为MyBatis会智能的把⾸个and 或 or 给忽略。

五、MyBatis – set

主要⽤于更新时

<update id="dynamicSetTest" parameterType="Blog">
	update t_blog
	<set>
		<if test="title != null">
			title =  #{title},
		</if>
		<if test="content != null">
			content =  #{content},
		</if>
		<if test="owner != null">
			owner =  #{owner}
		</if>
	</set>
	where id =  #{id}
</update>

set元素主要是⽤在更新操作的时候,它的主要功能和where元素其实是差不多的,主要是在包含的语前输出⼀个set,然后如果包含的语句是以逗号结束的话将会把该逗号忽略,如果set包含的内容为空的话会出错。有了set元素我们就可以动态的更新那些修改了的字段。

六、MyBatis – foreach

在实现 mybatis in 语句查询时特别有⽤

<select id="queryById" resultMap="BaseReslutMap" >
	select * FROM entity
	where id  in
	<foreach collection="userids" item="userid" index="index" open="(" separator="," close=")">
		#{userid}
	</foreach>
</select>

foreach的主要⽤在构建in条件中,它可以在SQL语句中进⾏迭代⼀个集合。foreach元素的属性主要有item,index,collection,open,separator,close。item表⽰集合中每⼀个元素进⾏迭代时的别名index指定⼀个名字,⽤于表⽰在迭代过程中,每次迭代到的位置,open表⽰该语句以什么开始,separator⽰在每次进⾏迭代之间以什么符号作为分隔符,close表⽰以什么结束,在使⽤foreach的时候最关键的也最容易出错的就是collection属性,该属性是必须指定的,但是在不同情况下,该属性的值是不⼀样的,主要有⼀下3种情况:

  1. 如果传⼊的是单参数且参数类型是⼀个List的时候,collection属性值为list。
  2. 如果传⼊的是单参数且参数类型是⼀个array数组的时候,collection的属性值为array。
  3. 如果传⼊的参数是多个的时候,我们就需要把它们封装成⼀个Map了,当然单参数也可以封装map实际上如果你在传⼊参数的时候,在MyBatis⾥⾯也是会把它封装成⼀个Map的,map的key就是参数名所以这个时候collection属性值就是传⼊的List或array对象在⾃⼰封装的map⾥⾯的key。

七、MyBatis – concat

模糊查询

<select id="queryById" resultMap="BascResultMap" parameterType="entity">
	SELECT *  from entity
	<where>
		<if test="name!=null">
			name like  concat('%',concat(#{name},'%'))
		</if>
	</where>
</select>

八、MyBatis – sql片段

sql⽚段标签 :通过该标签可定义能复⽤的sql语句⽚段,在执⾏sql语句标签中直接引⽤即可。这既可以提⾼编码效率,还能有效简化代码,提⾼可读性。

<!--定义sql⽚段-->
<sql id="orderAndItem">
	o.order_id,o.cid,o.address,o.create_date,o.orderitem_id,i.orderitem_id,i.product_id,i.count
</sql>
<select id="findOrderAndItemsByOid" parameterType="java.lang.String" resultMap="BaseResultMap">
	select
	<!--引⽤sql⽚段-->
	<include refid="orderAndItem" />
	from ordertable o
	join orderitem i on o.orderitem_id = i.orderitem_id
	where o.order_id = #{orderId}
</select>

需要配置的属性:id="" >>>表⽰需要改sql语句⽚段的唯⼀标识

引⽤:通过 标签引⽤,refid="" 中的值指向需要引⽤的 中的id=“”属性

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值