当前位置: 首页 > 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.结构体是…...

【杂谈】-递归进化:人工智能的自我改进与监管挑战

递归进化&#xff1a;人工智能的自我改进与监管挑战 文章目录 递归进化&#xff1a;人工智能的自我改进与监管挑战1、自我改进型人工智能的崛起2、人工智能如何挑战人类监管&#xff1f;3、确保人工智能受控的策略4、人类在人工智能发展中的角色5、平衡自主性与控制力6、总结与…...

Spark 之 入门讲解详细版(1)

1、简介 1.1 Spark简介 Spark是加州大学伯克利分校AMP实验室&#xff08;Algorithms, Machines, and People Lab&#xff09;开发通用内存并行计算框架。Spark在2013年6月进入Apache成为孵化项目&#xff0c;8个月后成为Apache顶级项目&#xff0c;速度之快足见过人之处&…...

Python:操作 Excel 折叠

💖亲爱的技术爱好者们,热烈欢迎来到 Kant2048 的博客!我是 Thomas Kant,很开心能在CSDN上与你们相遇~💖 本博客的精华专栏: 【自动化测试】 【测试经验】 【人工智能】 【Python】 Python 操作 Excel 系列 读取单元格数据按行写入设置行高和列宽自动调整行高和列宽水平…...

Mybatis逆向工程,动态创建实体类、条件扩展类、Mapper接口、Mapper.xml映射文件

今天呢&#xff0c;博主的学习进度也是步入了Java Mybatis 框架&#xff0c;目前正在逐步杨帆旗航。 那么接下来就给大家出一期有关 Mybatis 逆向工程的教学&#xff0c;希望能对大家有所帮助&#xff0c;也特别欢迎大家指点不足之处&#xff0c;小生很乐意接受正确的建议&…...

Qt Widget类解析与代码注释

#include "widget.h" #include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget) {ui->setupUi(this); }Widget::~Widget() {delete ui; }//解释这串代码&#xff0c;写上注释 当然可以&#xff01;这段代码是 Qt …...

系统设计 --- MongoDB亿级数据查询优化策略

系统设计 --- MongoDB亿级数据查询分表策略 背景Solution --- 分表 背景 使用audit log实现Audi Trail功能 Audit Trail范围: 六个月数据量: 每秒5-7条audi log&#xff0c;共计7千万 – 1亿条数据需要实现全文检索按照时间倒序因为license问题&#xff0c;不能使用ELK只能使用…...

css的定位(position)详解:相对定位 绝对定位 固定定位

在 CSS 中&#xff0c;元素的定位通过 position 属性控制&#xff0c;共有 5 种定位模式&#xff1a;static&#xff08;静态定位&#xff09;、relative&#xff08;相对定位&#xff09;、absolute&#xff08;绝对定位&#xff09;、fixed&#xff08;固定定位&#xff09;和…...

【C语言练习】080. 使用C语言实现简单的数据库操作

080. 使用C语言实现简单的数据库操作 080. 使用C语言实现简单的数据库操作使用原生APIODBC接口第三方库ORM框架文件模拟1. 安装SQLite2. 示例代码:使用SQLite创建数据库、表和插入数据3. 编译和运行4. 示例运行输出:5. 注意事项6. 总结080. 使用C语言实现简单的数据库操作 在…...

Unity | AmplifyShaderEditor插件基础(第七集:平面波动shader)

目录 一、&#x1f44b;&#x1f3fb;前言 二、&#x1f608;sinx波动的基本原理 三、&#x1f608;波动起来 1.sinx节点介绍 2.vertexPosition 3.集成Vector3 a.节点Append b.连起来 4.波动起来 a.波动的原理 b.时间节点 c.sinx的处理 四、&#x1f30a;波动优化…...

中医有效性探讨

文章目录 西医是如何发展到以生物化学为药理基础的现代医学&#xff1f;传统医学奠基期&#xff08;远古 - 17 世纪&#xff09;近代医学转型期&#xff08;17 世纪 - 19 世纪末&#xff09;​现代医学成熟期&#xff08;20世纪至今&#xff09; 中医的源远流长和一脉相承远古至…...