文章目录

  • 前言
  • 一、常用属性
  • 二、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_infoset 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">selectuserName,userSexfrom 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  t1left 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>

mybatis标签详解,一篇就够了相关推荐

  1. TCP/IP概述和详解--一篇就够

    OSI(Open System Interface)模型属于理论的,主要看TCP/IP的实现. 先来个图 协议解释 TCP/IP模型分成:物理层(网络接口层).数据链路层.网络层.传输层.应用层 5层 ...

  2. MyBatis标签详解

    MyBatis 真正的核心在映射文件中.比直接使用 JDBC 节省95%的代码.而且将 SQL 语句独立在 Java 代码之外,可以进行更为细致的 SQL 优化. 一. 映射文件的顶级元素 selec ...

  3. mybatis注解详解

    mybatis注解详解 首 先当然得下载mybatis-3.0.5.jar和mybatis-spring-1.0.1.jar两个JAR包,并放在WEB-INF的lib目录下 (如果你使用maven,则 ...

  4. Mybatis映射详解

    Mybatis映射详解 在最近的工作中,碰到一个比较复杂的返回结果,发现简单映射已经解决不了这个问题了,只好去求助百度,学习mybatis复杂映射应该怎么写,将学习笔记结合工作碰到的问题写下本文,供自 ...

  5. Java SE MyBatis框架(详解)

    Java SE MyBatis框架 目录 Java SE MyBatis框架 通用框架 1.lib 2.src 2.1.mybatis.xml 2.2.DateBase.properties 2.3. ...

  6. [转帖]前端-chromeF12 谷歌开发者工具详解 Sources篇

    前端-chromeF12 谷歌开发者工具详解 Sources篇 原贴地址:https://blog.csdn.net/qq_39892932/article/details/82498748 cons ...

  7. html中的mata标签详解

    总是有很多朋友问我<meta name="author" content"xxxxx"><meta http-equiv="xx&q ...

  8. Java--web.xml加载过程;文件标签详解

    一.web.xml加载过程 我们在启动Javaweb项目时,首先需要启动一个容器(如Tomcat,JBoss) WEB加载web.xml过程如下: 1.在启动Web项目时,容器(如Tomcat,JBo ...

  9. maven中强大的scope标签详解

    maven中强大的scope标签详解 本文目的   接上一篇maven的版本号version的总结及理解   当我在封装工具jar包的时候,发现有些依赖,是一定要在工具代码里使用的,比如我做的工具包里 ...

最新文章

  1. smack连接openfire
  2. Eclipse设置字符编码
  3. python time,datetime当前时间,昨天时间,时间戳和字符串的转化
  4. linux开机和登陆欢迎信息
  5. 适配Win11!Edge重磅更新来袭
  6. Python学习笔记(一)——基本知识点
  7. Angular.js学习笔记(1)
  8. VC学习笔记:状态栏
  9. 实现atoi函数(C++实现)
  10. 全球第一开源ERP Odoo操作手册 数据库自动备份
  11. ArcGis-学习笔记6-4 空间插值简介
  12. python实现多接口翻译软件
  13. 键盘没有Home键和End键的完美解决办法
  14. 跟着团子学SAP PS后台篇——项目编码配置 OPSJ/OPSK
  15. 汇智网node学习笔记
  16. 计算机中汉字的顺序用什么牌,中国汉字的写做顺序,你知道吗?
  17. linux peek,Peek - Gif 录制软件
  18. 抖音小店入驻条件及费用,2021个人开通抖音小店条件
  19. DVWA平台漏洞测试与源码分析(一)SQL注入
  20. 刺客列传鸿蒙记,高淳高级中学2020-2021学年高一上学期阶段测试语文试题.docx

热门文章

  1. vc build tools完美解决
  2. Linux之cat tail less常见用法
  3. Nginx-限制并发、访问速率、流量
  4. WordPress主题 大前端 阿里百秀 XIU 小清新CMS高级主题[更新v6.0]
  5. stackTrace
  6. 在html语言中段落标签是,HTML的段落标签是什么?段落标签具体都有哪些属性?...
  7. 浏览器占满整个屏幕_如何实现div布满整个浏览器,全屏
  8. 一分钟了解:“文献:基于Sigma点卡尔曼滤波的天基红外低轨卫星目标跟踪”
  9. 「物流跟踪管理系统」 · Java Swing + MySQL JDBC开发,美和易思结业考试机试试题
  10. 大一 JAVAOOP-T2 面向 对象 进阶- 实践任务指南(美和易思)