mybatis的XML映射器和动态SQL
bigegpt 2025-04-26 16:57 10 浏览
1.XML 映射器
sql映射文件的顶级元素:
select,insert,update,delete,sql,resultMap,cache,cache-ref。
1.select元素:
<select
id="getUser" //命名空间的对应的方法名称
parameterType="int" //传入的参数类的名称
resultType="User" //返回结果的类名称
resultMap="userMap"//对外部的resultMap的命名引用
flushCache="false" //默认值为false。设置true时,会每次查询都清除本地缓存和二级缓存
useCache="true" //默认值为true。设置true时,会启用本地缓存和二级缓存
timeout="2000" //超时时间
fetchSize="200" //返回的接口不能超过设置值,默认不设置。
>
2.insert元素:
<insert
id="insertUser" //命名空间的对应的方法名称
paramterType="User" //传入的参数类的名称
flushCache="true" //默认值为false。设置true时,会每次查询都清除本地缓存和二级缓存
keyProperty="" //(仅适用于 insert 和 update)实体类对应的主键的名称
keyColumn="" // (仅适用于 insert 和 update)设置生成键值在表中的列名,当主键列不是表中的第一列的时候,是必须设置的
useGeneratedKeys="" // (仅适用于 insert 和 update) 使用 JDBC 的 getGeneratedKeys 方法来取出由数据库内部生成的主键
timeout="2000" //超时时间
>
3.update元素:
<update
id="updateUser" //命名空间的对应的方法名称
paramterType="User" //传入的参数类的名称
flushCache="true" //默认值为false。设置true时,会每次查询都清除本地缓存和二级缓存
keyProperty="" //(仅适用于 insert 和 update)实体类对应的主键的名称
keyColumn="" // (仅适用于 insert 和 update)设置生成键值在表中的列名,当主键列不是表中的第一列的时候,是必须设置的
useGeneratedKeys="" // (仅适用于 insert 和 update) 使用 JDBC 的 getGeneratedKeys 方法来取出由数据库内部生成的主键
timeout="2000" //超时时间
>
4.delete元素:
<delete
id="deleteUser" //命名空间的对应的方法名称
paramterType="User" //传入的参数类的名称
flushCache="true" //默认值为false。设置true时,会每次查询都清除本地缓存和二级缓存
timeout="2000" //超时时间
>
5.sql元素:
<sql id="selectSql"
>
id,username,userpassword
</sql>
6.include元素:
<select>
select
<include refid="selectSql"></include>
from
t_sys_user
</select>
7.resultMap元素:
//resultMap以及其下级的元素
resultMap
constructor
idArg
arg
id // property,colum,jdbcType,javaType,typeHandler
result // property,colum,jdbcType,javaType,typeHandler
association
collection
discriminator
case
8.association 关联元素:
8.1.使用association的select方法:
<resultMap id="responseResultMap" type="com.lz.org.system.response.ResponseUser" >
<association
property="role"
column="user_id"
javaType="com.lz.org.system.entity.Role"
select="getRolesByUserId"
>
</association>
</resultMap>
<select id="getUserByUserId" resultMap="responseResultMap">
select *
from t_sys_user
where user_id = #{id}
</select>
<select id="getRolesByUserId" resultType="com.lz.org.system.entity.Role">
select * from t_sys_role r join t_sys_user_role ur
on r.role_id=ur.role_id and ur.user_id=#{id}
</select>
8.2.通过join连接返回嵌套结果:
<resultMap id="responseResultMap" type="com.lz.org.system.response.ResponseUser" >
<!-- 一定要写属性相关的result,否则无法显示数据或者数据为null -->
<id property="userId" column="user_id" jdbcType="BIGINT"/>
<result property="userName" column="user_name" jdbcType="VARCHAR"/>
<result property="userPassword" column="user_password" jdbcType="VARCHAR"/>
<result property="salt" column="salt" jdbcType="VARCHAR"/>
<result property="userPhone" column="user_phone" jdbcType="VARCHAR"/>
<result property="userEmai" column="user_emai" jdbcType="VARCHAR"/>
<result property="userTitle" column="user_title" jdbcType="VARCHAR"/>
<result property="createrId" column="creater_id" jdbcType="BIGINT"/>
<result property="createrName" column="creater_name" jdbcType="VARCHAR"/>
<result property="createrTime" column="creater_time" jdbcType="TIMESTAMP"/>
<result property="updaterId" column="updater_id" jdbcType="BIGINT"/>
<result property="updaterName" column="updater_name" jdbcType="VARCHAR"/>
<result property="updaterTime" column="updater_time" jdbcType="TIMESTAMP"/>
<result property="roleIds" column="role_ids" jdbcType="VARCHAR"/>
<result property="roleNames" column="role_names" jdbcType="VARCHAR"/>
<association
property="role"
column="user_id"
javaType="com.lz.org.system.entity.Role"
>
<!-- 一定要写属性相关的result,否则无法显示数据或者数据为null -->
<id property="roleId" column="role_id" jdbcType="BIGINT"/>
<result property="roleName" column="role_name" jdbcType="VARCHAR"/>
<result property="roleCode" column="role_code" jdbcType="VARCHAR"/>
<result property="createrId" column="creater_id" jdbcType="BIGINT"/>
<result property="createrName" column="creater_name" jdbcType="VARCHAR"/>
<result property="createrTime" column="creater_time" jdbcType="TIMESTAMP"/>
<result property="updaterId" column="updater_id" jdbcType="BIGINT"/>
<result property="updaterName" column="updater_name" jdbcType="VARCHAR"/>
<result property="updaterTime" column="updater_time" jdbcType="TIMESTAMP"/>
<result property="permissionIds" column="permission_ids" jdbcType="VARCHAR"/>
<result property="permissionNames" column="permission_names" jdbcType="VARCHAR"/>
</association>
</resultMap>
<select id="getUserByUserId" resultMap="responseResultMap">
select *
FROM t_sys_user AS u
INNER JOIN t_sys_user_role AS ur ON u.user_id = ur.user_id
INNER JOIN t_sys_role AS r ON u.user_id = ur.user_id AND r.role_id = ur.role_id and u.user_id=#{id};
</select>
9.collection 集合元素:
9.1.使用collection的select方法:
<resultMap id="responseResultMap" type="com.lz.org.system.response.ResponseUser" >
<collection property="roles"
column="user_id"
ofType="com.lz.org.system.entity.Role"
select="getRoleListByUserId">
</collection>
</resultMap>
<select id="getUserByUserId" resultMap="responseResultMap">
select * from t_sys_user u where u.user_id=#{id}
</select>
<select id="getRoleListByUserId" resultType="com.lz.org.system.entity.Role">
select * from t_sys_role r left join t_sys_user_role ur
on r.role_id=ur.role_id and ur.user_id=#{userId}
</select>
10.字符串替换:
参数处理:
#{age,javaType=int,jdbcType=NUMERIC}
<insert id="insertUser" parameterType="User">
insert into users (id, username, password)
values (#{id}, #{username}, #{password})
</insert>
<insert id="insertUser" parameterType="User">
insert into users (id, username, password)
values (${id}, ${username}, ${password})
</insert>
备注:
${column} 会被直接替换,而 #{value} 会使用 ? 预处理。
${column}这种方式接受用户的输入,并用作语句参数是不安全的,会导致潜在的 SQL 注入攻击。
2.动态 SQL
if
foreach
trim(where,set)
choose(when,otherwise)
2.1.if
<if test="userId!=null">
#{userId,jdbcType = BIGINT},
</if>
2.2.foreach
说明:
你可以将任何可迭代对象(如 List、Set 等)、Map 对象或者数组对象作为集合参数传递给 foreach。当使用可迭代对象或者数组时,index 是当前迭代的序号,item 的值是本次迭代获取到的元素。当使用 Map 对象(或者 Map.Entry 对象的集合)时,index 是键,item 是值
<delete id="deleteByIds">
delete from t_sys_user
where user_id in
<foreach item="item" index="index" collection="list" open="("
separator=" , " close=")">#{item}
</foreach>
</delete>
2.3.set
<set>
<if test="userName!=null and userName!=''">
user_name=#{userName,jdbcType = VARCHAR},
</if>
<if test="userPassword!=null and userPassword!=''">
user_password=#{userPassword,jdbcType = VARCHAR},
</if>
<set>
2.4.choose,when,othenwise
<select id="getUser"
resultType="User">
SELECT * FROM BLOG WHERE deteled = ‘0’
<choose>
<when test="userName != null">
AND user_name like #{userName}
</when>
<when test="phone != null">
AND phone like #{phone}
</when>
<otherwise>
AND status = 1
</otherwise>
</choose>
</select>
2.5.where
<select id="getUser"
resultType="User">
SELECT * FROM user
<where>
<if test="userName != null">
user_name = #{userName}
</if>
<if test="phone != null">
AND phone like #{phone}
</if>
<if test="email != null">
AND email like #{email}
</if>
</where>
</select>
2.5.trim
<trim prefix="WHERE" prefixOverrides="AND |OR ">
</trim>
<trim prefix="SET" suffixOverrides=",">
</trim>
相关推荐
- 得物可观测平台架构升级:基于GreptimeDB的全新监控体系实践
-
一、摘要在前端可观测分析场景中,需要实时观测并处理多地、多环境的运行情况,以保障Web应用和移动端的可用性与性能。传统方案往往依赖代理Agent→消息队列→流计算引擎→OLAP存储...
- warm-flow新春版:网关直连和流程图重构
-
本期主要解决了网关直连和流程图重构,可以自此之后可支持各种复杂的网关混合、多网关直连使用。-新增Ruoyi-Vue-Plus优秀开源集成案例更新日志[feat]导入、导出和保存等新增json格式支持...
- 扣子空间体验报告
-
在数字化时代,智能工具的应用正不断拓展到我们工作和生活的各个角落。从任务规划到项目执行,再到任务管理,作者深入探讨了这款工具在不同场景下的表现和潜力。通过具体的应用实例,文章展示了扣子空间如何帮助用户...
- spider-flow:开源的可视化方式定义爬虫方案
-
spider-flow简介spider-flow是一个爬虫平台,以可视化推拽方式定义爬取流程,无需代码即可实现一个爬虫服务。spider-flow特性支持css选择器、正则提取支持JSON/XML格式...
- solon-flow 你好世界!
-
solon-flow是一个基础级的流处理引擎(可用于业务规则、决策处理、计算编排、流程审批等......)。提供有“开放式”驱动定制支持,像jdbc有mysql或pgsql等驱动,可...
- 新一代开源爬虫平台:SpiderFlow
-
SpiderFlow:新一代爬虫平台,以图形化方式定义爬虫流程,不写代码即可完成爬虫。-精选真开源,释放新价值。概览Spider-Flow是一个开源的、面向所有用户的Web端爬虫构建平台,它使用Ja...
- 通过 SQL 训练机器学习模型的引擎
-
关注薪资待遇的同学应该知道,机器学习相关的岗位工资普遍偏高啊。同时随着各种通用机器学习框架的出现,机器学习的门槛也在逐渐降低,训练一个简单的机器学习模型变得不那么难。但是不得不承认对于一些数据相关的工...
- 鼠须管输入法rime for Mac
-
鼠须管输入法forMac是一款十分新颖的跨平台输入法软件,全名是中州韵输入法引擎,鼠须管输入法mac版不仅仅是一个输入法,而是一个输入法算法框架。Rime的基础架构十分精良,一套算法支持了拼音、...
- Go语言 1.20 版本正式发布:新版详细介绍
-
Go1.20简介最新的Go版本1.20在Go1.19发布六个月后发布。它的大部分更改都在工具链、运行时和库的实现中。一如既往,该版本保持了Go1的兼容性承诺。我们期望几乎所...
- iOS 10平台SpriteKit新特性之Tile Maps(上)
-
简介苹果公司在WWDC2016大会上向人们展示了一大批新的好东西。其中之一就是SpriteKitTileEditor。这款工具易于上手,而且看起来速度特别快。在本教程中,你将了解关于TileE...
- 程序员简历例句—范例Java、Python、C++模板
-
个人简介通用简介:有良好的代码风格,通过添加注释提高代码可读性,注重代码质量,研读过XXX,XXX等多个开源项目源码从而学习增强代码的健壮性与扩展性。具备良好的代码编程习惯及文档编写能力,参与多个高...
- Telerik UI for iOS Q3 2015正式发布
-
近日,TelerikUIforiOS正式发布了Q32015。新版本新增对XCode7、Swift2.0和iOS9的支持,同时还新增了对数轴、不连续的日期时间轴等;改进TKDataPoin...
- ios使用ijkplayer+nginx进行视频直播
-
上两节,我们讲到使用nginx和ngixn的rtmp模块搭建直播的服务器,接着我们讲解了在Android使用ijkplayer来作为我们的视频直播播放器,整个过程中,需要注意的就是ijlplayer编...
- IOS技术分享|iOS快速生成开发文档(一)
-
前言对于开发人员而言,文档的作用不言而喻。文档不仅可以提高软件开发效率,还能便于以后的软件开发、使用和维护。本文主要讲述Objective-C快速生成开发文档工具appledoc。简介apple...
- macOS下配置VS Code C++开发环境
-
本文介绍在苹果macOS操作系统下,配置VisualStudioCode的C/C++开发环境的过程,本环境使用Clang/LLVM编译器和调试器。一、前置条件本文默认前置条件是,您的开发设备已...
- 一周热门
- 最近发表
- 标签列表
-
- mybatiscollection (79)
- mqtt服务器 (88)
- keyerror (78)
- c#map (65)
- resize函数 (64)
- xftp6 (83)
- bt搜索 (75)
- c#var (76)
- mybatis大于等于 (64)
- xcode-select (66)
- httperror403.14-forbidden (63)
- logstashinput (65)
- hadoop端口 (65)
- dockernetworkconnect (63)
- esxi7 (63)
- vue阻止冒泡 (67)
- c#for循环 (63)
- oracle时间戳转换日期 (64)
- jquery跨域 (68)
- php写入文件 (73)
- java大写转小写 (63)
- kafkatools (66)
- mysql导出数据库 (66)
- jquery鼠标移入移出 (71)
- 取小数点后两位的函数 (73)