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

Linux应用开发之网络套接字编程(实例篇)

服务端与客户端单连接 服务端代码 #include <sys/socket.h> #include <sys/types.h> #include <netinet/in.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <arpa/inet.h> #include <pthread.h> …...

《基于Apache Flink的流处理》笔记

思维导图 1-3 章 4-7章 8-11 章 参考资料 源码&#xff1a; https://github.com/streaming-with-flink 博客 https://flink.apache.org/bloghttps://www.ververica.com/blog 聚会及会议 https://flink-forward.orghttps://www.meetup.com/topics/apache-flink https://n…...

Fabric V2.5 通用溯源系统——增加图片上传与下载功能

fabric-trace项目在发布一年后,部署量已突破1000次,为支持更多场景,现新增支持图片信息上链,本文对图片上传、下载功能代码进行梳理,包含智能合约、后端、前端部分。 一、智能合约修改 为了增加图片信息上链溯源,需要对底层数据结构进行修改,在此对智能合约中的农产品数…...

推荐 github 项目:GeminiImageApp(图片生成方向,可以做一定的素材)

推荐 github 项目:GeminiImageApp(图片生成方向&#xff0c;可以做一定的素材) 这个项目能干嘛? 使用 gemini 2.0 的 api 和 google 其他的 api 来做衍生处理 简化和优化了文生图和图生图的行为(我的最主要) 并且有一些目标检测和切割(我用不到) 视频和 imagefx 因为没 a…...

【电力电子】基于STM32F103C8T6单片机双极性SPWM逆变(硬件篇)

本项目是基于 STM32F103C8T6 微控制器的 SPWM(正弦脉宽调制)电源模块,能够生成可调频率和幅值的正弦波交流电源输出。该项目适用于逆变器、UPS电源、变频器等应用场景。 供电电源 输入电压采集 上图为本设计的电源电路,图中 D1 为二极管, 其目的是防止正负极电源反接, …...

Golang——6、指针和结构体

指针和结构体 1、指针1.1、指针地址和指针类型1.2、指针取值1.3、new和make 2、结构体2.1、type关键字的使用2.2、结构体的定义和初始化2.3、结构体方法和接收者2.4、给任意类型添加方法2.5、结构体的匿名字段2.6、嵌套结构体2.7、嵌套匿名结构体2.8、结构体的继承 3、结构体与…...

在 Spring Boot 中使用 JSP

jsp&#xff1f; 好多年没用了。重新整一下 还费了点时间&#xff0c;记录一下。 项目结构&#xff1a; pom: <?xml version"1.0" encoding"UTF-8"?> <project xmlns"http://maven.apache.org/POM/4.0.0" xmlns:xsi"http://ww…...

永磁同步电机无速度算法--基于卡尔曼滤波器的滑模观测器

一、原理介绍 传统滑模观测器采用如下结构&#xff1a; 传统SMO中LPF会带来相位延迟和幅值衰减&#xff0c;并且需要额外的相位补偿。 采用扩展卡尔曼滤波器代替常用低通滤波器(LPF)&#xff0c;可以去除高次谐波&#xff0c;并且不用相位补偿就可以获得一个误差较小的转子位…...

小智AI+MCP

什么是小智AI和MCP 如果还不清楚的先看往期文章 手搓小智AI聊天机器人 MCP 深度解析&#xff1a;AI 的USB接口 如何使用小智MCP 1.刷支持mcp的小智固件 2.下载官方MCP的示例代码 Github&#xff1a;https://github.com/78/mcp-calculator 安这个步骤执行 其中MCP_ENDPOI…...

写一个shell脚本,把局域网内,把能ping通的IP和不能ping通的IP分类,并保存到两个文本文件里

写一个shell脚本&#xff0c;把局域网内&#xff0c;把能ping通的IP和不能ping通的IP分类&#xff0c;并保存到两个文本文件里 脚本1 #!/bin/bash #定义变量 ip10.1.1 #循环去ping主机的IP for ((i1;i<10;i)) doping -c1 $ip.$i &>/dev/null[ $? -eq 0 ] &&am…...