mybatis常用标签语法使用

在平时使用中,个人对查询用的比较多,且查询是数据库访问的主要压力,所以在查询过程中应该减少不必要的数据,多采用主键、索引列进行条件查询。

select标签

<!-- 
	id:对应DAO层的方法名 
	resultType:返回结果的数据类型类的全限定名 比如 Long、String、map、list、pojo
	resultMap:映射结果集,用于处理比较复杂的查询结果,不可以与resultType同时使用
	parameterType:接收参数的类型 比如Long、String、map、pojo...
-->
  	<select id=""  resultType=""  parameterType="">
		SELECT * FROM table_name where 1 = 1
  	</select>
  	

除了上述的三种,还有一些使用频率不高的属性,例如:

flushCache="" #为:true,调用时清空本地缓存与二级缓。存默认值:false。
useCache="" #为:true,调用时缓存二级缓存,对 select 标签默认为:true。

update

<update id=""  paraneterType="Integer" >
    update 
    	table_name
    SET
    	field=#{value} #需要修改的字段值
    WHERE
    	ID=#{ID} #一般为主键
</update>

delete

<delete id="" paraneterType="Integer">
	DELETE FROM table_name WHERE field= #{value} 
</delete>

insert

<insert id="" paraneterType="Integer">
	INSERT INTO table_name (field1,field2,field3,...) VALUES (value1,value2,value3,...)
</insert>

其他标签

rresultMap


<!-- 
	id:唯一标识
	type:返回值类型 pojo
	extend:可以选择继承其他pojo
	autoMapping:是否自动映射,默认为true,旧版本为false
 -->
 <select id=""  resultMap="resultMap_id" parameterType="">
	SELECT * FROM table_name where 1 = 1
 </select>
  	
<resultMap id="resultMap_id" type=""  extend="" autoMapping="">
	<!-- 
	字段映射:
		column为你sql查询的结果中的一个字段(可以是别名)
		property 为pojo对应的一个属性
		jdbcType 为当前字段在数据的的数据类型
	 -->
	<id colum="" property="">	<!-- 绑定主键-->
	<result column="" property="" jdbcType="">
<!--
	当返回值中有封装的pojo时,我们需要使用 collection 标签
	ofType:为pojo对象
	当开启自动映射之后,无需再写collection中的内容
-->
	<collection property="" ofType="" autoMapping="">
		<id colum="" property="">	<!-- ofType的pojo主键 -->
		<result column="" property="" jdbcType="">
	</collection>
	 
</resultMap>

if

<!--
	判断该是否不为null, 如果是,则执行下面的语句
-->
<if test=" value != null">
	AND field = #{value}
</if>

foreach


<!-- 
	collection:需要循环的参数,例如list
	item:当前遍历到的值
	open:从哪里开始遍历
	close:在哪里结束遍历
	separator:对遍历结果集进行分割  一般为  ,
 -->
AND field IN
<foreaach collection="" item="" open="" close="" separator="">
	#{value}
</foreach>

include–>sql

<!--
	可以看做是用来拼接sql,在sql标签中可以用来写多个sql语句的共同部分
-->
<select id=""  resultType=""  parameterType="">
	<include refid="include_id"/> where 1 = 1
</select>

<sql id="include_id">
	SELECT * FROM table_name
</sql>
  	

where -->sql

<!--
	如果有多个条件,选择不为null的作为查询条件,可以使用where_if组合。
	在更新语句中,也可以通过set_if使用不为null的字段进行更新
-->
SELECT * FROM table_name
						<where>
							<if test=" value1 != null">
								AND field = #{value1}
							</if>
							<if test=" value2 != null">
								AND field = #{value2}
							</if>
							<if test=" value3 != null">
								AND field = #{value3}
							</if>
						</where>

where–>choose–>when/otherwise

<!--
	类似于switch case 语句
-->
SELECT * FROM table_name
<where>
	<choose>
		<when test="value != null">
			field=#{value}
		</when>
		<otherwise>
			field=#{val}
		</otherwise>
	</choose>
</where>

目前常用的标签就是这些,欢迎大家补充讨论。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大大|泡泡糖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值