mybatis标签详解,一篇就够了

1年前 (2022) 程序员胖胖胖虎阿
149 0 0

文章目录

  • 前言
  • 一、常用属性
  • 二、SQL定义标签
    • 1. select
    • 2. insert
    • 3. update
    • 4. delete
    • 5. resultMap
    • 6. sql
  • 三、SQL动态标签
    • 1. if
    • 2. foreach
    • 3. choose/when/otherwise
    • 4. where
    • 5. set

前言

MyBatis 是一款优秀的持久层框架,它支持自定义 SQL、存储过程以及高级映射。MyBatis 的真正强大在于它的语句映射,这是它的魔力所在,使映射器的 XML 文件显得相对简单。如果拿它跟具有相同功能的 JDBC 代码进行对比,你会立即发现省掉了将近 95% 的代码。MyBatis 致力于减少使用成本,让用户能更专注于 SQL 代码。

继springBoot整合myBatis完整前后端项目实例后,本文详细介绍下mybatis常用标签的使用。


一、常用属性

属性 描述
id 在命名空间中唯一的标识符,被用来引用这条语句
parameterType 传入这条语句的参数的类全限定名或别名
resultType 期望从这条语句中返回结果的类全限定名或别名,resultType 和 resultMap 之间只能同时使用一个
resultMap 在命名空间中唯一的标识符,被用来引用这条语句 ,resultType 和 resultMap 之间只能同时使用一个
flushCache 将其设置为 true 后,只要语句被调用,都会导致本地缓存和二级缓存被清空,默认值:对于(select语句)false ;对于( insert、update 和 delete 语句)true
useCache 将其设置为 true 后,将会导致本条语句的结果被二级缓存缓存起来,默认值:对于(select语句)为 true
useGeneratedKeys (仅适用于 insert 和 update)这会令 MyBatis 使用 JDBC 的 getGeneratedKeys 方法来取出由数据库内部生成的主键(比如:像 MySQL 和 SQL Server 这样的关系型数据库管理系统的自动递增字段),默认值:false
keyProperty (仅适用于 insert 和 update)指定能够唯一识别对象的属性,MyBatis 会使用 getGeneratedKeys 的返回值或 insert 语句的 selectKey 子元素设置它的值,默认值:未设置(unset)

二、SQL定义标签

1. select

用于数据查询操作,例:

<select id="selectUserInfo" parameterType="int" resultType="map">
  select * from user_info where id=#{keyId}
</select>

2. insert

用于数据保存操作,例:

<insert id="insertUserInfo" parameterType="map" useGeneratedKeys="true" keyProperty="keyId">
  insert into user_info (
	userName,
	userSex
  )values(
  	#{userName},
  	#{userSex}
  )
</insert>

PS:keyProperty属性可返回此条插入数据的主键值

3. update

用于数据更新操作,例:

<update id="updateUserInfo" parameterType="map">
  update  user_info
  set userName=#{userName}
  where id=#{keyId}
</update>

4. delete

用于数据删除操作,例:

<delete id="selectUserInfo" parameterType="int">
  delete  from user_info 
  where id=#{keyId}
</delete>

5. resultMap

SQL返回与实体类映射关系信息,例:

<resultMap id="userInfoMap" type="User">
  <result property="user_name" column="userName"/>
  <result property="user_sex" column="userSex"/>
</resultMap>
<select id="selectUserInfo" parameterType="int" resultType="userInfoMap">
  select
  userName,
  userSex
  from user_info 
  where id=#{keyId}
</select>
将数据表字段userName、userSex映射到实体类User的user_name、user_sex

6. sql

用于定义可重用的 SQL 代码片段,以便在多个SQL语句中使用。 参数可以静态地(在加载的时候)确定下来,并且可以在不同的 include 元素中定义不同的参数值。例:

<!-- 定义 -->
<sql id="userColumns"> ${alias}.userName,${alias}.userSex</sql>
<!-- 运用 -->
<select id="selectUserInfo" resultType="map">
  select
    <include refid="userColumns"><property name="alias" value="t1"/></include>,
    <include refid="userColumns"><property name="alias" value="t2"/></include>
  from user_info  t1
  left join user_info_copy t2
</select>

三、SQL动态标签

1. if

单个条件判断,用以实现条件筛选,例:

<select id="selectUserInfo" parameterType="map" resultType="map">
  select * from user_info 
  where 1=1
  <if test="userSex !=null and userSex !='' ">
  	and userSex=#{userSex}
  </if>
  <if test="userName !=null and userName !='' ">
  	and userName like CONCAT('%',#{userName},'%')
  </if>
</select>

PS:此处需要注意,如果参数值为数字int型,判断是否等于某个固定值时可能会导致判断失效,例如:

<if test="userFlag !=null and userFlag !='' and userFlag =='1'">
  	……
</if>

可以修改为:

<!-- .toString()转换 -->
<if test="userFlag !=null and userFlag !='' and userFlag =='1'.toString()">
  	……
</if>

<!-- 外层单引号,内部双引号 -->
<if test='userFlag !=null and userFlag !="" and userFlag =="1"'>
  	……
</if>

2. foreach

用于更新或保存数据时的批量操作,例:

<!-- userList为List<HashMap<String,Object>>类型数据 -->
insert into user_info(
userName,
userSex
)values
<foreach item="item" index="index" collection="userList" separator="," >
(
#{item.userName},
#{item.userSex}
)
</foreach>
<!-- userList为List<String>类型数据 -->
insert into user_info(
userName
)values
<foreach item="item" index="index" collection="userList" separator="," >
(
#{userName}
)
</foreach>
update user_info
set userAge=#{userAge}
where id in
<foreach collection="keyIds" index="index" item="item" separator="," open="(" close=")">
#{item}
</foreach>

3. choose/when/otherwise

用以实现条件的多种判断,类似与if else,例:

<select id="selectUserInfo" parameterType="map" resultType="map">
  select * from user_info 
  where 1=1
  <choose>
  	<when test="userFlag!=null and userFlag!='' and userFlag=='Y'">
  		and id<=100
  	</when>
  	<when test="userFlag!=null and userFlag!='' and userFlag=='N'">
  		and id <=200
  	</when>
  	<otherwise>
  		and id<=300
  	</otherwise>
  </choose>
</select>

4. where

只会在子元素返回任何内容的情况下才插入 “WHERE” 子句,并且可以自动处理判断条件语句返回的第一个and或or,例:

不使用where标签时,若userSex为空,语法错误会报错:

<select id="selectUserInfo" parameterType="map" resultType="map">
  select * from user_info 
  where
  <if test="userSex !=null and userSex !='' ">
  	userSex=#{userSex}
  </if>
  <if test="userName !=null and userName !='' ">
  	and userName like CONCAT('%',#{userName},'%')
  </if>
</select>

修改为:

<select id="selectUserInfo" parameterType="map" resultType="map">
  select * from user_info
  <where>
  <if test="userSex !=null and userSex !='' ">
  	userSex=#{userSex}
  </if>
  <if test="userName !=null and userName !='' ">
  	and userName like CONCAT('%',#{userName},'%')
  </if>
  </where>
</select>
自动转换为:select * from user_info where userName like ……

5. set

可以动态更新需要更新的列,忽略其它不更新的列,例:

<update id="updateUserInfo" parameterType="map">
  update  user_info
  <set>
  <if test="userName!= null and userName!=''">
  userName=#{userName},
  </if>
  userSex=#{userSex}
  </set>
  where id=#{keyId}
</update>

版权声明:程序员胖胖胖虎阿 发表于 2022年11月21日 下午11:32。
转载请注明:mybatis标签详解,一篇就够了 | 胖虎的工具箱-编程导航

相关文章

暂无评论

暂无评论...