初学Mybatis之 CRUD 增删改查
namespace 中的包名要和 Dao/Mapper 接口的包名一致
select:选择,查询语句
同理,还有 insert、update、delete 标签
id:对应的 namespace 中的方法名
resultType:sql 语句执行的返回值
parameterType:参数类型
先在 mysql 建表(id、name、pwd)
配置 Maven 环境(mysql、mybatis、junit)
MybatisUtils.java:
package com.demo.utils;import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;import java.io.IOException;
import java.io.InputStream;public class MybatisUtils {private static SqlSessionFactory sqlSessionFactory;static {try {//获取sqlSessionFactory对象String resource = "mybatis-config.xml";InputStream inputStream = Resources.getResourceAsStream(resource);sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);} catch (IOException e) {e.printStackTrace();}}/*有了SqlSessionFactory,就可以获得SqlSession的实例SqlSession提供了在数据库执行SQL命令所需的所有方法可以通过SqlSession实例来执行已映射的SQL语句*/public static SqlSession getSqlSession(){return sqlSessionFactory.openSession();}
}
mybatis-config.xml:(resources 目录下)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"><!-- configuration核心配置文件 -->
<configuration><environments default="development"><environment id="development"><transactionManager type="JDBC"/><dataSource type="POOLED"><property name="driver" value="com.mysql.jdbc.Driver"/><property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false&useUnicode=true&characterEncoding=UTF-8"/><property name="username" value="root"/><property name="password" value="root"/></dataSource></environment></environments><!-- 每一个Mapper.xml都需要在Mybatis核心配置文件中注册 --><mappers><mapper resource="com/demo/dao/UserMapper.xml"/></mappers></configuration>
User.java:
package com.demo.pojo;
//实体类
public class User {private int id;private String name;private String pwd;//无参构造public User() {}//有参构造public User(int id, String name, String pwd) {this.id = id;this.name = name;this.pwd = pwd;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPwd() {return pwd;}public void setPwd(String pwd) {this.pwd = pwd;}@Overridepublic String toString() {return "User{" +"id=" + id +", name='" + name + '\'' +", pwd='" + pwd + '\'' +'}';}
}
UserMapper 接口:
package com.demo.dao;import com.demo.pojo.User;import java.util.List;public interface UserMapper {//查询全部用户List<User> getUserList();//根据ID查询用户User getUserById(int id);//insert插入用户int addUser(User user);//update修改用户int updateUser(User user);//delete删除用户int deleteUser(int id);
}
UserMapper.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.demo.dao.UserMapper"><!-- select 查询语句 --><select id="getUserList" resultType="com.demo.pojo.User">select * from mybatis.user</select><select id="getUserById" parameterType="int" resultType="com.demo.pojo.User">select * from mybatis.user where id = #{id}</select><!-- insert插入数据,对象中的属性可以直接取出来 --><insert id="addUser" parameterType="com.demo.pojo.User">insert into mybatis.user(id,name,pwd) values(#{id},#{name},#{pwd});</insert><!-- update更新数据 --><update id="updateUser" parameterType="com.demo.pojo.User">update mybatis.user set name = #{name}, pwd = #{pwd} where id = #{id};</update><!-- delete删除数据 --><delete id="deleteUser" parameterType="int">delete from mybatis.user where id = #{id};</delete></mapper>
UserDaoTest.java:(test 目录下)
package com.demo.dao;import com.demo.pojo.User;
import com.demo.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;import java.util.List;public class UserDaoTest {//查询全部@Testpublic void test(){//获得SqlSession对象SqlSession sqlSession = MybatisUtils.getSqlSession();UserMapper mapper = sqlSession.getMapper(UserMapper.class);List<User> userList = mapper.getUserList();for(User user : userList){System.out.println(user);}//关闭sqlSessionsqlSession.close();}//根据id查询@Testpublic void getUserById(){SqlSession sqlSession = MybatisUtils.getSqlSession();UserMapper mapper = sqlSession.getMapper(UserMapper.class);User user = mapper.getUserById(1);System.out.println(user);sqlSession.close();}//增删改需要提交事务//添加数据@Testpublic void addUser(){SqlSession sqlSession = MybatisUtils.getSqlSession();UserMapper mapper = sqlSession.getMapper(UserMapper.class);mapper.addUser(new User(4, "张三4", "123"));//提交事务sqlSession.commit();sqlSession.close();}//修改数据@Testpublic void updateUser(){SqlSession sqlSession = MybatisUtils.getSqlSession();UserMapper mapper = sqlSession.getMapper(UserMapper.class);mapper.updateUser(new User(4,"李四","111"));sqlSession.commit();sqlSession.close();}//删除数据@Testpublic void deleteUser(){SqlSession sqlSession = MybatisUtils.getSqlSession();UserMapper mapper = sqlSession.getMapper(UserMapper.class);mapper.deleteUser(4);sqlSession.commit();sqlSession.close();}
}
总结:
1.编写接口
2.编写对应的 Mapper 中的 sql 语句
3.测试
select:
<select id="getUserById" parameterType="int" resultType="com.demo.pojo.User">select * from mybatis.user where id = #{id}</select>
insert:
<insert id="addUser" parameterType="com.demo.pojo.User">insert into mybatis.user(id,name,pwd) values(#{id},#{name},#{pwd});</insert>
update:
<update id="updateUser" parameterType="com.demo.pojo.User">update mybatis.user set name = #{name}, pwd = #{pwd} where id = #{id};</update>
delete:
<delete id="deleteUser" parameterType="int">delete from mybatis.user where id = #{id};</delete>
注意:增删改需要提交事务
相关文章:
![](https://www.ngui.cc/images/no-images.jpg)
初学Mybatis之 CRUD 增删改查
namespace 中的包名要和 Dao/Mapper 接口的包名一致 select:选择,查询语句 同理,还有 insert、update、delete 标签 id:对应的 namespace 中的方法名 resultType:sql 语句执行的返回值 parameterType:…...
![](https://www.ngui.cc/images/no-images.jpg)
Kali Linux APT 设置指南:如何控制软件包更新行为
在我浏览 CSDN 的问答社区时,我发现一篇求助内容是一位用户对于如何在使用 APT 更新时避免更新 Arduino 这个问题感到困惑。这激发了我写这篇博客的灵感。我希望通过这篇文章,帮助那些在 Kali Linux 上使用 APT 管理软件包更新的朋友们,特别是…...
![](https://www.ngui.cc/images/no-images.jpg)
Android 10.0 Settings 加载流程
一、系统设置首页 代码路径:packages/app/Settings/ 1 主界面加载: <!-- Alias for launcher activity only, as this belongs to each profile. --><activity-alias android:name"Settings"android:label"string/settings_la…...
![](https://i-blog.csdnimg.cn/direct/6b9f8116b17a4068999f468cca10a11d.png)
mysql的索引、事务和存储引擎
目录 索引 索引的概念 索引的作用 作用 索引的副作用 创建索引 创建索引的原则和依据 索引的类型 创建索引 查看索引 删除索引 drop 主键索引 普通索引 添加普通索引 唯一索引 添加唯一索引 组合索引 添加组合索引 查询组合索引 全文索引 添加全文索引 …...
![](https://www.ngui.cc/images/no-images.jpg)
基于trace_id实现SpringCloudGateway网关的链路追踪
之前写的两篇关于基于 trace_id 的链路追踪的文章: 基于trace_id的链路追踪(含Feign、Hystrix、线程池等场景)基于trace_id的链路追踪(ForkJoinPool场景) 一、引言 在之前的文章中,我们讨论了基于 trace…...
![](https://www.ngui.cc/images/no-images.jpg)
Windows 11 version 22H2 中文版、英文版 (x64、ARM64) 下载 (updated Jul 2024)
Windows 11 version 22H2 中文版、英文版 (x64、ARM64) 下载 (updated Jul 2024) Windows 11, version 22H2,企业版 arm64 x64 请访问原文链接:https://sysin.org/blog/windows-11/,查看最新版。原创作品,转载请保留出处。 作者…...
![](https://i-blog.csdnimg.cn/direct/07775b7bde2f45f988a2650f4be1c691.png)
【C语言】动态内存管理(上)
文章目录 前言1.为什么要存在动态内存2. malloc和free2.1 malloc2.2 free2.3 使用实例(malloc和free) 3. calloc3.1 calloc例子 前言 本文开始将开始学习C语言中一个比较重要的知识点或者是操作——动态内存管理。由于本次的知识比较重要,为…...
![](https://img-blog.csdnimg.cn/direct/df413fc3bbea46f7962bc7fe31fa6a01.png)
【BUG】已解决:ModuleNotFoundError: No module named‘ pip‘
已解决:ModuleNotFoundError: No module named‘ pip‘ 目录 已解决:ModuleNotFoundError: No module named‘ pip‘ 【常见模块错误】 【解决方案】 欢迎来到英杰社区https://bbs.csdn.net/topics/617804998 欢迎来到我的主页,我是博主英杰…...
![](https://www.ngui.cc/images/no-images.jpg)
网络安全-网络安全及其防护措施11
51.网络容量规划 网络容量规划的概念和重要性 网络容量规划: 是指根据业务需求和预期增长,合理规划和设计网络的带宽、设备和资源,以满足未来网络流量和服务质量的需求。通过有效的网络容量规划,确保网络性能稳定和用户体验良好…...
![](https://i-blog.csdnimg.cn/direct/f4e261a82f024069bd52482e3d468e02.png)
使用IDEA编写lua脚本并运行
下载lua https://github.com/rjpcomputing/luaforwindows/releases 是否创建桌面快捷方式:我们的目标是使用IDEA编写lua脚本,所以不需要勾选。后面需要的话,可以到安装目录下手动创建快捷方式 环境变量自动配置 安装后会自动配置好环境变量…...
![](https://i-blog.csdnimg.cn/direct/addbf989194241f6b801319ef1f5f0f9.png)
CentOS 7 安装MySQL 5.7.30
CentOS 7 安装MySQL卸载(离线安装) 安装配置MySQL之前先查询是否存在,如存在先卸载再安装 rpm -qa|grep -i mysql rpm -qa|grep -i mariadb rpm -e --nodeps mariadb-libs-5.5.68-1.el7.x86_64如下命令找到直接 rm -rf 删除(删除…...
![](https://img-blog.csdnimg.cn/fa80b15381a8400ca124d37ae57376ff.png)
Bash 学习摘录
文章目录 1、变量和参数的介绍(1)变量替换$(...) (2)特殊的变量类型export位置参数shift 2、引用(1)引用变量(2)转义 3、条件判断(1)条件测试结构(…...
![](https://i-blog.csdnimg.cn/direct/621cafe3056f49aa8155817bf79038f3.png)
GD32 MCU是如何进入中断函数的
用过GD32 MCU的小伙伴们都知道,程序是顺序执行的,但当有中断来的时候程序会跳转到中断函数,执行完中断函数后程序又继续回到原来的位置继续执行,那么你们知道MCU是如何找到中断函数入口的吗? 今天我们就以GD32F303系列…...
![](https://www.ngui.cc/images/no-images.jpg)
Ruby 循环
Ruby 循环 在编程中,循环是一种常用的控制结构,它允许我们重复执行一段代码多次。Ruby 作为一种灵活的编程语言,提供了多种循环方法,包括 while、until、for、each 和 loop 等。本文将详细介绍 Ruby 中的循环机制,并通…...
![](https://i-blog.csdnimg.cn/direct/2cae243d3c8f4b198bd1594165da8012.png)
三字棋游戏(C语言详细解释)
hello,小伙伴们大家好,算是失踪人口回归了哈,主要原因是期末考试完学校组织实训,做了俄罗斯方块,后续也会更新,不过今天先从简单的三字棋说起 话不多说,开始今天的内容 一、大体思路 我们都知…...
![](https://i-blog.csdnimg.cn/direct/21275bdb558e48bfb94b02929009515e.png)
H3CNE(计算机网络的概述)
1. 计算机网络的概述 1.1 计算机网络的三大基本功能 1. 资源共享 2. 分布式处理与负载均衡 3. 综合信息服务 1.2 计算机网络的三大基本类型 1.3 网络拓扑 定义: 网络设备连接排列的方式 网络拓扑的类型: 总线型拓扑: 所有的设备共享一…...
![](https://www.ngui.cc/images/no-images.jpg)
【极客日常】Golang一个的slice数据替换的bug排查
上周某天下班前,接到同事转来一个bug要排查,症状是代码重构之后某些业务效果不符合预期,由于代码重构人是笔者,于是blame到笔者这边。经过10min左右的排查和尝试后,解决了这个问题:既往逻辑没有改动&#x…...
![](https://i-blog.csdnimg.cn/direct/b261d216a196426788ebb3acbfda73ac.jpeg#pic_center)
HarmonyOS应用开发者高级认证,Next版本发布后最新题库 - 单选题序号3
基础认证题库请移步:HarmonyOS应用开发者基础认证题库 注:有读者反馈,题库的代码块比较多,打开文章时会卡死。所以笔者将题库拆分,单选题20个为一组,多选题10个为一组,题库目录如下,…...
![](https://i-blog.csdnimg.cn/direct/e3e4176119874666a41a7675ed24a518.png)
UE4-光照重建
当我们拉入新的光源和模型到我们的场景中后,会产生这样的情况: Preview:预览 表示此时由于光照物体所产生的阴影都是预览级别的并不是真正的效果。 方法一: 或者也可以在世界大纲中选中我们的光源,然后将我们的光源改变为可以…...
![](https://www.ngui.cc/images/no-images.jpg)
【2024德国签证】留学面签问题汇总
在去交材料的时候,可能会被随机安排面试。这些面试问题一般都很简单,主要是测试你的基本英文交流能力。无需担心,签证官不会问太专业的问题,因为他们也不懂专业内容。到目前为止,没有一个博士生因为这个面试被拒签。毕…...
![](https://img-blog.csdnimg.cn/201d4553cbce4376b2d0e55fab70c0a5.png)
知识点大纲
学习方法 学习、整理笔记过程中,顺便整理出一个以问题为模版的大纲,到时候对着问题,就像是在和面试官讲解那样,相当于升级版的费曼学习法 除了看博客,问gpt外,亲自实验也是获取知识及加深印象的关键点 很…...
![](https://i-blog.csdnimg.cn/direct/fdaa7086e030402688db881eb663c993.png)
MySQL:库表操作
MySQL:库表操作 库操作查看创建字符编码集 删除修改备份 表操作创建查看删除修改 库操作 查看 查看存在哪些数据库: show databases;示例: 查看自己当前处于哪一个数据库: select database();示例: 此处由于我不处于任…...
![](https://www.ngui.cc/images/no-images.jpg)
8.3 End-to-end Data Protection (Optional)
8.3 End-to-end Data Protection (Optional) 为了提供从应用程序到NVM介质并返回到应用程序本身的稳健数据保护,可以使用端到端数据保护。如果启用了此可选机制,则将额外的保护信息(例如CRC)添加到逻辑块中,控制器和/或主机软件可以对其进行评估,以确定逻辑块的完整性。…...
![](https://i-blog.csdnimg.cn/direct/320f738b8bd4471da2c38882db50f00c.png)
python实现图像对比度增强算法
python实现直方图均衡化、自适应直方图均衡化、连接组件标记算法 1.直方图均衡化算法详解算法步骤公式Python 实现详细解释优缺点 2.自适应直方图均衡化算法详解算法步骤公式Python 实现详细解释优缺点 3.连接组件标记算法详解算法步骤8连通与4连通公式Python 实现详细解释优缺…...
![](https://i-blog.csdnimg.cn/direct/eacf8c9a58a74a99b057b99e7c7bea04.png#pic_center)
【D3.js in Action 3 精译_020】2.6 用 D3 设置与修改元素样式 + 名人专访(Nadieh Bremer)+ 2.7 本章小结
当前内容所在位置 第一部分 D3.js 基础知识 第一章 D3.js 简介(已完结) 1.1 何为 D3.js?1.2 D3 生态系统——入门须知1.3 数据可视化最佳实践(上)1.3 数据可视化最佳实践(下)1.4 本章小结 第二章…...
![](https://i-blog.csdnimg.cn/direct/6c57a0f7f55f4baa84083f523709f1e9.png)
GIT命令学习 二
📑打牌 : da pai ge的个人主页 🌤️个人专栏 : da pai ge的博客专栏 ☁️宝剑锋从磨砺出,梅花香自苦寒来 ☁️运维工程师的职责:监…...
![](https://www.ngui.cc/images/no-images.jpg)
LeetCode 150, 112, 130
文章目录 150. 逆波兰表达式求值题目链接标签思路代码 112. 路径总和题目链接标签思路代码 130. 被围绕的区域题目链接标签思路代码 150. 逆波兰表达式求值 题目链接 150. 逆波兰表达式求值 标签 栈 数组 数学 思路 本题很像 JVM 中的 操作数栈,当写出以下三行…...
![](https://www.ngui.cc/images/no-images.jpg)
c++应用网络编程之五Windows常用的网络IO模型
一、Windows的网络编程 其实对开发者而言,只有Windows和其它平台。做为一种普遍流行的图形OS,其一定会与类Linux的编程有着明显的区别,这点当然也会体现在网络编程上。Windows有着自己一套相对独立的上层Socket编程模型或者说框架࿰…...
![](https://i-blog.csdnimg.cn/direct/e2a5dff72bcf4242b473e5bbd0ab7134.png)
PostgreSQL 中如何解决因大量并发删除和插入操作导致的索引抖动?
🍅关注博主🎗️ 带你畅游技术世界,不错过每一次成长机会!📚领书:PostgreSQL 入门到精通.pdf 文章目录 PostgreSQL 中如何解决因大量并发删除和插入操作导致的索引抖动一、理解索引抖动二、索引抖动的影响三…...
![](https://i-blog.csdnimg.cn/direct/b165f88ee3ef48cf86d938223f1b0ec2.jpeg)
鑫创SSS1700USB音频桥芯片USB转IIS芯片
鑫创SSS1700支持IIC初始外部编(EEPROM选项),两线串行总线(I2C总线)用于外部MCU控制整个EEPROM空间可以通过MCU访问用于主机控制同步的USB HID外部串行EEPROM(24C02~24C16)接口,用于客户特定的USB视频、PID、…...
![](/images/no-images.jpg)
格尔木市住建和城乡建设局网站/佛山百度seo点击软件
正在学前台,出现了vertical-align: middle 这个属性怎么都不起作用的情况,解决过程如下: 刚开始是这样: .table_yht{ text-align: center; vertical-align: middle; } 发现文字左右的确居中,但是上下不能居中ÿ…...
![](/images/no-images.jpg)
织梦开发网站/网站设计培训
开始学习tensorflow了,张量是tensorflow最基础的概念,我发现自己还不会。学习的视频中,老师也没讲到,只是一带而过,刚刚参考了几篇博客,对张量大概有个了解,但是里面的数学用语还是不懂…...
![](https://common.cnblogs.com/images/copycode.gif)
160;作者:网站建设&/潍坊百度网站排名
平时我们如果要用到委托一般都是先声明一个委托类型,比如: private delegate string Say(); string说明适用于这个委托的方法的返回类型是string类型,委托名Say后面没有参数,说明对应的方法也就没有传入参数。 写一个适用于该委托…...
![](/images/no-images.jpg)
独立站建站东莞/成都网络推广中联无限
GetModuleHandle功能说明 获取一个应用程序或动态链接库的模块句柄 (前提是:只有欲获取的模块已映射到调用该函数的进程内,才会正确得到模块句柄。常用模块映射函数:LoadLibrary(..)。)HMODULE GetModuleHandle ( L…...
![](https://img-blog.csdnimg.cn/img_convert/b7d19e41ac5ed5dedfa0fa88ae5dfd73.png)
改图网在线制作图片/网站seo主要是做什么的
来源:数据学堂本文来源于网络,如有侵权,联系浪尖删除:langjianliaodashuju全文共7596个字,建议阅读12分钟由于在变化快速的商业世界里,业务形态多种多样,为了能够更有针对性的进行数据建模&…...
![](https://img-blog.csdnimg.cn/20201205190407895.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0xJTlpFWVU2NjY=,size_16,color_FFFFFF,t_70#pic_center)
住房和城乡建设厅网站青海省/今日军事新闻头条
typedef int ElemType; typedef struct Node { ElemType data; struct Node *prev; struct Node *next; }RingLink;以上是定义结构体。 以下是双向循环链表的实现 void RingLinkInit(RingLink *head) //初始化 { if (head NULL) exit(0); head->next head->prev …...