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

Mybatis基础---------增删查改

目录结构

增删改

1、新建工具类用来获取会话对象
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.io.Resources;import java.io.IOException;
import java.io.InputStream;/*** 静态变量:使用 static 关键字声明的变量是类级别的,所有实例共享相同的静态变量。这意味着无论创建多少个对象,它们都将共享相同的静态变量值。* public class MyClass {*     static int staticVar; // 静态变量,所有实例共享* }* 静态方法:使用 static 关键字声明的方法是类级别的,可以通过类名直接调用,无需实例化对象。这些方法通常用于执行与类本身相关的操作,而不是特定实例的操作。* public class MyClass {*     static void staticMethod() {*         // 静态方法*     }* }* 静态代码块:使用 static 关键字标识的代码块在类加载时执行,通常用于进行类级别的初始化操作。*/
public class SqlSessionUtil {static SqlSessionFactory sqlSessionFactory;static {try {InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);//加载输入流创建会话工厂} catch (IOException e) {e.printStackTrace();}}public  static SqlSession openSession(){return sqlSessionFactory.openSession();}}
2、加入junit依赖
<dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.13.2</version><scope>test</scope>
</dependency>
3、通过映射传递属性

之前的sql语句全部写在了映射文件中,然而在实际应用时是通过映射传递属性的,也就是java对象对应sql语句中的占位符属性,属性名一般和java对象中的属性名相同,我们只需要用#{}作为占位符,占位符名称与java对象属性名一致即可。

如下实体类:

import java.util.Date;
public class User {private Integer id;private String username;private String password;private String salt;private String email;private int type;private int status;private String activationCode;private String headerUrl;private Date createTime;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public String getSalt() {return salt;}public void setSalt(String salt) {this.salt = salt;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}public int getType() {return type;}public void setType(int type) {this.type = type;}public int getStatus() {return status;}public void setStatus(int status) {this.status = status;}public String getActivationCode() {return activationCode;}public void setActivationCode(String activationCode) {this.activationCode = activationCode;}public String getHeaderUrl() {return headerUrl;}public void setHeaderUrl(String headerUrl) {this.headerUrl = headerUrl;}public User(Integer id, String username, String password, String salt, String email, int type, int status, String activationCode, String headerUrl, Date createTime) {this.id = id;this.username = username;this.password = password;this.salt = salt;this.email = email;this.type = type;this.status = status;this.activationCode = activationCode;this.headerUrl = headerUrl;this.createTime = createTime;}public Date getCreateTime() {return createTime;}public void setCreateTime(Date createTime) {this.createTime = createTime;}
}

映射文件

<insert id="insertUser">INSERT INTO users (user_id, username, password, salt, email, type, status, activation_Code, header_url, create_time)
VALUES (null, #{username}, #{password}, #{salt}, #{email}, #{type}, #{status}, #{activationCode}, #{headerUrl}, #{createTime});
</insert>

测试:

@Test
public  void insertTest(){SqlSession sqlSession = SqlSessionUtil.openSession();User user = new User(null, "JohnDoe", "password", "salt123", "johndoe@example.com", 1, 1, "abcxyz", "https://example.com/image.jpg", new Date());//这里传入user实体,mybatis会自动将user属性值填充到sql语句中的占位符sqlSession.insert("insertUser",user);sqlSession.commit();sqlSession.close();//关闭会话
}

测试下修改和删除

<delete id="deleteUser">delete  from users where user_id=#{id};
</delete>
<update id="updateUser">UPDATE usersSET username = #{username},password = #{password},salt = #{salt},email = #{email},type = #{type},status = #{status},activation_Code = #{activationCode},header_Url = #{headerUrl},create_Time = #{createTime}WHERE user_id = #{id};
</update>
@Test//修改
public  void updateTest(){SqlSession sqlSession = SqlSessionUtil.openSession();User user = new User(2, "DDDD", "password", "salt123", "johndoe@example.com", 1, 1, "abcxyz", "https://example.com/image.jpg", new Date());sqlSession.insert("updateUser",user);sqlSession.commit();sqlSession.close();//关闭会话
}
@Test//删除
public  void deleteTest(){SqlSession sqlSession = SqlSessionUtil.openSession();sqlSession.insert("deleteUser",2);//当sql只有一个占位符时,传递的参数会直接赋值到该占位符中,与占位符名称无关sqlSession.commit();sqlSession.close();//关闭会话
}

查询

获取结果集,通过select标签中的resultType参数来指定查询结果封装到对应的实体类中,如果实体类中的属性与数据库表中属性不一致,可以使用as将对应数据库表中列名重命名并与实体类一致。

<select id="selectOneUser" resultType="User">select user_id as id, username, password, salt, email, type, status, activation_Code as activationCode, header_url as headerUrl, create_time as createTime from users where user_id=#{id};
</select>

或者采用另一种方式:通过在<resultMap> 中使用 <result> 标签来进行手动映射。

column--property==>数据库列名--实体类属性名

<resultMap id="userResultMap" type="User"><id property="id" column="user_id"/><result property="username" column="username"/><result property="password" column="password"/><result property="salt" column="salt"/><result property="email" column="email"/><result property="type" column="type"/><result property="status" column="status"/><result property="activationCode" column="activation_Code"/><result property="headerUrl" column="header_url"/><result property="create_time" column="createTime"/>
</resultMap>
<select id="selectUser" resultMap="userResultMap">select  * from users;
</select>
@Test
public void selectTest(){SqlSession sqlSession = SqlSessionUtil.openSession();List<User> selectUser = sqlSession.selectList("selectUser");for (User user : selectUser) {System.out.println(user.toString());}sqlSession.commit();sqlSession.close();//关闭会话
}

这里说明:不管是查询单个记录还多个记录,设置返回封装映射时,resultType应该设置为实体类或者List<实体类>型中的实体类。设置手动映射resultMapper同样如此。

相关文章:

Mybatis基础---------增删查改

目录结构 增删改 1、新建工具类用来获取会话对象 import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.apache.ibatis.io.Resources;import java.io…...

CentOS查看修改时间

经常玩docker的朋友应该都知道&#xff0c;有很多的镜像运行起来后&#xff0c;发现容器里的系统时间不对&#xff0c;一般是晚被北京时间8个小时&#xff08;不一定&#xff09;。 这里合理怀疑是镜像给的初始时区是世界标准时间&#xff08;也叫协调世界时间&#xff09;。 有…...

Kafka消费流程

Kafka消费流程 消息是如何被消费者消费掉的。其中最核心的有以下内容。 1、多线程安全问题 2、群组协调 3、分区再均衡 1.多线程安全问题 当多个线程访问某个类时&#xff0c;这个类始终都能表现出正确的行为&#xff0c;那么就称这个类是线程安全的。 对于线程安全&…...

RPC原理介绍与使用(@RpcServiceAnnotation)

Java RPC&#xff08;Remote Procedure Call&#xff0c;远程过程调用&#xff09;是一种用于实现分布式系统中不同节点之间通信的技术。它允许在不同的计算机或进程之间调用远程方法&#xff0c;就像调用本地方法一样。 ** 一.Java RPC的原理如下&#xff1a; ** 定义接口&…...

力扣labuladong——一刷day94

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 前言二叉堆&#xff08;Binary Heap&#xff09;没什么神秘&#xff0c;性质比二叉搜索树 BST 还简单。其主要操作就两个&#xff0c;sink&#xff08;下沉&#xf…...

Vim 是一款强大的文本编辑器,广泛用于 Linux 和其他 Unix 系统。以下是 Vim 的一些基本用法

Vim 是一款强大的文本编辑器&#xff0c;广泛用于 Linux 和其他 Unix 系统。以下是 Vim 的一些基本用法&#xff1a; 打开文件&#xff1a; vim filename 基本移动&#xff1a; 使用箭头键或 h, j, k, l 分别向左、下、上、右移动。Ctrl f: 向前翻页。Ctrl b: 向后翻页。…...

软件工程:黑盒测试等价分类法相关知识和多实例分析

目录 一、黑盒测试和等价分类法 1. 黑盒测试 2. 等价分类法 二、黑盒测试等价分类法实例分析 1. 工厂招工年龄测试 2. 规定电话号码测试 3. 八位微机测试 4. 三角形判断测试 一、黑盒测试和等价分类法 1. 黑盒测试 黑盒测试就是根据被测试程序功能来进行测试&#xf…...

stable-diffusion 学习笔记

必看文档&#xff1a; 万字长篇&#xff01;超全Stable Diffusion AI绘画参数及原理详解 - 知乎 &#xff08;提示词&#xff09;语法控制 常用语法&#xff1a; 加权&#xff1a;() 或 {} 降权&#xff1a;[](word)//将括号内的提示词权重提高 1.1 倍 ((word))//将括号内的提示…...

手写webpack核心原理,支持typescript的编译和循环依赖问题的解决

主要知识点 babel读取代码的import语句算法&#xff1a;bfs遍历依赖图为浏览器定义一个require函数的polyfill算法&#xff1a;用记忆化搜索解决require函数的循环依赖问题 Quick Start GitHub&#xff1a;https://github.com/Hans774882968/mini-webpack npm install npm…...

开箱即用之MyBatisPlus XML 自定义分页

调用方法 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;public Page<User> queryListByPage(User user) { Page<User> page new Page<>(1, 12); return userMapper.queryListByPage(page, user); } mapper接口 import co…...

GPT应用开发:运行你的第一个聊天程序

本系列文章介绍基于OpenAI GPT API开发应用的方法&#xff0c;适合从零开始&#xff0c;也适合查缺补漏。 本文首先介绍基于聊天API编程的方法。 环境搭建 很多机器学习框架和类库都是使用Python编写的&#xff0c;OpenAI提供的很多例子也是Python编写的&#xff0c;所以为了…...

力扣刷MySQL-第一弹(详细解析)

&#x1f389;欢迎您来到我的MySQL基础复习专栏 ☆* o(≧▽≦)o *☆哈喽~我是小小恶斯法克&#x1f379; ✨博客主页&#xff1a;小小恶斯法克的博客 &#x1f388;该系列文章专栏&#xff1a;力扣刷题讲解-MySQL &#x1f379;文章作者技术和水平很有限&#xff0c;如果文中出…...

Xcode 15 for Mac:超越开发的全新起点

作为一名开发人员&#xff0c;你是否正在寻找一款强大而高效的开发工具&#xff0c;来帮助你在Mac上构建出卓越的应用程序&#xff1f;那么&#xff0c;Xcode 15就是你一直在寻找的答案。 Xcode 15是苹果公司最新推出的一款集成开发环境&#xff08;IDE&#xff09;&#xff0…...

2021腾讯、华为前端面试题集(基础篇)

Vue 面试题 生命周期函数面试题 1.什么是 vue 生命周期2.vue 生命周期的作用是什么 3.第一次页面加载会触发哪几个钩子 4.简述每个周期具体适合哪些场景 5.created 和 mounted 的区别 6.vue 获取数据在哪个周期函数 7.请详细说下你对 vue 生命周期的理解&#xff1f; **vue 路由…...

怎么修改或移除WordPress后台仪表盘概览底部的版权信息和主题信息?

前面跟大家分享『WordPress怎么把后台左上角的logo和评论图标移除&#xff1f;』和『WordPress后台底部版权信息“感谢使用 WordPress 进行创作”和版本号怎么修改或删除&#xff1f;』&#xff0c;其实在WordPress后台仪表盘的“概览”底部还有一个WordPress版权信息和所使用的…...

计算机三级(网络技术)——应用题

第一题 61.输出端口S0 &#xff08;直接连接&#xff09; RG的输出端口S0与RE的S1接口直接相连构成一个互联网段 对172.0.147.194和172.0.147.193 进行聚合 前三段相同&#xff0c;将第四段分别转换成二进制 11000001 11000010 前6位相同&#xff0c;加上前面三段 共30…...

Node.js基础知识点(四)

本节介绍一下最简单的http服务 一.http 可以使用Node 非常轻松的构建一个web服务器&#xff0c;在 Node 中专门提供了一个核心模块&#xff1a;http http 这个模块的就可以帮你创建编写服务器。 1. 加载 http 核心模块 var http require(http) 2. 使用 http.createServe…...

持久双向通信网络协议-WebSocket-入门案例实现demo

1 介绍 WebSocket 是基于 TCP 的一种新的网络协议。它实现了浏览器与服务器全双工通信——浏览器和服务器只需要完成一次握手&#xff0c;两者之间就可以创建持久性的连接&#xff0c; 并进行双向数据传输。 HTTP协议和WebSocket协议对比&#xff1a; HTTP是短连接&#xff0…...

LV.13 D10 Linux内核移植 学习笔记

具体实验步骤在lv13day10 实验十 一、Linux内核概述 1.1 内核与操作系统 内核 内核是一个操作系统的核心&#xff0c;提供了操作系统最基本的功能&#xff0c;是操作系统工作的基础&#xff0c;决定着整个系统的性能和稳定性 操作系统 操作系统是在内核的基础上添…...

STM32面试体验和题目

目录 一、说一下你之前的工作主要干了什么&#xff1f; 二、stm32有关的知识点 1.stm32的外设有哪一些 2.你的毕业论文的项目里面是怎么设计的 三&#xff0c;C语言的考察 1.写一个结构体&#xff08;结构体的内容自由发挥&#xff09; 2.写一个指针型的变量 3.结构体是…...

模型参数、模型存储精度、参数与显存

模型参数量衡量单位 M&#xff1a;百万&#xff08;Million&#xff09; B&#xff1a;十亿&#xff08;Billion&#xff09; 1 B 1000 M 1B 1000M 1B1000M 参数存储精度 模型参数是固定的&#xff0c;但是一个参数所表示多少字节不一定&#xff0c;需要看这个参数以什么…...

R语言AI模型部署方案:精准离线运行详解

R语言AI模型部署方案:精准离线运行详解 一、项目概述 本文将构建一个完整的R语言AI部署解决方案,实现鸢尾花分类模型的训练、保存、离线部署和预测功能。核心特点: 100%离线运行能力自包含环境依赖生产级错误处理跨平台兼容性模型版本管理# 文件结构说明 Iris_AI_Deployme…...

DAY 47

三、通道注意力 3.1 通道注意力的定义 # 新增&#xff1a;通道注意力模块&#xff08;SE模块&#xff09; class ChannelAttention(nn.Module):"""通道注意力模块(Squeeze-and-Excitation)"""def __init__(self, in_channels, reduction_rat…...

YSYX学习记录(八)

C语言&#xff0c;练习0&#xff1a; 先创建一个文件夹&#xff0c;我用的是物理机&#xff1a; 安装build-essential 练习1&#xff1a; 我注释掉了 #include <stdio.h> 出现下面错误 在你的文本编辑器中打开ex1文件&#xff0c;随机修改或删除一部分&#xff0c;之后…...

基于Uniapp开发HarmonyOS 5.0旅游应用技术实践

一、技术选型背景 1.跨平台优势 Uniapp采用Vue.js框架&#xff0c;支持"一次开发&#xff0c;多端部署"&#xff0c;可同步生成HarmonyOS、iOS、Android等多平台应用。 2.鸿蒙特性融合 HarmonyOS 5.0的分布式能力与原子化服务&#xff0c;为旅游应用带来&#xf…...

对WWDC 2025 Keynote 内容的预测

借助我们以往对苹果公司发展路径的深入研究经验&#xff0c;以及大语言模型的分析能力&#xff0c;我们系统梳理了多年来苹果 WWDC 主题演讲的规律。在 WWDC 2025 即将揭幕之际&#xff0c;我们让 ChatGPT 对今年的 Keynote 内容进行了一个初步预测&#xff0c;聊作存档。等到明…...

Qt Http Server模块功能及架构

Qt Http Server 是 Qt 6.0 中引入的一个新模块&#xff0c;它提供了一个轻量级的 HTTP 服务器实现&#xff0c;主要用于构建基于 HTTP 的应用程序和服务。 功能介绍&#xff1a; 主要功能 HTTP服务器功能&#xff1a; 支持 HTTP/1.1 协议 简单的请求/响应处理模型 支持 GET…...

SAP学习笔记 - 开发26 - 前端Fiori开发 OData V2 和 V4 的差异 (Deepseek整理)

上一章用到了V2 的概念&#xff0c;其实 Fiori当中还有 V4&#xff0c;咱们这一章来总结一下 V2 和 V4。 SAP学习笔记 - 开发25 - 前端Fiori开发 Remote OData Service(使用远端Odata服务)&#xff0c;代理中间件&#xff08;ui5-middleware-simpleproxy&#xff09;-CSDN博客…...

Qemu arm操作系统开发环境

使用qemu虚拟arm硬件比较合适。 步骤如下&#xff1a; 安装qemu apt install qemu-system安装aarch64-none-elf-gcc 需要手动下载&#xff0c;下载地址&#xff1a;https://developer.arm.com/-/media/Files/downloads/gnu/13.2.rel1/binrel/arm-gnu-toolchain-13.2.rel1-x…...

【学习笔记】erase 删除顺序迭代器后迭代器失效的解决方案

目录 使用 erase 返回值继续迭代使用索引进行遍历 我们知道类似 vector 的顺序迭代器被删除后&#xff0c;迭代器会失效&#xff0c;因为顺序迭代器在内存中是连续存储的&#xff0c;元素删除后&#xff0c;后续元素会前移。 但一些场景中&#xff0c;我们又需要在执行删除操作…...