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定义标签
Select
用于数据查询操作
<select id="selectUserInfo" parameterType="int" resultType="map"> select * from user_info where id=#{keyId} </select>
insert
用于数据保存操作
<insert id="insertUserInfo" parameterType="map" useGeneratedKeys="true" keyProperty="keyId"> insert into user_info ( userName, userSex )values( #{userName}, #{userSex} ) </insert>
PS:keyProperty属性可返回此条插入数据的主键值
update
用于数据更新操作
<update id="updateUserInfo" parameterType="map"> update user_info set userName=#{userName} where id=#{keyId} </update>
delete
用于数据删除操作
<delete id="selectUserInfo" parameterType="int"> delete from user_info where id=#{keyId} </delete>
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
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动态标签
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>
foreach
用于更新或保存数据时的批量操作,要用于构建in条件
<!-- 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>
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>
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 ……
或不适用<where>标签改为where 1=1
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>
trim
trim是更灵活的去处多余关键字的标签,他可以实践where和set的效果。
主要属性:
prefix:在条件语句前需要加⼊的内容,也就是给sql拼接的前缀。
suffix:在条件语句后需要加⼊的内容,sql拼接后缀
prefixOverrides:覆盖/去掉第⼀个前缀,⽐如 and 条件
suffixOverrides:覆盖/去掉最后⼀个后缀,比如 ,
prefixOverrides,suffixOverrides属性都会自动去除多余的and逗号
where例子的等效trim语句:
<!-- 查询学生list,like姓名,=性别 --> <select id="getStudentListWhere" parameterType="StudentEntity" resultMap="studentResultMap"> SELECT * from STUDENT_TBL ST <trim prefix="WHERE" prefixOverrides="AND|OR"> <if test="studentName!=null and studentName!='' "> ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%') </if> <if test="studentSex!= null and studentSex!= '' "> AND ST.STUDENT_SEX = #{studentSex} </if> </trim> </select>
set例子的等效trim语句:
<!-- 更新学生信息 --> <update id="updateStudent" parameterType="StudentEntity"> UPDATE STUDENT_TBL <trim prefix="SET" suffixOverrides=","> <if test="studentName!=null and studentName!='' "> STUDENT_TBL.STUDENT_NAME = #{studentName}, </if> <if test="studentSex!=null and studentSex!='' "> STUDENT_TBL.STUDENT_SEX = #{studentSex}, </if> <if test="studentBirthday!=null "> STUDENT_TBL.STUDENT_BIRTHDAY = #{studentBirthday}, </if> <if test="classEntity!=null and classEntity.classID!=null and classEntity.classID!='' "> STUDENT_TBL.CLASS_ID = #{classEntity.classID} </if> </trim> WHERE STUDENT_TBL.STUDENT_ID = #{studentID}; </update>
配置关联关系
association
association通常用来映射一对一的关系,例如,有个类user,对应的实体类如下:(getter,setter方法省略)
//有个类Article,对应的实体类如下: private String id;//主键 private String articleTitle;//文章标题 private String articleContent;//文章内容
//有个读者类user private String id;//主键 private String userName;//用户姓名 private Article article;//文章属性
mapper.xml 我在user类的mapper.xml这样配置
<resultMap id="userResultMap" type="test.mybatis.entity.User"> <id column="id" property="id" jdbcType="VARCHAR" javaType="java.lang.String"/> <result column="userName" property="userName" jdbcType="VARCHAR" javaType="java.lang.String"/> //这里把user的id传过去 <association property="article" column="id" select="test.mybatis.dao.articleMapper.selectArticleByUserId" />//test.mybatis.dao.articleMapper为命名空间 </resultMap>
collection
一对多,JavaBean内部嵌套一个复杂数据类型(集合);
实体类增加对应属性
private String id;//主键 private String userName;//用户姓名 private List<Article> articleList;
<resultMap id="userResultMap" type="test.mybatis.entity.User"> <id column="id" property="id" jdbcType="VARCHAR" javaType="java.lang.String"/> <result column="userName" property="userName" jdbcType="VARCHAR" javaType="java.lang.String"/> //这里把user的id传过去 <collection property="articleList" column="id" select="test.mybatis.dao.articleMapper.selectArticleListByUserId" /> </resultMap> 以下省略,类同,Mybatis会把结果封装成List类型。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!