mybatis标签详解,一篇就够了
文章目录
- 前言
- 一、常用属性
- 二、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标签详解,一篇就够了相关推荐
- TCP/IP概述和详解--一篇就够
OSI(Open System Interface)模型属于理论的,主要看TCP/IP的实现. 先来个图 协议解释 TCP/IP模型分成:物理层(网络接口层).数据链路层.网络层.传输层.应用层 5层 ...
- MyBatis标签详解
MyBatis 真正的核心在映射文件中.比直接使用 JDBC 节省95%的代码.而且将 SQL 语句独立在 Java 代码之外,可以进行更为细致的 SQL 优化. 一. 映射文件的顶级元素 selec ...
- mybatis注解详解
mybatis注解详解 首 先当然得下载mybatis-3.0.5.jar和mybatis-spring-1.0.1.jar两个JAR包,并放在WEB-INF的lib目录下 (如果你使用maven,则 ...
- Mybatis映射详解
Mybatis映射详解 在最近的工作中,碰到一个比较复杂的返回结果,发现简单映射已经解决不了这个问题了,只好去求助百度,学习mybatis复杂映射应该怎么写,将学习笔记结合工作碰到的问题写下本文,供自 ...
- Java SE MyBatis框架(详解)
Java SE MyBatis框架 目录 Java SE MyBatis框架 通用框架 1.lib 2.src 2.1.mybatis.xml 2.2.DateBase.properties 2.3. ...
- [转帖]前端-chromeF12 谷歌开发者工具详解 Sources篇
前端-chromeF12 谷歌开发者工具详解 Sources篇 原贴地址:https://blog.csdn.net/qq_39892932/article/details/82498748 cons ...
- html中的mata标签详解
总是有很多朋友问我<meta name="author" content"xxxxx"><meta http-equiv="xx&q ...
- Java--web.xml加载过程;文件标签详解
一.web.xml加载过程 我们在启动Javaweb项目时,首先需要启动一个容器(如Tomcat,JBoss) WEB加载web.xml过程如下: 1.在启动Web项目时,容器(如Tomcat,JBo ...
- maven中强大的scope标签详解
maven中强大的scope标签详解 本文目的 接上一篇maven的版本号version的总结及理解 当我在封装工具jar包的时候,发现有些依赖,是一定要在工具代码里使用的,比如我做的工具包里 ...
最新文章
- 第3节 sqoop:3、sqoop的入门测试使用
- 爬虫学习笔记(十三)—— scrapy-redis(二):存储到MySQL、Scrapy项目部署
- 数据结构与算法之选择排序图文详解及代码 (C++实现)
- obj.toSource()
- Centos 7安装gvim
- 【英语学习】【科学】【Glencoe Science】【B】From Bacteria to Plants 目录及术语表
- 关于PHP各种循环,关于php迭代循环(无限分类)
- 【Flink】Flink 任务实时监控
- 服务器内容推送技术(转)
- 鸿蒙会取代emui,华为称自家手机运行鸿蒙系统正在推进 未来会取代安卓吗?
- Pa2 函数指针 指针函数 分析 (转)
- 做Meta分析要用哪些软件?Meta分析软件盘点,含软件安装包!
- java嵌入chrome内核
- 机器学习防止模型过拟合的方法知识汇总
- 2020计算机一级必背知识点,2020高考理综必背知识点
- AndroidOTA增量包(差分包)制作记录
- 3DES加密,苹果、Java 、安卓 平台一致的加密工具
- web前端整套面试题(二)--今日头条面试题
- Spring Security优劣之我见
- springboot引入国际化