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

Spring Mybatis 基本使用 总结

1. 简介

Mybatis库可以简化数据库的操作,专注于sql语句。

2.搭建步骤

2.1 在pom.xml引入mybatis

<dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.11</version>
</dependency>

2.3 在resources下新建mybatis配置文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Mapper 3.0/EN""https://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration><settings>
<!--        <setting name="logImpl" value="STDOUT_LOGGING"/>&lt;!&ndash; 开启mybatis的日志输出 &ndash;&gt;--><setting name="mapUnderscoreToCamelCase" value="true"/><!-- 开启驼峰式自动映射 a_big => aBig --></settings><typeAliases><typeAlias alias="goods" type="com.jojo.pojo.Goods"/><!-- 单独设置别名 --><package name="com.jojo.pojo"/><!-- 批量设置别名, com.jojo.pojo包下的所有类名的别名为类的首字母小写--></typeAliases><environments default="development"><environment id="development"><transactionManager type="JDBC"/>  <!-- 自动开启事务 --><dataSource type="POOLED"><!-- mybatis维护连接池 --><property name="driver" value="com.mysql.cj.jdbc.Driver"/><property name="url" value="jdbc:mysql://localhost:3306/mybatis-example"/><property name="username" value="root"/><property name="password" value="a12345678"/></dataSource></environment></environments><mappers><!-- 指定mapper xml文件的位置 --><mapper resource="mappers/GoodsMapper.xml"/></mappers>
</configuration>

2.3 在resources/mapper下新建mapper的xml配置文件:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0/EN""https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.jojo.mapper.GoodsMapper"><!-- 对应Mapper的全限定符 -->
<!--    这里写sql语句 --><insert id="insert" useGeneratedKeys="true" keyColumn="id" keyProperty="id">insert into goods (name) value(#{name})</insert><update id="update">update goods set name=#{name} where id=#{id}</update><delete id="delete">delete from goods where id = #{id}</delete><select id="selectById" resultType="goods">select * from goods where id = #{id}</select><select id="selectAll" resultType="goods">select * from goods</select>
</mapper>

2.4 新建pojo类

import lombok.Data;
@Data//lombook插件的@Data标签可以自动生成get和set以及toString方法
public class Goods {private Integer id;private String name;
}

2.5 新建mapper接口

public interface GoodsMapper {int insert(Goods goods);int update(Goods goods);int delete(Integer id);Goods selectById(Integer id);List<Goods> selectAll();
}

2.6 测试

public class MybatisTest {@Testpublic void test() throws IOException {//1.读取外部配置文件InputStream ips = Resources.getResourceAsStream("mybatis-config.xml");//2.创建sqlSessionFactorySqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(ips);//3.根据sqlSessionFactory创建sqlSessionSqlSession sqlSession = sqlSessionFactory.openSession();//4.获取接口的代理对象,调用代理对象的方法就会查找mapper接口的方法GoosdMapper mapper = sqlSession.getMapper(GoosdMapper.class);Goods goods = mapper.queryById(1);System.out.println(goods);//5.提交事务和释放资源//sqlSession.commit();sqlSession.close();}
}

3.常用mapper语句

3.1 传入值

<!-- #{id} = 使用占位符?,防止sql注入攻击,但不能替代表名表项-->
<!-- ${id} = 不使用占位符?,不能防止sql注入攻击,但可以替代表名表项-->
<select id="queryById" resultType="com.jojo.pojo.Employee">select emp_id empId,emp_name empName, emp_salary empSalary from t_emp where emp_id = #{id}
</select><delete id="deleteById">delete from t_emp where emp_id = #{id} <!-- 传入Integer类型,id可以改写成任意字符串-->
</delete><select id="queryBySalary" resultType="com.jojo.pojo.Employee">select emp_id empId,emp_name empName, emp_salary empSalary from t_emp where empSalary = #{salary} <!-- 传入Double类型,salary可以改写成任意字符串-->
</select><insert id="insertEmp">insert into t_emp (emp_name, emp_salary) values (#{empName},#{empSalary});<!-- 传入对象时,要写传入对象的属性 -->
</insert><select id="queryByNameAndSalary" resultType="com.jojo.pojo.Employee">select emp_id empId,emp_name empName, emp_salary empSalary from t_emp where empSalary = #{a} and empName = #{b} <!-- 传入两个基本类型,根据接口中的@Param("名称")来指定-->
</select><select id="queryByNameAndSalary" resultType="com.jojo.pojo.Employee">select emp_id empId,emp_name empName, emp_salary empSalary from t_emp where empSalary = #{arg0} and empName = #{arg1} <!-- 法2:传入两个基本类型,可以根据顺序来取arg0...arg1...-->
</select><select id="queryByNameAndSalary" resultType="com.jojo.pojo.Employee">select emp_id empId,emp_name empName, emp_salary empSalary from t_emp where empSalary = #{param1} and empName = #{param2} <!-- 法3:传入两个基本类型,可以根据顺序来取param1...param2...-->
</select><insert id="insertEmpMap">insert into t_emp (emp_name, emp_salary) values (#{name},#{salary});<!-- 传入Map时,要写传入Map的key -->
</insert>

3.2 返回值

<select id="queryNameById" resultType="string"><!-- resultType指定返回的类型,写类的全限定符或者mybatis提供的别名(在mybatis官网查)-->select emp_name from t_emp where emp_id = #{id} 
</select><select id="queryById" resultType="employee"> <!-- resultType指定返回的为对象时,select的行需要起别名来与类的属性完全一致-->select emp_id empId,emp_name empName, emp_salary empSalary from t_emp where emp_id = #{id} 
</select><select id="queryById" resultType="employee"><!-- resultType指定返回的为对象时,开启驼峰映射(mapUnderscoreToCamelCase)后,select的行不再需要起别名来与类的属性完全一致-->select * from t_emp where emp_id = #{id}  
</select><select id="selectEmpNameAndMaxSalary" resultType="map"> <!-- resultType返回的值没有未定义类时,可以用map接值,map的每一项的key对应一个列名 -->select emp_name 员工姓名, emp_salary 员工工资, (SELECT AVG(emp_salary) from t_emp) 部门平均工资 from t_emp where emp_salary=(select max(emp_salary) from t_emp)
</select><select id="queryNamesBySalary" resultType="string"><!--如果返回类型时List<String>,那么指定String即可-->select emp_name from t_emp where emp_salary > #{ salary};
</select><select id="queryAll" resultType="employee"><!--如果返回类型时List<Employee>,那么指定Employee即可-->select * from t_emp;
</select><insert id="insertEmp" useGeneratedKeys="true" keyColumn="emp_id" keyProperty="empId"><!-- 主键自增长型:插入时,获取插入的id放在empId中 -->insert into t_emp (emp_name, emp_salary) value(#{empName},#{empSalary});
</insert><insert id="insertTeacher"><selectKey order="BEFORE" resultType="string" keyProperty="tId">select replace(UUID(),'-',''); <!-- 插入前由数据库生成uuid并放在tId中--></selectKey>insert into teacher (t_id,t_name) value (#{tId},#{tName})
</insert>

4.多表查询

4.1 一对一

1对1关系:一个A类中包含一个B类:

public class A {private Integer Id;private String aName;private Integer bId;private B b;
}public class B {private Integer bId;private String bName;
}

使用resultMap来装数据:

<resultMap id="aMap" type="a"><!-- a的主键 id标签--><id column="a_id" property="aId"/><!-- order的普通列 custom标签--><result column="a_name" property="aName"/><result column="b_id" property="bId"/><!-- 给第二层对象属性赋值 --><association property="b" javaType="b"><id column="b_id" property="bId"/><result column="b_name"  property="bName"></result></association>
</resultMap><select id="queryAById" resultMap="aMap">SELECT * FROM t_a ta join t_b tb on ta.b_id = tb.b_id where ta.a_id = #{id};
</select>

在config文件中加入:

<settings><!-- 开启驼峰式自动映射 a_big => aBig --><setting name="mapUnderscoreToCamelCase" value="true"/><!-- 开启自动映射 a_big => aBig --><setting name="autoMappingBehavior" value="FULL"/>
</settings>

后可省略主键以外的映射关系:

<resultMap id="aMap" type="a"><!-- a的主键 id标签--><id column="a_id" property="aId"/><!-- 给第二层对象属性赋值 --><association property="b" javaType="b"><id column="b_id" property="bId"/></association>
</resultMap><select id="queryAById" resultMap="aMap">SELECT * FROM t_a ta join t_b tb on ta.b_id = tb.b_id where ta.a_id = #{id};
</select>

4.2 一对多

1对多关系:一个A类中包含多个B类(List):

public class A {private Integer Id;private String aName;private Integer bId;private List<B> bList;
}public class B {private Integer bId;private String bName;
}

使用resultMap来装数据:

<resultMap id="aMap" type="a"><id column="a_id" property="aId"/><result column="a_name" property="aName"/><result column="b_id" property="bId"/><!--针对List<A>属性使用collection --><collection property="bList" ofType="b"><id column="b_id" property="bId"></id><result column="b_name" property="bName"/></collection>
</resultMap><select id="queryAList" resultMap="aMap">select * from t_a ta join t_b tb on ta.customer_id = tb.customer_id
</select>

在config文件中加入:

<settings><!-- 开启驼峰式自动映射 a_big => aBig --><setting name="mapUnderscoreToCamelCase" value="true"/><!-- 开启自动映射 a_big => aBig --><setting name="autoMappingBehavior" value="FULL"/>
</settings>

后可省略主键以外的映射关系:

<resultMap id="aMap" type="a"><id column="a_id" property="aId"/><!--针对List<A>属性使用collection --><collection property="bList" ofType="b"><id column="b_id" property="bId"></id></collection>
</resultMap><select id="queryAList" resultMap="aMap">select * from t_a ta join t_b tb on ta.customer_id = tb.customer_id
</select>

相关文章:

Spring Mybatis 基本使用 总结

1. 简介 Mybatis库可以简化数据库的操作&#xff0c;专注于sql语句。 2.搭建步骤 2.1 在pom.xml引入mybatis <dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.11</version> </dep…...

接口幂等性和并发安全的区别?

目录标题 幂等性并发安全总结 接口幂等性和并发安全是两个不同的概念&#xff0c;虽然它们在设计API时都很重要&#xff0c;但侧重点不同。 幂等性 定义&#xff1a;幂等性指的是无论对接口进行多少次相同的操作&#xff0c;结果都是一致的。例如&#xff0c;HTTP的PUT和DELE…...

【记录一下VMware上开虚拟端口映射到公网】

材料 win11 和装在vmware上的ubuntu 步骤一在Ubuntu上配置静态地址&#xff0c;配置如下 vim /etc/netplan/01-network-manager-all.yaml(此文件看系统上对应的是哪个文件&#xff0c;建议先备份)network:version: 2renderer: NetworkManagerethernets:ens33:dhcp4: falseadd…...

半导体器件制造5G智能工厂数字孪生物联平台,推进制造业数字化转型

半导体器件制造行业作为高科技领域的核心驱动力&#xff0c;正积极探索和实践以5G智能工厂数字孪生平台为核心的新型制造模式。这一创新不仅极大地提升了生产效率与质量&#xff0c;更为制造业的未来发展绘制了一幅智能化、网络化的宏伟蓝图。 在半导体器件制造5G智能工厂中&a…...

数据结构之存储位置

p 和 "hello,world"存储在内存哪个区域&#xff1f;( ) (鲁科安全) int main() { char *p "hello,world"; return 0; } p是栈区&#xff0c;”hello,world”是.ro段 一个由C/C编译的程序&#xff0c;会将占用的内存分为几个部分&#xff1a;堆、栈、代…...

传输层协议(TCP和UDP)

目录 一、UDP 1、UDPAPI 2、UDPAPI的使用 二、TCP 1、TCPAPI 2、TCP的相关特性 2.1 确认应答 2.2 超时重传 2.3 连接管理&#xff08;三次握手&#xff0c;四次挥手&#xff09; 2.4 滑动窗口 2.5 流量控制 2.6 拥塞控制 2.7 延时应答 2.8 捎带应答 2.9 面向字节…...

智能仓库|基于springBoot的智能无人仓库管理设计与实现(附项目源码+论文+数据库)

私信或留言即免费送开题报告和任务书&#xff08;可指定任意题目&#xff09; 目录 一、摘要 二、相关技术 三、系统设计 四、数据库设计 五、核心代码 六、论文参考 七、源码获取 一、摘要 互联网发展至今&#xff0c;无论是其理论还是技术都已经成熟&#xf…...

2.《DevOps》系列K8S部署CICD流水线之部署NFS网络存储与K8S创建StorageClass

架构 服务器IP服务名称硬件配置192.168.1.100k8s-master8核、16G、120G192.168.1.101k8s-node18核、16G、120G192.168.1.102k8s-node28核、16G、120G192.168.1.103nfs2核、4G、500G操作系统:Rocky9.3 后续通过K8S部署GitLab、Harbor、Jenkins 一、环境准备 #关闭防火墙开机自…...

【数据仓库】数据仓库常见的数据模型——维度模型

文章部分图参考自&#xff1a;多维数据模型各种类型&#xff08;星型、雪花、星座、交叉连接&#xff09; - 知乎 (zhihu.com) 文章部分文字canla一篇文章搞懂数据仓库&#xff1a;四种常见数据模型&#xff08;维度模型、范式模型等&#xff09;-腾讯云开发者社区-腾讯云 (ten…...

【Kubernetes】常见面试题汇总(三十)

目录 82. Worker 节点宕机&#xff0c;简述 Pods 驱逐流程。 特别说明&#xff1a; 题目 1-68 属于【Kubernetes】的常规概念题&#xff0c;即 “ 汇总&#xff08;一&#xff09;~&#xff08;二十二&#xff09;” 。 题目 69-113 属于【Kubernetes】的生产应用题。 8…...

【Web】PolarCTF2024秋季个人挑战赛wp

EZ_Host 一眼丁真命令注入 payload: ?host127.0.0.1;catf* 序列一下 exp: <?phpclass Polar{public $lt;public $b; } $pnew Polar(); $p->lt"system"; $p->b"tac /f*"; echo serialize($p);payload: xO:5:"Polar":2:{s:2:"…...

职业技能大赛-自动化测试笔记分享-2

一、时间等待处理 1、强制等待(无条件等待) 使用方法:time.sleep(delay) delay的单位为秒,delay设置多少秒页面就会等待多长时间,容易让线程挂掉,使程序抛异常,所以要慎用此方法。 #导入强制等待模块 import time from selenium import webdriverwd = webdriver.Chro…...

LeetCode讲解篇之1343. 大小为 K 且平均值大于等于阈值的子数组数目

文章目录 题目描述题解思路题解代码 题目描述 题解思路 题目让我们求长度为k的子数组并且该子数组的平均值大于threshold&#xff0c;对于这题&#xff0c;我们可以考虑维护一个长度为k的窗口&#xff0c;窗口不断向右滑动&#xff0c;遍历所有长度为k的子数组&#xff0c;我们…...

电子元件制造5G智能工厂物联数字孪生平台,推进制造业数字化转型

5G智能工厂与物联数字孪生平台的融合应用&#xff0c;不仅为电容器制造业注入了新的活力&#xff0c;更为整个制造业的数字化转型树立了新的标杆。电子元件制造过程中&#xff0c;数字孪生平台通过实时监测生产线的各个环节&#xff0c;实现了生产流程的可视化监控。管理人员可…...

【成品论文】2024年华为杯研赛E题25页高质量成品论文(后续会更新

您的点赞收藏是我继续更新的最大动力&#xff01; 一定要点击如下的卡片链接&#xff0c;那是获取资料的入口&#xff01; 点击链接加入【2024华为杯研赛资料汇总】&#xff1a;https://qm.qq.com/q/Mxv2XNWxUc https://qm.qq.com/q/Mxv2XNWxUc 高速公路应急车道紧急启用模型…...

【后端】【语言】【python】python常见操作

文章目录 1. List 操作2. JSON 操作3. Dict 操作 下面是分别演示 list、json、dict 操作 1. List 操作 my_list[] # List 操作示例 my_list [1, 2, 3, "apple", True]# 添加元素 my_list.append("new item") # [1, 2, 3, "apple", True, &qu…...

二叉树的链式结构和递归程序的递归流程图

二叉树的链式存储结构是指&#xff0c;用链表来表示一棵二叉树&#xff0c;即用链来指示元素的逻辑关系。 通常的方法是链表中每个结点由三个域组成&#xff0c;数据域和左右指针域&#xff0c;左右指针分别用来给出该结点左孩子和右孩子所在的链结点的存储地址 。链式结构又分…...

研究生如何利用 ChatGPT 帮助开展日常科研工作?

ChatGPT科研 一、 如何精读论文“三步提问法”1.为什么要做这个研究&#xff1f;这个研究是否值得我们做&#xff1f;2.他们怎么做这个研究3.他们发现了什么&#xff1f; 二、如何利用ChatGPT快速精读论文&#xff1f;首先&#xff0c;“三步走之第一步”--为什么要做这个研究&…...

【LLM学习之路】9月16日 第六天

【LLM学习之路】9月16日 第六天 损失函数 L1Loss 可以取平均也可以求和 参数解析 input &#xff08;N&#xff0c;*&#xff09; N是batchsize&#xff0c;星号代表可以是任意维度 不是输入的参数&#xff0c;只是描述数据 target 形状要同上 MSELoss平方差 CrossEntr…...

Qt_窗口界面QMainWindow的介绍

目录 1、菜单栏QMenuBar 1.1 使用QMainWindow的准备工作 1.2 在ui文件中设计窗口 1.3 在代码中设计窗口 1.4 实现点击菜单项的反馈 1.5 菜单中设置快捷键 1.6 菜单中添加子菜单 1.7 菜单项中添加分割线和图标 1.8 关于菜单栏创建方式的讨论 2、工具栏QToolBar …...

Python爬虫实战:研究MechanicalSoup库相关技术

一、MechanicalSoup 库概述 1.1 库简介 MechanicalSoup 是一个 Python 库,专为自动化交互网站而设计。它结合了 requests 的 HTTP 请求能力和 BeautifulSoup 的 HTML 解析能力,提供了直观的 API,让我们可以像人类用户一样浏览网页、填写表单和提交请求。 1.2 主要功能特点…...

XCTF-web-easyupload

试了试php&#xff0c;php7&#xff0c;pht&#xff0c;phtml等&#xff0c;都没有用 尝试.user.ini 抓包修改将.user.ini修改为jpg图片 在上传一个123.jpg 用蚁剑连接&#xff0c;得到flag...

【磁盘】每天掌握一个Linux命令 - iostat

目录 【磁盘】每天掌握一个Linux命令 - iostat工具概述安装方式核心功能基础用法进阶操作实战案例面试题场景生产场景 注意事项 【磁盘】每天掌握一个Linux命令 - iostat 工具概述 iostat&#xff08;I/O Statistics&#xff09;是Linux系统下用于监视系统输入输出设备和CPU使…...

大语言模型如何处理长文本?常用文本分割技术详解

为什么需要文本分割? 引言:为什么需要文本分割?一、基础文本分割方法1. 按段落分割(Paragraph Splitting)2. 按句子分割(Sentence Splitting)二、高级文本分割策略3. 重叠分割(Sliding Window)4. 递归分割(Recursive Splitting)三、生产级工具推荐5. 使用LangChain的…...

定时器任务——若依源码分析

分析util包下面的工具类schedule utils&#xff1a; ScheduleUtils 是若依中用于与 Quartz 框架交互的工具类&#xff0c;封装了定时任务的 创建、更新、暂停、删除等核心逻辑。 createScheduleJob createScheduleJob 用于将任务注册到 Quartz&#xff0c;先构建任务的 JobD…...

在 Nginx Stream 层“改写”MQTT ngx_stream_mqtt_filter_module

1、为什么要修改 CONNECT 报文&#xff1f; 多租户隔离&#xff1a;自动为接入设备追加租户前缀&#xff0c;后端按 ClientID 拆分队列。零代码鉴权&#xff1a;将入站用户名替换为 OAuth Access-Token&#xff0c;后端 Broker 统一校验。灰度发布&#xff1a;根据 IP/地理位写…...

转转集团旗下首家二手多品类循环仓店“超级转转”开业

6月9日&#xff0c;国内领先的循环经济企业转转集团旗下首家二手多品类循环仓店“超级转转”正式开业。 转转集团创始人兼CEO黄炜、转转循环时尚发起人朱珠、转转集团COO兼红布林CEO胡伟琨、王府井集团副总裁祝捷等出席了开业剪彩仪式。 据「TMT星球」了解&#xff0c;“超级…...

PL0语法,分析器实现!

简介 PL/0 是一种简单的编程语言,通常用于教学编译原理。它的语法结构清晰,功能包括常量定义、变量声明、过程(子程序)定义以及基本的控制结构(如条件语句和循环语句)。 PL/0 语法规范 PL/0 是一种教学用的小型编程语言,由 Niklaus Wirth 设计,用于展示编译原理的核…...

CMake控制VS2022项目文件分组

我们可以通过 CMake 控制源文件的组织结构,使它们在 VS 解决方案资源管理器中以“组”(Filter)的形式进行分类展示。 🎯 目标 通过 CMake 脚本将 .cpp、.h 等源文件分组显示在 Visual Studio 2022 的解决方案资源管理器中。 ✅ 支持的方法汇总(共4种) 方法描述是否推荐…...

JAVA后端开发——多租户

数据隔离是多租户系统中的核心概念&#xff0c;确保一个租户&#xff08;在这个系统中可能是一个公司或一个独立的客户&#xff09;的数据对其他租户是不可见的。在 RuoYi 框架&#xff08;您当前项目所使用的基础框架&#xff09;中&#xff0c;这通常是通过在数据表中增加一个…...