spring和Mybatis的各种查询

ops/2024/9/19 23:27:55/ 标签: spring, mybatis, java

文章目录

    • 六**、MyBatis的各种查询功能**
      • 6.1、查询一个实体类的对象
      • 6.2、查询一个list集合
      • 6.3、查询单个数据
      • 6.4、查询一条数据为Map集合
      • 6.5、查询多条数据为Map集合
    • 七、**特殊SQL的执行**
      • 7.1、模糊查询
      • 7.2、批量删除
      • 7.3、动态设置表名
      • 7.4、添加功能获取自增的主键
    • 八、**自定义映射resultMap 一对一、多对一、一对多、多对多**
      • 8.1、resultMap处理字段和属性的映射关系
      • 8.2、一对一映射处理
      • 8.3、多对一映射处理
      • 8.4、一对多映射处理 部门 -->员工
      • 8.5、多对多映射关系

六**、MyBatis的各种查询功能**

6.1、查询一个实体类的对象

6.2、查询一个list集合

  • 当查询的数据为多条记录时,不能使用实体类作为返回值,否则会抛出异常:TooManyResultsException,但是如果查询的数据只有一条,可以使用实体类或集合作为返回值

6.3、查询单个数据

6.4、查询一条数据为Map集合

6.5、查询多条数据为Map集合

  1. 方式一

    • 将表中的数据以map集合的方式查询,一条数据对应一个map;如果有多条数据,就会产生多个map集合,此时可以将这些map放在一个list集合中获取
  2. 方式二

    • 如果有多条数据,可以将每条数据转换的map集合放在一个大的map集合中,但是必须要通过@MapKey注解来完成。将查询的某个字段的值作为大的Map集合的键(key)

七、特殊SQL的执行

7.1、模糊查询

    <select id="方法名" resultType="返回值类型">SELECT *from userwhere username like "%"#{name}"%"</select>

7.2、批量删除

java">    <delete id="deleteAll">deletefrom userwhere id in #{ids}</delete>

7.3、动态设置表名

    <select id="getUserList" resultType="object">SELECT *from ${tableName}</select>

7.4、添加功能获取自增的主键

    <insert id="insertUser" useGeneratedKeys="true" keyProperty="id">INSERT INTO user (username, password)VALUES (#{username}, #{password})</insert>

八、自定义映射resultMap 一对一、多对一、一对多、多对多

8.1、resultMap处理字段和属性的映射关系

如果字段名和实体类中的属性名不一致的情况下,可以通过resultMap设置自定义映射。

  1. 可以通过为字段起别名的方式,别名起成和属性名一致。保证字段名和实体类中的属性名一致
java"><select id="getEmpByEmpId" resultType="emp">select emp_id empId,emp_name empName,age,sex from emp where emp_id = #{empId}
</select>
  1. 如果字段名和实体类中的属性名不一致的情况下,但是字段名符合数据库的规则(使用_),实体类中使用的属性名符合java的规则(使用驼峰命名),可以在MyBatis的核心配置文件中设置一个全局配置信息mapUnderscoreToCamelCase,可以在查询表中的数据时,自动将带下划线“_”的字段名转为驼峰命名
    • user_name:userName
    • emp_id:empId
<settings><!--将数据库字段名的下划线映射为驼峰--><setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<!--Emp getEmpByEmpId(@Param("empId") Integer empId);-->
<select id="getEmpByEmpId" resultType="emp">select * from emp where emp_id = #{empId}
</select>
  1. 使用resutlMap自定义映射处理
<!--resultMap:设置自定义的映射关系id:唯一标识type:处理映射关系的实体类的类型 一般使用MyBatis的别名常用的标签id:处理主键和实体类中属性的映射关系result:处理普通字段和实体类中属性的映射关系column:设置映射关系中的字段名,必须是sql查询出的某个字段property:设置映射关系中的属性的属性名,必须是处理的实体类型中的属性名
-->
<resultMap id="empReslutMap" type="emp"><id property="empId" column="emp_id"></id><result property="empName" column="emp_name"></result><result property="age" column="age"></result><result property="sex" column="sex"></result>
</resultMap>

8.2、一对一映射处理

人与身份证号

  1. 级联方式处理
java">/*** 根据id查询人员信息* @param id* @return*/
Person findPersonById(@Param("id") Integer id);
<!-- Person findPersonById(Integer id);-->
<select id="findPersonById" resultMap="IdCardWithPersonResult">SELECT person.*,idcard.codeFROM person,idcardWHERE person.card_id=idcard.id AND person.id=#{id}
</select><resultMap id="IdCardWithPersonResult" type="person"><id property="id" column="id"></id><result property="name" column="name"></result><result property="age" column="age"></result><result property="sex" column="sex"></result><result property="card.id" column="id"></result><result property="card.code" column="code"></result>
</resultMap>
  1. association
<resultMap id="IdCardWithPersonResult2" type="person"><id property="id" column="id"></id><result property="name" column="name"></result><result property="age" column="age"></result><result property="sex" column="sex"></result><association property="card" javaType="IdCard"><id property="id" column="id"></id><result property="code" column="code"></result></association>
</resultMap>
  1. 分步查询
<!--分步查询第一步-->
<!-- Person findPersonById3(@Param("id") Integer id);-->
<select id="findPersonById3" resultMap="IdCardWithPersonResult3">select * from person where id=#{id}
</select><resultMap id="IdCardWithPersonResult3" type="person"><id property="id" column="id"></id><result property="name" column="name"></result><result property="age" column="age"></result><result property="sex" column="sex"></result><!--association:处理一对一或者多对一的映射关系(处理的实体类类型的属性)property:设置需要处理映射关系的属性的属性名javaType:设置要处理的属性的类型column:第一步传递给第二步的字段,映射关系的表关联的字段--><association property="card" javaType="IdCard" column="card_id"select="com.qcby.mybatis.mapper.IdCardMapper.findCodeById"></association>
</resultMap>
<!--分步查询的第二步-->
<!--IdCard findCodeById(@Param("id") Integer id);-->
<select id="findCodeById" resultType="idcard">SELECT * from idcard where id=#{id}
</select>

8.3、多对一映射处理

场景模拟:

查询员工信息以及员工所对应的部门信息

使用resultMap自定义映射处理
处理多对一的映射关系:

  • 级联方式处理

  • association

  • 分步查询

  1. 级联方式处理
java">/*** 获取员工以及所对应的部门信息* @param empId* @return*/
Emp getEmpAndDeptByEmpId(@Param("empId") Integer empId);
<!-- Emp getEmpAndDeptByEmpId(@Param("empId") Integer empId);-->
<select id="getEmpAndDeptByEmpId" resultMap="empAndDeptResultMap">SELECT emp.*,dept.*FROM empLEFT JOIN deptON emp.dept_id=dept.dept_idWHERE emp.emp_id=#{empId}
</select>
<resultMap id="empAndDeptResultMap" type="emp"><id column="emp_id" property="empId"></id><result column="emp_name" property="empName"></result><result column="age" property="age"></result><result column="sex" property="sex"></result><result column="dept_id" property="dept.deptId"></result><result column="dept_name" property="dept.deptName"></result></resultMap>
java">@Test
public void testGetEmpAndDeptByEmpId(){Emp emp = empMapper.getEmpAndDeptByEmpId(1);System.out.println(emp);
}
  1. association
<resultMap id="empAndDeptResultMap" type="emp"><id column="emp_id" property="empId"></id><result column="emp_name" property="empName"></result><result column="age" property="age"></result><result column="sex" property="sex"></result><association property="dept" javaType="dept"><id column="dept_id" property="deptId"/><result column="dept_name" property="deptName"></result></association>
</resultMap>
  1. 分步查询

  2. 根据员工id查询员工信息

emp接口中

java">/*** 通过分步查询来查询员工以及所对应部门信息的第一步* @param empId* @return*/
Emp getEmpAndDeptByStepOne(@Param("empId") Integer empId);

dept接口中

java">/*** 通过分步查询来查询员工以及所对应部门信息的第二步* @param deptId* @return*/
Dept getEmpAndDeptByStepTwo(@Param("deptId") Integer deptId);

emp映射文件中

<!--Emp getEmpAndDeptByStepOne(@Param("empId") Integer empId);-->
<select id="getEmpAndDeptByStepOne" resultMap="empAndDeptByStepResultMap">select * from emp where emp_id = #{empId}
</select>
<resultMap id="empAndDeptByStepResultMap" type="Emp"><id column="emp_id" property="empId"></id><result column="emp_name" property="empName"></result><result column="age" property="age"></result><result column="sex" property="sex"></result><association property="dept" column="dept_id"select="com.qcby.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwo"></association>
</resultMap>

dept映射文件中

<!--Dept getEmpAndDeptByStepTwo(@Param("deptId") Integer deptId);-->
<select id="getEmpAndDeptByStepTwo" resultType="Dept">select * from dept where dept_id = #{deptId}
</select>

测试

java">@Test
public void testGetEmpAndDeptByStepOne(){Emp emp = empMapper.getEmpAndDeptByStepOne(1);System.out.println(emp);
}

分步查询的优点:可以实现延迟加载(懒加载),但是必须在核心配置文件中设置全局配置信息:

lazyLoadingEnabled:延迟加载的全局开关。当开启时,所有管理对象都会延迟加载

aggressiveLazyLoading:当开启时,任何方法的调用都会加载该对象的所有属性。否则,每个属性会按需加载,此时就可以实现按需加载,获取的数据是什么,就会执行相应的sql语句

此时可以通过association和collection中的fetchType属性设置当前的分步查询是否使用延迟加载,fetchType=“lazy(延迟加载)|eager(立即加载)”

8.4、一对多映射处理 部门 -->员工

没有级联方式的查询,只有collection 和分步查询

8.4.1 collection

dept接口

java">/*** 查询部门以及部门中的员工信息* @param deptId* @return*/
Dept getDeptAndEmpByDeptId(@Param("deptId") Integer deptId);

dept映射文件中

<!--Dept getDeptAndEmpByDeptId(@Param("deptId") Integer deptId);-->
<select id="getDeptAndEmpByDeptId" resultMap="deptAndEmpResultMap">SELECT *FROM deptLEFT JOIN empON dept.dept_id=emp.dept_idWHERE dept.dept_id=#{deptId}
</select><resultMap id="deptAndEmpResultMap" type="dept"><id column="dept_id" property="deptId"></id><result column="dept_name" property="deptName"></result><!--ofType:设置集合类型的属性中存储的数据的类型--><collection property="emps" ofType="emp"><id column="emp_id" property="empId"></id><result column="emp_name" property="empName"></result><result column="age" property="age"></result><result column="sex" property="sex"></result></collection>
</resultMap>

测试方法

java">@Test
public  void testGetDeptAndEmpByDeptId(){Dept dept = deptMapper.getDeptAndEmpByDeptId(1);System.out.println(dept);
}

8.4.1 分步查询

⑴查询部门信息

java">/*** 通过分步查询进行查询部门及部门中的员工信息的第一步:查询部门信息* @param deptId* @return*/
Dept getDeptAndEmpBySetpOne(@Param("deptId") Integer deptId);
<!-- Dept getDeptAndEmpBySetpOne(@Param("deptId") Integer deptId);-->
<select id="getDeptAndEmpBySetpOne" resultMap="deptAndEmpResultMapByStep">select * from dept where dept_id = #{deptId}
</select><resultMap id="deptAndEmpResultMapByStep" type="dept"><id column="dept_id" property="deptId"></id><result column="dept_name" property="deptName"></result><collection property="emps" column="dept_id"select="com.qcby.mybatis.mapper.EmpMapper.getDeptAndEmpBySetpTwo"></collection>
</resultMap>

⑵查询员工信息

java">/*** 通过分步查询进行查询部门及部门中的员工信息的第二步:查询员工信息* @param deptId* @return*/
List<Emp> getDeptAndEmpBySetpTwo(@Param("deptId")Integer deptId);
<!-- List<Emp> getDeptAndEmpBySetpTwo(@Param("deptId")Integer deptId);-->
<select id="getDeptAndEmpBySetpTwo" resultType="emp">select  * from  emp where dept_id = #{deptId}
</select>

⑶测试方法

java">@Test
public void testGetDeptAndEmpBySetp(){Dept dept = deptMapper.getDeptAndEmpBySetpOne(2);System.out.println(dept);
}

8.5、多对多映射关系

8.5.1、pojo

8.5.2、collection

8.5.3 分步查询

⑴查询订单信息

java">/*** 通过分步查询进行查询订单以及订单中的商品信息的第一步* @param id* @return*/
List<Orders>  findOrdersWithProduct2(@Param("id") Integer id);
<!-- List<Orders>  findOrdersWithProduct2(@Param("id") Integer id);-->
<select id="findOrdersWithProduct2" resultMap="OrdersWithProductResult2">select * from  orders where id = #{id}
</select>
<resultMap id="OrdersWithProductResult2" type="orders"><id column="id" property="id"></id><result column="number" property="number"></result><collection property="productList" column="id" ofType="product"select="com.qcby.mybatis.mapper.ProductMapper.findProductById"></collection>
</resultMap>

⑵查询商品信息

java">/*** 通过分步查询进行查询订单以及订单中的商品信息的第二步* @param id* @return*/
List<Product> findProductById(@Param("id") Integer id);
<!--List<Product> findProductById(@Param("id") Integer id);-->
<select id="findProductById" resultType="product">select * from product where id in(select product_id from ordersitem where orders_id = #{id})
</select>

⑶测试

java">@Test
public void testFindOrdersWithProduct2(){List<Orders> orders = ordersMapper.findOrdersWithProduct2(1);orders.forEach(System.out::println);
}

步查询进行查询订单以及订单中的商品信息的第二步

  • @param id
  • @return
    */
    List findProductById(@Param(“id”) Integer id);

```xml
<!--List<Product> findProductById(@Param("id") Integer id);-->
<select id="findProductById" resultType="product">select * from product where id in(select product_id from ordersitem where orders_id = #{id})
</select>

⑶测试

java">@Test
public void testFindOrdersWithProduct2(){List<Orders> orders = ordersMapper.findOrdersWithProduct2(1);orders.forEach(System.out::println);
}

http://www.ppmy.cn/ops/48558.html

相关文章

攻防演练之-成功的钓鱼邮件溯源

书接上文&#xff0c;《网络安全攻防演练风云》专栏之攻防演练之-网络安全产品大巡礼二&#xff0c;这里。 演练第一天并没有太大的波澜&#xff0c;白天的时间过得很快。夜色降临&#xff0c;攻防演练中心内的灯光依旧明亮。对于网络安全团队来说&#xff0c;夜晚和白天并没有…

如何把路由器设备的LAN口地址为三大私网地址

要将路由器的LAN口地址配置为三大私有IP地址范围之一&#xff08;10.0.0.0/8、172.16.0.0/12 或 192.168.0.0/16&#xff09;&#xff0c;我们需要访问路由器的管理界面并进行相应的设置。 下面是步骤&#xff1a; 连接到路由器&#xff1a; 连接到路由器的管理界面&#xf…

Flutter基础 -- Flutter容器布局

目录 1. MaterialApp 1.1 组件定义 1.2 主要功能和属性 1.3 示例 2. 脚手架 Scaffold 2.1 定义 2.2 Scaffold 的属性 2.3 示例 PS: 对于 Scaffold 和 MaterialApp 3. 线性布局 Column Row 3.1 Row 3.2 Column 4. 盒模型 Box Model 4.1 定义 4.2 示例 5. 容器 C…

C#——类和对象详情

类和对象 类 类是一种数据结构&#xff0c;它可以包含数据成员&#xff08;常量和字段&#xff09;、函数成员&#xff08;方法、属性、事件、索引器、运算符、实例构造函数、静态构造函数和析构函数&#xff09;以及嵌套类型。类类型支持继承&#xff0c;继承是一种机制&…

深入讲解Java线程中 wait 和 notify 的用法和原理

基本概念 wait 和 notify 是 Java 中用于线程间通信的方法&#xff0c;定义在 Object 类中。它们的主要作用是在多线程环境中协调线程的执行顺序。 wait 方法 作用&#xff1a;使当前线程进入等待状态&#xff0c;直到其他线程调用 notify 或 notifyAll 方法唤醒它。调用条件…

【车载音视频电脑】嵌入式AI分析车载DVR,支持8路1080P

产品特点 采用H.265 & H.264编解码&#xff0c;节约存储空间、传输流量&#xff1b; 高分辨率&#xff1a;支持8路1080P*15FPS/4路1080P*30FPS、720P、D1等编解码&#xff1b; 支持1张SATA硬盘&#xff0c;取用方便&#xff0c;满足大容量存储要求&#xff1b; 支持1个…

[word] word表格如何设置外框线和内框线 #媒体#笔记

word表格如何设置外框线和内框线 点击表格的左上角按钮从而选中表格 点击边框按钮边上的下拉箭头&#xff0c;选择边框和底纹 点击颜色边上的下拉箭头&#xff0c;选择红色 点击取消掉中间的边框&#xff0c;只保留外围边框 点击颜色边上的下拉箭头&#xff0c;选择另外一个颜…

common.js和es6中模块引入的区别

common.js CommonJS 是一种模块系统&#xff0c;主要用于 Node.js 环境。它使用 require 函数来引入模块&#xff0c;并使用 module.exports 来导出模块。 语法&#xff1a; 导出模块&#xff1a; // moduleA.js const name Jo; module.exports name;// 或者导出一个对象…

openlayers 绘图功能,编辑多边形,长度面积测量,tip提示(七)

本篇介绍一下使用openlayers添加tips&#xff0c;比如测量时&#xff0c;实时显示长度、面积等&#xff0c;两种方法&#xff1a; 使用Overlay&#xff0c;会添加指定的HTML元素到dom中&#xff0c;会跟随地图的平移】缩放等动作使用vector样式&#xff0c;会渲染到地图的canv…

【十大排序算法】归并排序

归并排序&#xff0c;如同秋日落叶&#xff0c;分散而细碎&#xff0c; 然而风吹叶动&#xff0c;自然而有序&#xff0c; 彼此相遇&#xff0c;轻轻合拢&#xff0c; 最终成就&#xff0c;秩序之谧。 文章目录 一、归并排序二、发展历史三、处理流程四、算法实现五、算法特性…

使用C#快速搭建一个在windows运行的exe应用

文章目录 一、前言1.1 编写语言需要工具1.2 选择自己需要的组件进行安装 二、新建项目1.1 新建一个 .NET4.x 的项目1.2 添加一个小案例1.3 对界面进行美化1.3.1、配置Form属性1.3.2、配置Button按钮 1.4 查看组将的相关代码 三、后记 一、前言 这是一个比较旧的内容&#xff0…

Redis宣布商用后,Redis国产化替代方案有那些?

一、背景 Redis作为使用最为广泛的开源缓存中间件&#xff0c;现已成为IT开发中必不可少的核心组件。官方修改协议印证了“开源”不意味着“无偿使用”&#xff0c;相关限制或将对基于开源Redis提供中间件产品的厂商&#xff0c;及提供Redis服务的云厂商产生一定影响。 二、国…

java版B/S架构UWB人员定位系统源码spring boot+vue技术架构uwb定位装置-工业级UWB室内定位系统源码

java版B/S架构UWB人员定位系统源码spring bootvue技术架构uwb定位装置-工业级UWB室内定位系统源码 本套系统运用UWB定位技术&#xff0c;开发的高精度人员定位系统&#xff0c;通过独特的射频处理&#xff0c;配合先进的位置算法&#xff0c;可以有效计算复杂环境下的人员与物…

【html】如何利用HTML+CSS制作自己的印章

大家有没有尝试过用HTML和CSS制作自己的印章. 首先印章具有两个最基本的特点就是它是圆形的并且有边框 当然它还有一些其他的属性吗&#xff0c;废话不多说我们直接上源码&#xff1a; 效果图&#xff1a; 源码&#xff1a; html&#xff1a; <!DOCTYPE html> <h…

2024 Java 异常—面试常见问题

目录 一、异常的分类 二、throw和throws都是异常处理的关键字&#xff0c;二者区别。 三、try-catch-finally 中&#xff0c;如果 catch 中 return 了&#xff0c;finally 还会执行吗&#xff1f; 四、try-catch-finally 中哪个部分可以省略&#xff1f; 五、常见的 Runti…

C++:SLT容器-->deque

C:SLT容器-->deque 1. 构造函数2. deque 赋值操作3. deque 大小操作4. deque 插入和删除5. deque 容器数据存取6. deque 排序操作 双端数组&#xff0c;可以对头部和尾部进行插入删除操作 需要导入头文件#include <deque> 1. 构造函数 deque deqT; // 默认构造函数 de…

单列集合.java

单列集合 为了存储不同类型的多个对象&#xff0c;Java提供了一些特殊系列的类&#xff0c;这些类可以存储任意类型的对象&#xff0c;并且存储的长度可变&#xff0c;这些类统称为集合。可以简单的理解为一个长度可变&#xff0c;可以存储不同数据类型的动态数组。集合都位于j…

vue关闭页面时触发的函数(ai生成)

在Vue中&#xff0c;可以通过监听浏览器的beforeunload事件来在关闭页面前触发函数。这里是一个简单的示例&#xff1a; new Vue({el: #app,methods: {handleBeforeUnload(event) {// 设置returnValue属性以显示确认对话框event.returnValue 你确定要离开吗&#xff1f;;// 在…

RabbitMQ-工作模式(Topics模式RPC模式Publisher Confirms模式)

文章目录 Topics模式topic代码示例 RPC模式客户端界面回调队列关联ID总结RPC代码示例 Publisher Confirms模式概述在通道上启用发布者确认单独发布消息批量发布消息异步处理发布者确认总结总体代码示例 更多相关内容可查看 Topics模式 在Topics中&#xff0c;发送的消息不能具…

短剧app系统开发(对接广告联盟)源码搭建

随着网络视频的迅速崛起&#xff0c;短剧作为一种新兴的娱乐形式&#xff0c;受到了广大用户的热烈欢迎。为了满足市场对于短剧平台的需求&#xff0c;许多开发者开始关注短剧系统的源码搭建。本文将为你提供一份详尽的短剧系统源码搭建全攻略&#xff0c;帮助你从0到1构建自己…