Mybatis特殊SQL的执行
文章目录
- 模糊查询
- 批量删除
- 动态设置表名
- 添加功能获取自增的主键
- 自定义映射resultMap
- resultMap处理字段和属性的映射关系
- 多对一映射处理
- 级联方式处理映射关系
- 使用association处理映射关系
- 分步查询
- 1. 查询员工信息
- 2. 查询部门信息
- 一对多映射处理
- collection
模糊查询
/*** 根据用户名进行模糊查询* @param username * @return User*/
List<User> getUserByLike(@Param("username") String username);
<!--List<User> getUserByLike(@Param("username") String username);-->
<select id="getUserByLike" resultType="User"><!--select * from t_user where username like '%${mohu}%'--> <!--select * from t_user where username like concat('%',#{mohu},'%')--> select * from t_user where username like "%"#{mohu}"%"
</select>
其中select * from t_user where username like "%"#{mohu}"%"
是最常用的
批量删除
只能使用${},如果使用#{},则解析后的sql语句为delete from t_user where id in ('1,2,3')
,这样是将1,2,3
看做是一个整体,只有id为1,2,3
的数据会被删除。正确的语句应该是delete from t_user where id in (1,2,3)
,或者delete from t_user where id in ('1','2','3')
/*** 根据id批量删除* @param ids * @return int*/
int deleteMore(@Param("ids") String ids);
<delete id="deleteMore">delete from t_user where id in (${ids})
</delete>
//测试类
@Test
public void deleteMore() {......//获取userMapper.....int result = userMapper.deleteMore("1,2,3,8");System.out.println(result);
}
动态设置表名
- 只能使用${},因为表名不能加单引号
/*** 查询指定表中的数据* @param tableName * @return User*/
List<User> getUserByTable(@Param("tableName") String tableName);
<!--List<User> getUserByTable(@Param("tableName") String tableName);-->
<select id="getUserByTable" resultType="User">select * from ${tableName}
</select>
添加功能获取自增的主键
-
使用场景
-
t_clazz(clazz_id,clazz_name)
- t_student(student_id,student_name,clazz_id)
- 添加班级信息
- 获取新添加的班级的id
- 为班级分配学生,即将某学生的班级id修改为新添加的班级的id
-
在mapper.xml中设置两个属性
-
useGeneratedKeys:设置使用自增的主键
- keyProperty:因为增删改有统一的返回值是受影响的行数,因此只能将获取的自增的主键放在传输的参数user对象的某个属性中
/*** 添加用户信息* @param user */
void insertUser(User user);
<!--void insertUser(User user);-->
<insert id="insertUser" useGeneratedKeys="true" keyProperty="id">insert into t_user values (null,#{username},#{password},#{age},#{sex},#{email})
</insert>
//测试类
@Test
public void insertUser() {'''获取userMapper'''User user = new User(null, "ton", "123", 23, "男", "123@321.com");userMapper.insertUser(user);System.out.println(user);//输出:user{id=10, username='ton', password='123', age=23, sex='男', email='123@321.com'},自增主键存放到了user的id属性中
}
自定义映射resultMap
resultMap处理字段和属性的映射关系
- resultMap:设置自定义映射
- 属性:
- id:表示自定义映射的唯一标识,不能重复
- type:查询的数据要映射的实体类的类型
- 子标签:
- id:设置主键的映射关系
- result:设置普通字段的映射关系
- 子标签属性:
- property:设置映射关系中实体类中的属性名
- column:设置映射关系中表中的字段名
- 若字段名和实体类中的属性名不一致,则可以通过resultMap设置自定义映射,即使字段名和属性名一致的属性也要映射,也就是全部属性都要列出来
<resultMap id="empResultMap" type="Emp"><id property="eid" column="eid"></id><result property="empName" column="emp_name"></result><result property="age" column="age"></result><result property="sex" column="sex"></result><result property="email" column="email"></result>
</resultMap>
<!--List<Emp> getAllEmp();-->
<select id="getAllEmp" resultMap="empResultMap">select * from t_emp
</select>
若字段名和实体类中的属性名不一致,但是字段名符合数据库的规则(使用_),实体类中的属性名符合Java的规则(使用驼峰)。此时也可通过以下两种方式处理字段名和实体类中的属性的映射关系
1. 可以通过为字段起别名的方式,保证和实体类中的属性名保持一致 ```xml<!--List<Emp> getAllEmp();--><select id="getAllEmp" resultType="Emp">select eid,emp_name empName,age,sex,email from t_emp</select>```
2. 可以在MyBatis的核心配置文件中的`setting`标签中,设置一个全局配置信息mapUnderscoreToCamelCase,可以在查询表中数据时,自动将_类型的字段名转换为驼峰,例如:字段名user_name,设置了mapUnderscoreToCamelCase,此时字段名就会转换为userName。[核心配置文件详解](#核心配置文件详解)```xml
<settings><setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>```
多对一映射处理
查询员工信息以及员工所对应的部门信息
public class Emp { private Integer eid; private String empName; private Integer age; private String sex; private String email; private Dept dept;//...构造器、get、set方法等
}
级联方式处理映射关系
<resultMap id="empAndDeptResultMapOne" type="Emp"><id property="eid" column="eid"></id><result property="empName" column="emp_name"></result><result property="age" column="age"></result><result property="sex" column="sex"></result><result property="email" column="email"></result><result property="dept.did" column="did"></result><result property="dept.deptName" column="dept_name"></result>
</resultMap>
<!--Emp getEmpAndDept(@Param("eid")Integer eid);-->
<select id="getEmpAndDept" resultMap="empAndDeptResultMapOne">select * from t_emp left join t_dept on t_emp.eid = t_dept.did where t_emp.eid = #{eid}
</select>
使用association处理映射关系
- association:处理多对一的映射关系
- property:需要处理多对的映射关系的属性名
- javaType:该属性的类型
<resultMap id="empAndDeptResultMapTwo" type="Emp"><id property="eid" column="eid"></id><result property="empName" column="emp_name"></result><result property="age" column="age"></result><result property="sex" column="sex"></result><result property="email" column="email"></result><association property="dept" javaType="Dept"><id property="did" column="did"></id><result property="deptName" column="dept_name"></result></association>
</resultMap>
<!--Emp getEmpAndDept(@Param("eid")Integer eid);-->
<select id="getEmpAndDept" resultMap="empAndDeptResultMapTwo">select * from t_emp left join t_dept on t_emp.eid = t_dept.did where t_emp.eid = #{eid}
</select>
分步查询
1. 查询员工信息
- select:设置分布查询的sql的唯一标识(namespace.SQLId或mapper接口的全类名.方法名)
- column:设置分步查询的条件
//EmpMapper里的方法
/*** 通过分步查询,员工及所对应的部门信息* 分步查询第一步:查询员工信息* @param * @return Emp*/
Emp getEmpAndDeptByStepOne(@Param("eid") Integer eid);
<resultMap id="empAndDeptByStepResultMap" type="Emp"><id property="eid" column="eid"></id><result property="empName" column="emp_name"></result><result property="age" column="age"></result><result property="sex" column="sex"></result><result property="email" column="email"></result><association property="dept"select="com.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwo"column="did"></association>
</resultMap>
<!--Emp getEmpAndDeptByStepOne(@Param("eid") Integer eid);-->
<select id="getEmpAndDeptByStepOne" resultMap="empAndDeptByStepResultMap">select * from t_emp where eid = #{eid}
</select>
association 里面有接口里面的方法
2. 查询部门信息
//DeptMapper里的方法
/*** 通过分步查询,员工及所对应的部门信息* 分步查询第二步:通过did查询员工对应的部门信息* @param* @return Emp*/
Dept getEmpAndDeptByStepTwo(@Param("did") Integer did);
<!--此处的resultMap仅是处理字段和属性的映射关系-->
<resultMap id="EmpAndDeptByStepTwoResultMap" type="Dept"><id property="did" column="did"></id><result property="deptName" column="dept_name"></result>
</resultMap>
<!--Dept getEmpAndDeptByStepTwo(@Param("did") Integer did);-->
<select id="getEmpAndDeptByStepTwo" resultMap="EmpAndDeptByStepTwoResultMap">select * from t_dept where did = #{did}
</select>
一对多映射处理
public class Dept {private Integer did;private String deptName;private List<Emp> emps;//...构造器、get、set方法等
}
collection
- collection:用来处理一对多的映射关系
- ofType:表示该属性对饮的集合中存储的数据的类型
<resultMap id="DeptAndEmpResultMap" type="Dept"><id property="did" column="did"></id><result property="deptName" column="dept_name"></result><collection property="emps" ofType="Emp"><id property="eid" column="eid"></id><result property="empName" column="emp_name"></result><result property="age" column="age"></result><result property="sex" column="sex"></result><result property="email" column="email"></result></collection>
</resultMap>
<!--Dept getDeptAndEmp(@Param("did") Integer did);-->
<select id="getDeptAndEmp" resultMap="DeptAndEmpResultMap">select * from t_dept left join t_emp on t_dept.did = t_emp.did where t_dept.did = #{did}
</select>
进行分步查询。。。。
- 查询部门信息
/*** 通过分步查询,查询部门及对应的所有员工信息* 分步查询第一步:查询部门信息* @param did */
Dept getDeptAndEmpByStepOne(@Param("did") Integer did);
<resultMap id="DeptAndEmpByStepOneResultMap" type="Dept"><id property="did" column="did"></id><result property="deptName" column="dept_name"></result><collection property="emps"select="com.mybatis.mapper.EmpMapper.getDeptAndEmpByStepTwo"column="did"></collection>
</resultMap>
<!--Dept getDeptAndEmpByStepOne(@Param("did") Integer did);-->
<select id="getDeptAndEmpByStepOne" resultMap="DeptAndEmpByStepOneResultMap">select * from t_dept where did = #{did}
</select>
- 根据部门id查询部门中的所有员工
/*** 通过分步查询,查询部门及对应的所有员工信息* 分步查询第二步:根据部门id查询部门中的所有员工* @param did*/
List<Emp> getDeptAndEmpByStepTwo(@Param("did") Integer did);
<!--List<Emp> getDeptAndEmpByStepTwo(@Param("did") Integer did);-->
<select id="getDeptAndEmpByStepTwo" resultType="Emp">select * from t_emp where did = #{did}
</select>
相关文章:
Mybatis特殊SQL的执行
文章目录 模糊查询批量删除动态设置表名添加功能获取自增的主键自定义映射resultMapresultMap处理字段和属性的映射关系 多对一映射处理级联方式处理映射关系使用association处理映射关系 分步查询1. 查询员工信息 2. 查询部门信息 一对多映射处理collection 模糊查询 /*** 根…...

MyBatis-Flex(一):快速开始
框架介绍 MyBatis-Flex 是一个优雅的 MyBatis 增强框架,它非常轻量、同时拥有极高的性能与灵活性。 MyBatis-Flex 官方文档 说明 本文参照官方文档的【快速开始】 章节,编写 Spring Boot 项目的代码示例。 快速开始 创建数据库表 直接参照官网示…...

Vue组件化
组件 组件是实现应用中局部功能的代码(HTML,CSS,JS)和资源(图片,声音,视频)的集合,凡是采用组件方式开发的应用都可以称为组件化应用 模块是指将一个大的js文件按照模块化拆分规则进行拆分成的每个js文件, 凡是采用模块方式开发的应用都可以称为模块化应用(组件包括模块) 传…...

nodejs+python+php+微信小程序-基于安卓android的健身服务应用APP-计算机毕业设计
考虑到实际生活中在健身服务应用方面的需要以及对该系统认真的分析,将系统权限按管理员和用户这两类涉及用户划分。 则对于进一步提高健身服务应用发展,丰富健身服务应用经验能起到不少的促进作用。 健身服务应用APP能够通过互联网得到广泛的、全面的宣…...

SpringCloud 微服务全栈体系(九)
第九章 Docker 三、Dockerfile 自定义镜像 常见的镜像在 DockerHub 就能找到,但是我们自己写的项目就必须自己构建镜像了。 而要自定义镜像,就必须先了解镜像的结构才行。 1. 镜像结构 镜像是将应用程序及其需要的系统函数库、环境、配置、依赖打包而…...

Mybatis 多对一和一对多查询
文章目录 Mybatis 多对一 and 一对多查询详解数据库需求Mybatis代码注意 Mybatis 多对一 and 一对多查询详解 数据库 员工表 t_emp 部门表 t_dept CREATE TABLE t_emp (emp_id int NOT NULL AUTO_INCREMENT,emp_name varchar(25) CHARACTER SET utf8 COLLATE utf8_general_ci…...

MySQL的数据库操作、数据类型、表操作
目录 一、数据库操作 (1)、显示数据库 (2)、创建数据库 (3)、删除数据库 (4)、使用数据库 二、常用数据类型 (1)、数值类型 (2࿰…...

音视频技术开发周刊 | 317
每周一期,纵览音视频技术领域的干货。 新闻投稿:contributelivevideostack.com。 MIT惊人再证大语言模型是世界模型!LLM能分清真理和谎言,还能被人类洗脑 MIT等学者的「世界模型」第二弹来了!这次,他们证明…...

【JavaSE专栏58】“Java构造函数:作用、类型、调用顺序和最佳实践“ ⚙️⏱️
解析Java构造函数:作用、类型、调用顺序和最佳实践" 🚀📚🔍🤔📝🔄⚙️⏱️📖🌐 摘要引言1. 什么是构造函数 🤔2. 构造函数的类型与用途 📝1.…...

Ubuntu系统HUSTOJ 用 vim 修改php.ini 重启PHP服务
cd / sudo find -name php.ini 输出: ./etc/php/7.4/cli/php.ini ./etc/php/7.4/fpm/php.ini sudo vim /etc/php/7.4/cli/php.ini sudo vim /etc/php/7.4/fpm/php.ini 知识准备: vim的搜索与替换 在正常模式下键入 / ,即可进入搜索模式…...

案例分析真题-信息安全
案例分析真题-信息安全 2009年真题 【问题1】 【问题2】 【问题3】 2010年真题 【问题1】 【问题2】 【问题3】 2011 年真题 【问题1】 【问题2】 【问题3】 骚戴理解:这个破题目完全考的知识储备,不知道的连手都动不了,没法分析 2013年真题…...

envi5.3处理高分二号影像数据辐射定标大气校正
目录 一、多光谱影像处理 1. 辐射定标 2.大气校正 1. 需要准备一些数据: 2.大气校正过程 3、正射校正 二、全色影像处理 1. 辐射定标 2. 正射校正 三、图像融合 1.几何配准 2.图像融合 高分二号处理流程 envi5.3的安装教程: ENVI5.3安装 安装完ENVI5.3后࿰…...

C语言 结构体
结构体的自引用: 自引用的目的: 结构体的自引用就是指在结构体内部,包含指向自身类型结构体的指针。 像链表就会用到结构体的自引用。假如我们要创建链表 链表的没个节点都是一个结构体,它里面存放着它的数据和下个节点的地址。 假如我们用…...

frp-内网穿透部署-ubuntu22服务器-windows server-详细教程
文章目录 1.下载frp2.配置服务器2.1.配置frps.ini文件2.2.设置服务文件2.3.设置开机自启和服务操作2.4.后台验证2.5.服务器重启 3.配置本地window3.1.frpc配置3.2.添加开机计划启动3.3.控制台启动隐藏窗口 4.centos防火墙和端口3.1.开放端口3.2.查看端口 5.关闭进程5.1.杀死进程…...
MySQL内存使用的监控开关和使用查看
参考文档: https://brands.cnblogs.com/tencentcloud/p/11151 https://www.cnblogs.com/grasp/p/10306697.html MySQL如何使用内存 在MySQL中,内存占用主要包括以下几部分,全局共享的内存、线程独占的内存、内存分配器占用的内存࿰…...

数据库管理-第113期 Oracle Exadata 04-硬件选择(20231020)
数据库管理-第113期 Oracle Exadata 04-硬件选择(2023010290) 本周没写文章,主要是因为到上海参加了Oracle CAB/PAB会议,这个放在后面再讲,本期讲一讲Exadata,尤其是存储节点的硬件选择及其对应的一些通用…...

带着问题去分析:Spring Bean 生命周期 | 京东物流技术团队
1: Bean在Spring容器中是如何存储和定义的 Bean在Spring中的定义是_org.springframework.beans.factory.config.BeanDefinition_接口,BeanDefinition里面存储的就是我们编写的Java类在Spring中的元数据,包括了以下主要的元数据信息: 1&…...
C语言修行之函数篇(一)tolower —— 转换为小写字母
文章目录 函数说明函数声明函数返回值函数实现函数实例 函数说明 对于大写字母,如果在当前语言环境中存在小写表示形式,则tolower()返回其小写等效物。否则,tolower()函数执行相同的任务。 函数声明 #include <ctype.h> int tolower(…...

【JavaSE专栏55】Java集合类HashTable解析
🌲Java集合类HashTable解析 🌲Java集合类HashTable解析摘要引言Hashtable是什么?Hashtable vs. HashMap:何时使用Hashtable?多线程环境:历史遗留系统:不需要进行特殊操作: Hashtable…...

Apollo上机实践:一次对自动驾驶技术的亲身体验
上机实践 概述自动驾驶通信分布式系统开发模式开发工具 自动驾驶感知传感器特性感知流程及算法部署感知模型 自动驾驶决策规划决策规划流程和算法使用 Dreamview 进行控制在环的规划调试开发规划场景和算法 福利活动 主页传送门:📀 传送 概述 Apollo 是…...
RestClient
什么是RestClient RestClient 是 Elasticsearch 官方提供的 Java 低级 REST 客户端,它允许HTTP与Elasticsearch 集群通信,而无需处理 JSON 序列化/反序列化等底层细节。它是 Elasticsearch Java API 客户端的基础。 RestClient 主要特点 轻量级ÿ…...
浅谈 React Hooks
React Hooks 是 React 16.8 引入的一组 API,用于在函数组件中使用 state 和其他 React 特性(例如生命周期方法、context 等)。Hooks 通过简洁的函数接口,解决了状态与 UI 的高度解耦,通过函数式编程范式实现更灵活 Rea…...

深度学习在微纳光子学中的应用
深度学习在微纳光子学中的主要应用方向 深度学习与微纳光子学的结合主要集中在以下几个方向: 逆向设计 通过神经网络快速预测微纳结构的光学响应,替代传统耗时的数值模拟方法。例如设计超表面、光子晶体等结构。 特征提取与优化 从复杂的光学数据中自…...
在软件开发中正确使用MySQL日期时间类型的深度解析
在日常软件开发场景中,时间信息的存储是底层且核心的需求。从金融交易的精确记账时间、用户操作的行为日志,到供应链系统的物流节点时间戳,时间数据的准确性直接决定业务逻辑的可靠性。MySQL作为主流关系型数据库,其日期时间类型的…...
Ubuntu系统下交叉编译openssl
一、参考资料 OpenSSL&&libcurl库的交叉编译 - hesetone - 博客园 二、准备工作 1. 编译环境 宿主机:Ubuntu 20.04.6 LTSHost:ARM32位交叉编译器:arm-linux-gnueabihf-gcc-11.1.0 2. 设置交叉编译工具链 在交叉编译之前&#x…...

K8S认证|CKS题库+答案| 11. AppArmor
目录 11. AppArmor 免费获取并激活 CKA_v1.31_模拟系统 题目 开始操作: 1)、切换集群 2)、切换节点 3)、切换到 apparmor 的目录 4)、执行 apparmor 策略模块 5)、修改 pod 文件 6)、…...

【CSS position 属性】static、relative、fixed、absolute 、sticky详细介绍,多层嵌套定位示例
文章目录 ★ position 的五种类型及基本用法 ★ 一、position 属性概述 二、position 的五种类型详解(初学者版) 1. static(默认值) 2. relative(相对定位) 3. absolute(绝对定位) 4. fixed(固定定位) 5. sticky(粘性定位) 三、定位元素的层级关系(z-i…...
Python爬虫(二):爬虫完整流程
爬虫完整流程详解(7大核心步骤实战技巧) 一、爬虫完整工作流程 以下是爬虫开发的完整流程,我将结合具体技术点和实战经验展开说明: 1. 目标分析与前期准备 网站技术分析: 使用浏览器开发者工具(F12&…...

JUC笔记(上)-复习 涉及死锁 volatile synchronized CAS 原子操作
一、上下文切换 即使单核CPU也可以进行多线程执行代码,CPU会给每个线程分配CPU时间片来实现这个机制。时间片非常短,所以CPU会不断地切换线程执行,从而让我们感觉多个线程是同时执行的。时间片一般是十几毫秒(ms)。通过时间片分配算法执行。…...

让回归模型不再被异常值“带跑偏“,MSE和Cauchy损失函数在噪声数据环境下的实战对比
在机器学习的回归分析中,损失函数的选择对模型性能具有决定性影响。均方误差(MSE)作为经典的损失函数,在处理干净数据时表现优异,但在面对包含异常值的噪声数据时,其对大误差的二次惩罚机制往往导致模型参数…...