Mybatis3查询语句映射

查询语句是相对复杂的一项了

 

基本的查询属性模板

 

<select 
		id=”selectPerson” 
		parameterType=”int” 
		parameterMap=”deprecated” 
		resultType=”hashmap” 
		resultMap=”personResultMap” 
		flushCache=”false” 
		useCache=”true” 
		timeout=”10000” 
		fetchSize=”256” 
		statementType=”PREPARED” 
		resultSetType=”FORWARD_ONLY” 
		> 

 

 解释

 

 

id  在命名空间中唯一的标识符,可以被用来引用这条语句。 

parameterType  将会传入这条语句的参数类的完全限定名或别名。 

parameterMap  这是引用外部 parameterMap 的已经被废弃的方法。使用内联参数映射和 parameterType 属性。 

resultType  从这条语句中返回的期望类型的类的完全限定名或别名。注意集合情形,那应该是集合可以包含的类型,而不能是集合本身。使用 resultType或 resultMap,但不能同时使用。 

 

resultMap  命名引用外部的resultMap。返回map是MyBatis 最具力量的特对其有一个很好的理解的话,许多复杂映射的情形就能被解决使用 resultMap 或 resultType,但不能同时使用。 

flushCache  将其设置为 true,无论语句什么时候被调用,都会导致缓存空。默认值:false。 

useCache  将其设置为 true,将会导致本条语句的结果被缓存。默认值:

timeout   这个设置驱动程序等待数据库返回请求结果,并抛出异常时最大等待值。默认不设置(驱动自行处理)。 

fetchSize  这是暗示驱动程序每次批量返回的结果行数。默认不设置(自行处理)。 

statementType  STATEMENT,PREPARED 或 CALLABLE 的一种。这会让 My使用选择使用 Statement,PreparedStatement或 CallableStatem默认值:PREPARED。 

resultSetType  FORWARD_ONLY|SCROLL_SENSITIVE|SCROLL_INSENSIT中的一种。默认是不设置(驱动自行处理)。 

 

搜索条件的判断

 

	<select id=”findActiveBlogLike” parameterType=”Blog” resultType=”Blog”>
		SELECT * FROM BLOG WHERE state = "ACTIVE‟
		<if test="title ! null ">
			AND title like #{title} 
		</if>
		<if test="author ! null and author.name ! =null">
			AND title like #{author.name} 
		</if>
	</select>

 

 防止where语句的错误

对于可能出现where空,可where and的错误使用<where>标签会自动判断是否加入where关键字

 

	<select id=”findActiveBlogLike” parameterType=”Blog” resultType=”Blog”>
		SELECT * FROM BLOG
		<where>
			<if test=”state ! null ”>
				state = #{state} 
			</if>
			<if test=”title ! null ”>
				AND title like #{title} 
			</if>
			<if test=”author ! null and author.name ! =null”>
				AND title like #{author.name} 
			</if>
		</where>
	</select>

 

防止set空或逗号错误

对于可能在update时set值的多重判断后set空值或set值后逗号数量不对的错误使用<set>标签可自动判断处理

 

	<update id="updateAuthorIfNecessary" parameterType="domain.blog.Author">
		update Author
		<set>
			<if test="username != null">username=#{username},</if>
			<if test="password != null">password=#{password},</if>
			<if test="email != null">email=#{email},</if>
			<if test="bio != null">bio=#{bio}</if>
		</set>
		where id=#{id}
	</update>

 

处理集合in使用foreach标签

 

	<select id="selectPostIn" resultType="domain.blog.Post">
		SELECT *
		FROM POST P
		WHERE ID in
		<foreach item="item" index="index" collection="list" open="("
			separator="," close=")">
			#{item} 
		</foreach>
	</select>

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值