当前位置: 首页 > news >正文

MyBatis总结(2)- MyBatis实现原理(三)

核心配置

  • JavaBeanMapper.xml(sql映射)

作用

JavaBeanMapper.xml实现:

  1. 用来干什么?

    • 定义Sql语句映射。相对照JDBC的实现,是将原本的Sql代码提取出来,最终根据映射关系执行Sql操作。
  2. 好处?

    • 解耦,mapper只关心定义Sql的映射关系,与java代码分离,更易维护。
  3. 如何使用?

    • 先来展示一个基本的mapper xml,这里涉及到主要的几个标签元素:
      • Select
      • Insert
      • Update
      • Delete
      • ResultMap
      • Sql
      • Cache
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.example.daos.UserMapper"><resultMap id="userMap" type="Customer"><result column="pwd" property="password"/></resultMap><select id="getUserList" resultMap="userMap">SELECT * FROM mybatis.user</select><select id="getUserListByRowBounds" resultMap="userMap">SELECT * FROM mybatis.user</select><!-- 模糊查询1--><!--<select id="getUserListForFuzzyQuery" resultType="org.example.pojo.User">SELECT * FROM mybatis.user where name like #{name}</select>--><!-- 模糊查询2: "%"--><select id="getUserListForFuzzyQuery" resultType="org.example.pojo.User">SELECT * FROM mybatis.user where name like "%"#{name}</select><!-- 形参只有一个,且为基本类型时,parameterType可省略(parameterType="int" )--><select id="getUserById" resultType="org.example.pojo.User">SELECT * FROM mybatis.user where id = #{id}</select><insert id="addUser" parameterType="org.example.pojo.User">INSERT INTO mybatis.user(id, name, pwd) values (#{id},#{name},#{pwd})</insert><update id="updateUserByUser" parameterType="org.example.pojo.User">UPDATE mybatis.user set name=#{name}, pwd=#{pwd} where id=#{id}</update><update id="updateUserByMap" parameterType="map">UPDATE mybatis.user set name=#{userName}, pwd=#{userPwd} where id=#{userId}</update><delete id="deleteUser" parameterType="int">DELETE FROM mybatis.user where id=#{id}</delete>
</mapper>
  1. 具体的标签元素:
    • Select:
      • 这里的重点是,resultType,resultMap的使用,两者只能二选一
        • ResultType:语句中返回结果的类全限定名或别名。一般是该sql映射方法的返回值类型。特殊的,如果是集合类型,则只需定义集合的泛型类型即可。
        • ResultMap:对外部 resultMap 的命名引用。一般用于处理复杂的映射结果查询,比如:多表查询(一对多,多对一):

多对一查询:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.example.daos.StudentMapper"><!-- 多对一查询方式1:Teacher再查一次:子查询相当于:select id, name, tid from student where tid = (select id from teacher where id = tid)--><!--<resultMap id="StudentTeacher1" type="Student"><id property="id" column="id"/><result column="name" property="name"/><association property="teacher" column="tid" select="getTeacherById" javaType="Teacher"/></resultMap><select id="getStudentList" resultMap="StudentTeacher1">select * from student</select><select id="getTeacherById" resultType="Teacher">select * from teacher where id=#{tid}</select>--><!-- 多对一查询方式2:按照结果嵌套, 联表查询相当于:select s.id sid, s.name sname, t.id tid, t.name tname from student s, teacher t where s.tid = t.id--><select id="getStudentList" resultMap="StudentTeacher2">select s.id sid, s.name sname, t.id tid, t.name tname from student s, teacher t where s.tid = t.id</select><resultMap id="StudentTeacher2" type="Student"><result property="id" column="sid"/><result property="name" column="sname"/><association property="teacher" javaType="Teacher"><id column="id" property="tid"/><result property="name" column="tname"/></association></resultMap></mapper>

一对多查询:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.example.daos.TeacherMapper"><!--一对多:方式1:联表查询 --><select id="getTeacherById" resultMap="TeacherStudent">select t.id tid, t.name tname, s.id sid, s.name sname from teacher t, student s where t.id = s.tid and t.id=#{tid}</select><resultMap id="TeacherStudent" type="Teacher"><result property="id" column="tid"/><result property="name" column="tname"/><collection property="studentList" ofType="Student"><result property="id" column="sid"/><result property="name" column="sname"/><result property="tid" column="tid"/></collection></resultMap><!--一对多:方式2:子查询 --><select id="getTeacherById2" resultMap="TeacherStudent2">select * from teacher where id=#{tid}</select><resultMap id="TeacherStudent2" type="Teacher"><result column="id" property="id"/><collection property="studentList" column="id" ofType="Student" select="getStudentByTid"/></resultMap><select id="getStudentByTid" resultType="Student">select * from student where tid=#{tid}</select>
</mapper>
  • Insert:涉及到自动生成主键id的设置(keyProperty, useGeneratedKeys),多行插入(foreach)
  • Update:
  • Delete:
<!-- 自动生成主键id -->
<insert id="insertAuthor" useGeneratedKeys="true"keyProperty="id">insert into Author (username,password,email,bio)values (#{username},#{password},#{email},#{bio})
</insert><!-- 多行插入 -->
<insert id="insertAuthor" useGeneratedKeys="true"keyProperty="id">insert into Author (username, password, email, bio) values<foreach item="item" collection="list" separator=",">(#{item.username}, #{item.password}, #{item.email}, #{item.bio})</foreach>
</insert><update id="updateAuthor">update Author setusername = #{username},password = #{password},email = #{email},bio = #{bio}where id = #{id}
</update><delete id="deleteAuthor">delete from Author where id = #{id}
</delete>
  • Sql:sql语句重用片段。也可动态赋值
<sql id="if_title_author"><if test="title!= null">AND title = #{title}</if><if test="author != null">AND author = #{author}</if>
</sql><select id="queryBlog1" parameterType="map">select * from blog where 1=1<include refid="if_title_author"/>
</select><!-- 动态赋值: ${include_target}, property -->
<sql id="someinclude">from<include refid="${include_target}"/>
</sql>
<select id="select" resultType="map">selectfield1, field2, field3<include refid="someinclude"><property name="prefix" value="Some"/></include>
</select>
  • 参数的定义:
    • 如果一个列允许使用 null 值,并且会使用值为 null 的参数,就必须要指定 JDBC 类型(jdbcType)
#{average,javaType=double,jdbcType=NUMERIC,typeHandler=MyTypeHandler,numericScale=2}
  • 字符串替换: ${}方式不会被预编译转义,可以通过这种方式指定某个字符串column,而非对应的数值。但存在sql注入风险。
@Select("select * from user where ${column} = #{value}")
User findByColumn(@Param("column") String column, @Param("value") String value);
  • association & collection :collection 用于一对多,association用于多对一。
association 联表查询
<association property="author" column="blog_author_id" javaType="Author"><id property="id" column="author_id"/><result property="username" column="author_username"/>
</association>association 子表查询
<resultMap id="blogResult" type="Blog"><association property="author" column="author_id" javaType="Author" select="selectAuthor"/>
</resultMap>collection子表查询
<collection property="posts" column="id" ofType="Post" select="selectPostsForBlog"/>collection联表查询
<resultMap id="blogResult" type="Blog"><id property="id" column="blog_id" /><result property="title" column="blog_title"/><collection property="posts" ofType="Post"><id property="id" column="post_id"/><result property="subject" column="post_subject"/><result property="body" column="post_body"/></collection>
</resultMap>
  • OfType:
<collection property="posts" javaType="ArrayList" column="id" ofType="Post" select="selectPostsForBlog"/>

可以读作: “posts 是一个存储 Post 的 ArrayList 集合” 。且在一般情况下,MyBatis 可以推断 javaType 属性,因此并不需要填写。

  • 缓存Cache:

    • 一级缓存:默认开启。SqlSession级别的缓存,也叫本地缓存
    • 二级缓存:基于namespace级别的缓存,针对mapper <cache>, LRU, FIFO,开启二级缓存,需要在对于mapper上,加入标签元素<Cache>即可
    • 当会话sqlSession提交commit或关闭close时,一级缓存的数据才会提交到二级缓存中!!!
    • 缓存顺序:当查询业务来到DAO层时:
      • 先查看二级缓存;
      • 再查看一级缓存;
      • 最后再查数据库
  • 动态Sql:解决在定义Sql映射时,拼接sql语句:where子句条件,SET子句,多条语句foreach的编写。参考链接

    • If, choose, foreach, trim
  • 分页:limit

    • Select * from user limit startIndex, pageSize
    • RowBounds(selectList (String statement, Object parameter, RowBounds rowBounds)
    • Mybatis PageHelper
  • 注解开发: 参考链接

相关文章:

MyBatis总结(2)- MyBatis实现原理(三)

核心配置 JavaBeanMapper.xml&#xff08;sql映射&#xff09; 作用 JavaBeanMapper.xml实现&#xff1a; 用来干什么&#xff1f; 定义Sql语句映射。相对照JDBC的实现&#xff0c;是将原本的Sql代码提取出来&#xff0c;最终根据映射关系执行Sql操作。 好处&#xff1f; 解…...

【保姆级教程】Linux 基于 Docker 部署 MySQL 和 Nacos 并配置两者连接

一、Linux 部署 Docker 1.1 卸载旧版本&#xff08;如有&#xff09; sudo yum remove docker \docker-client \docker-client-latest \docker-common \docker-latest \docker-latest-logrotate \docker-logrotate \docker-engine1.2 安装 yum-utils 包 sudo yum install -y…...

Dev C++ 安装及使用方法教程-干活多超详细

Dev C 是一款非常好用&#xff0c;简约的C/C开发工具。可以减少很多创建工程的繁琐步骤&#xff0c;很快的进行开发。对于只用于来写代码的人来说&#xff0c;是比较轻量以及极速的。 Dev C 是一个windows下的c和c程序的集成开发环境。它使用mingw32/gcc编译器&#xff0c;遵循…...

无缝滚动的swiper

看效果 看代码 <swiper :indicator-dots"true" :autoplay"true" circular :interval"3000" :duration"6000" display-multiple-items"3" easing-function"linear"><swiper-item v-for"(item,indx…...

tvm实战踩坑

今天玩了一下tvm的安装 我要安装v0.14.0的版本 所以按照官网的方法 https://tvm.apache.org/docs/install/from_source.html#python-package-installation git clone --recursive https://github.com/apache/tvm tvmgit checkout v0.14.0recursive是很重要的 这一步可以替换成…...

计算机网络之网络层知识总结

网络层功能概述 主要任务 主要任务是把分组从源端传到目的端&#xff0c;为分组交换网上的不同主机提供通信服务。网络层传输单位是数据报。 分组和数据报的关系&#xff1a;把数据报进行切割之后&#xff0c;就是分组。 主要功能&#xff1a; 路由选择与分组转发 路由器…...

利用穿戴甲虚拟试戴技术提高销量和参与度

在不断变化的美容行业&#xff0c;保持领先意味着拥抱创新技术。其中一项改变游戏规则的技术是人工智能驱动的虚拟指甲试戴。在穿戴甲领域&#xff0c;不断兴起的虚拟试戴技术对促进销售和参与度产生了重大影响。 视觉吸引力的力量 要了解虚拟试戴的重要性&#xff0c;必须了解…...

后端|压缩Base64图片的两种方式

Base64是一种将二进制数据编码为ASCII字符串的方法。它通过将3个字节的二进制数据转换为4个可打印字符的ASCII字符&#xff0c;从而将二进制数据转换为可传输的文本格式。Base64编码常用于传输图片或音频文件。Base64编码可以保证数据在传输过程中不丢失&#xff0c;同时可以避…...

HCIP认证笔记(单选题)

1、OSPF Hello报文中不包括:process ID 3、IS-IS路由的开销在narrow模式下路由的开销值取值范围是:1~63; 在wide模式下路由的开销取值范围为:1~16777215 4、attached-bit advertise never 命令可以使level-1设备不生成缺省路由; 5、OSPFv3报文封装在IPv6报文内,IPv…...

数据结构笔记-2、线性表

2.1、线性表的定义和基本操作 如有侵权请联系删除。 2.1.1、线性表的定义&#xff1a; ​ 线性表是具有相同数据类型的 n (n>0) 个数据元素的有限序列&#xff0c;其中 n 为表长&#xff0c;当 n 0 时线性表是一个空表。若用 L 命名线性表&#xff0c;则其一般表示为&am…...

Linux基础IO【II】真的很详细

目录 一.文件描述符 1.重新理解文件 1.推论 2.证明 2.理解文件描述符 1.文件描述符的分配规则 3.如何理解文件操作的本质&#xff1f; 4.输入重定向和输出重定向 1.原理 2.代码实现重定向 3.dup函数 ​编辑 4.命令行中实现重定向 二.关于缓冲区 1.现象 …...

【C++】模板及模板的特化

目录 一&#xff0c;模板 1&#xff0c;函数模板 什么是函数模板 函数模板原理 函数模板的实例化 推演(隐式)实例化 显示实例化 模板的参数的匹配原则 2&#xff0c;类模板 什么是类模板 类模板的实例化 二&#xff0c;模板的特化 1&#xff0c;类模板的特化 全特化…...

2001-2023年上市公司数字化转型测算数据(含原始数据+处理代码+计算结果)

2001-2023年上市公司数字化转型测算数据&#xff08;含原始数据处理代码计算结果&#xff09;&#xff08;吴非&#xff09; 1、时间&#xff1a;2001-2023年 2、来源&#xff1a;上市公司年报 3、指标:行业代码、行业名称、证券简称、是否发生ST或ST或PT、是否发生暂停上市…...

ICRA 2024:基于视觉触觉传感器的物体表⾯分类的Sim2Real双层适应⽅法

⼈们通常通过视觉来感知物体表⾯的性质&#xff0c;但有时需要通过触觉信息来补充或替代视觉信息。在机器⼈感知物体属性⽅⾯&#xff0c;基于视觉的触觉传感器是⽬前的最新技术&#xff0c;因为它们可以产⽣与表⾯接触的⾼分辨率 RGB 触觉图像。然⽽&#xff0c;这些图像需要⼤…...

代理模式(设计模式)

文章目录 静态代理动态代理代理模式的应用场景动态代理和静态代理的区别 代理模式就是给一个对象提供一个代理&#xff0c;并由代理对象控制对原对象的引用。它使得客户不能直接与真正的目标对象通信。代理对象是目标对象的代表&#xff0c;其他需要与这个目标对象打交道的操作…...

C++函数参数传递

C 函数传参 在C中&#xff0c;函数传递参数的方式主要有三种&#xff1a; 按值传递&#xff08;pass by value&#xff09;按引用传递&#xff08;pass by reference&#xff09;按指针传递&#xff08;pass by pointer&#xff09;。 比较与总结 按值传递&#xff1a;适用…...

软考初级网络管理员_09_网络单选题

1.下列Internet应用中对实时性要求最高的是()。 电子邮件 Web浏览 FTP文件传输 IP电话 2.在Internet中的大多数服务(如WWW、FTP等)都采用()模型。 星型 主机/终端 客户机/服务器 网状 3.子网掩码的作用是()。 可以用来寻找网关 可以区分IP和MAC 可以识别子网 可以…...

曲线拟合 | 二次B样条拟合曲线

B 样条曲线拟合实例&#xff1a;能平滑化曲线 1. 实例1 为MASS包中mcycle数据集。它测试了一系列模拟的交通车事故中&#xff0c;头部的加速度&#xff0c;以此来评估头盔的性能。times为撞击时间(ms)&#xff0c;accel为加速度&#xff08;g&#xff09;。首先导入数据&#…...

delphi FDMemTable1.SourceView遍历各行数据,取任意行数据无需Next移动指针了。TFDDatSView

for m : 0 to FDMemTable1.SourceView.Rows.Count - 1 do begin if FDMemTable_SP.SourceView.Rows.ItemsI[m].GetData(0) varNull then Continue; end; 9行7列的值。 FDMemTable1.Data.DataView.Rows.ItemsI[9].ValueI[7]; FDMemTable1.Table.Ro…...

为什么选择 ABBYY FineReader PDF ?

帮助用户们对PDF文件进行快速的编辑处理&#xff0c;同时也可以快速识别PDF文件里的文字内容&#xff0c;并且可以让用户们进行文本编辑&#xff0c;所以可以有效提升办公效率。 ABBYY-ABBYY Finereader 15 Win-安装包&#xff1a;https://souurl.cn/OY2L3m 高级转换功能 ABBY…...

装饰模式(Decorator Pattern)重构java邮件发奖系统实战

前言 现在我们有个如下的需求&#xff0c;设计一个邮件发奖的小系统&#xff0c; 需求 1.数据验证 → 2. 敏感信息加密 → 3. 日志记录 → 4. 实际发送邮件 装饰器模式&#xff08;Decorator Pattern&#xff09;允许向一个现有的对象添加新的功能&#xff0c;同时又不改变其…...

【JavaEE】-- HTTP

1. HTTP是什么&#xff1f; HTTP&#xff08;全称为"超文本传输协议"&#xff09;是一种应用非常广泛的应用层协议&#xff0c;HTTP是基于TCP协议的一种应用层协议。 应用层协议&#xff1a;是计算机网络协议栈中最高层的协议&#xff0c;它定义了运行在不同主机上…...

React第五十七节 Router中RouterProvider使用详解及注意事项

前言 在 React Router v6.4 中&#xff0c;RouterProvider 是一个核心组件&#xff0c;用于提供基于数据路由&#xff08;data routers&#xff09;的新型路由方案。 它替代了传统的 <BrowserRouter>&#xff0c;支持更强大的数据加载和操作功能&#xff08;如 loader 和…...

8k长序列建模,蛋白质语言模型Prot42仅利用目标蛋白序列即可生成高亲和力结合剂

蛋白质结合剂&#xff08;如抗体、抑制肽&#xff09;在疾病诊断、成像分析及靶向药物递送等关键场景中发挥着不可替代的作用。传统上&#xff0c;高特异性蛋白质结合剂的开发高度依赖噬菌体展示、定向进化等实验技术&#xff0c;但这类方法普遍面临资源消耗巨大、研发周期冗长…...

大型活动交通拥堵治理的视觉算法应用

大型活动下智慧交通的视觉分析应用 一、背景与挑战 大型活动&#xff08;如演唱会、马拉松赛事、高考中考等&#xff09;期间&#xff0c;城市交通面临瞬时人流车流激增、传统摄像头模糊、交通拥堵识别滞后等问题。以演唱会为例&#xff0c;暖城商圈曾因观众集中离场导致周边…...

解决Ubuntu22.04 VMware失败的问题 ubuntu入门之二十八

现象1 打开VMware失败 Ubuntu升级之后打开VMware上报需要安装vmmon和vmnet&#xff0c;点击确认后如下提示 最终上报fail 解决方法 内核升级导致&#xff0c;需要在新内核下重新下载编译安装 查看版本 $ vmware -v VMware Workstation 17.5.1 build-23298084$ lsb_release…...

Java多线程实现之Callable接口深度解析

Java多线程实现之Callable接口深度解析 一、Callable接口概述1.1 接口定义1.2 与Runnable接口的对比1.3 Future接口与FutureTask类 二、Callable接口的基本使用方法2.1 传统方式实现Callable接口2.2 使用Lambda表达式简化Callable实现2.3 使用FutureTask类执行Callable任务 三、…...

关于 WASM:1. WASM 基础原理

一、WASM 简介 1.1 WebAssembly 是什么&#xff1f; WebAssembly&#xff08;WASM&#xff09; 是一种能在现代浏览器中高效运行的二进制指令格式&#xff0c;它不是传统的编程语言&#xff0c;而是一种 低级字节码格式&#xff0c;可由高级语言&#xff08;如 C、C、Rust&am…...

CRMEB 框架中 PHP 上传扩展开发:涵盖本地上传及阿里云 OSS、腾讯云 COS、七牛云

目前已有本地上传、阿里云OSS上传、腾讯云COS上传、七牛云上传扩展 扩展入口文件 文件目录 crmeb\services\upload\Upload.php namespace crmeb\services\upload;use crmeb\basic\BaseManager; use think\facade\Config;/*** Class Upload* package crmeb\services\upload* …...

Web后端基础(基础知识)

BS架构&#xff1a;Browser/Server&#xff0c;浏览器/服务器架构模式。客户端只需要浏览器&#xff0c;应用程序的逻辑和数据都存储在服务端。 优点&#xff1a;维护方便缺点&#xff1a;体验一般 CS架构&#xff1a;Client/Server&#xff0c;客户端/服务器架构模式。需要单独…...