Mabatis(CRUD)

Mybatis

CRUD(数据和配置使用Mybatis快速入门)

select标签

选择,查询语句:

<select id="getUserById" resultType="com.louis.pojo.User" parameterType="int"></select>

id:就是对应namespace中的方法名(就相当于重写了原来的方法)
resultType:sql语句执行的返回值
parameterType:表示参数类型,和传入的参数有关
namesepace中的包名要和Dao/mapper接口的包名一致。

UserDao接口

//根据id查询用户
User getUserById(Integer id);

UserMapper.xml

<select id="getUserById" resultType="com.louis.pojo.User" parameterType="int">select * from mybatis.user where id = #{id};
</select>

测试类

@Test
public void testUserById(){SqlSession sqlSession = MybatisUtils.getsqlSession();UserMapper mapper = sqlSession.getMapper(UserMapper.class);System.out.println(mapper.getUserById(1));sqlSession.close();
}

insert标签

UserDao接口

//insert一个用户
int addUser(User user);

UserMapper.xml

<!--对象中的属性可以直接取到-->
<insert id="addUser" parameterType="com.louis.pojo.User" >insert into mybatis.user (id, name, pwd) values (#{id}, #{name}, #{pwd});
</insert>

测试类

@Test
public void addUser(){SqlSession sqlSession = MybatisUtils.getsqlSession();UserMapper mapper = sqlSession.getMapper(UserMapper.class);mapper.addUser(new User(4, "DC", "12wa"));sqlSession.close();
}

通过上述方法我们可以执行成功,但在数据库中并没有插入相关的数据,这是事务导致。

在这里插入图片描述

解决方法

@Test
public void addUser(){SqlSession sqlSession = MybatisUtils.getsqlSession();UserMapper mapper = sqlSession.getMapper(UserMapper.class);int dc = mapper.addUser(new User(4, "DC", "12wa"));//解决数据库中未插入的情况,增删改需要提交事务if(dc > 0){System.out.println("插入成功");//提交sqlSession.commit();}sqlSession.close();
}

在这里插入图片描述

update标签

UserDao接口

//修改用户
int updateUser(User user);

UserMapper.xml

<update id="updateUser" parameterType="com.louis.pojo.User">update mybatis.user set name = #{name}, pwd = #{pwd} where id = #{id};
</update>

测试类

@Test
public void updateUser(){SqlSession sqlSession = MybatisUtils.getsqlSession();UserMapper userMapper = sqlSession.getMapper(UserMapper.class);int i = userMapper.updateUser(new User(3, "微微", "Lzh"));if(i > 0){sqlSession.commit();}sqlSession.close();
}

在这里插入图片描述

delete标签

UserDao接口

//删除用户
int deleteUser(int id);

UserMapper.xml

<delete id="deleteUser" parameterType="int">delete from mybatis.user where id = #{id};
</delete>

测试

@Test
public void deleteUser(){SqlSession sqlSession = MybatisUtils.getsqlSession();UserMapper userMapper = sqlSession.getMapper(UserMapper.class);int result = userMapper.deleteUser(4);if(result > 0){sqlSession.commit();}sqlSession.close();
}

在这里插入图片描述

增删改在没有设置自动提交的时候需要手动提交事务。

Map

假设我们的实体类或者数据库中的表字段或者参数过多,我们应当考虑使用map。
Map传递参数:直接在sql中取出key即可(parameterType=“map”)
对象传递参数:直接在sql中取对象的属性即可(parameterType=“com.louis.pojo.User”)
只有一个基本类型参数的情况下,可以直接在sql中取到。多个参数用Map,或者注解。

Map示例

UserDao接口

//Map
int addUser2(Map<String, Object> map);

UserMapper.xml

<insert id="addUser2" parameterType="map">insert into mybatis.user (id, name, pwd) values (#{userId}, #{userName}, #{password});
</insert>

测试类

@Test
public void addUser2(){SqlSession sqlSession = MybatisUtils.getsqlSession();UserMapper mapper = sqlSession.getMapper(UserMapper.class);Map<String, Object> map = new HashMap<>();map.put("userId", 5);map.put("userName", "咕噜咕噜");map.put("password", "xzz");int result = mapper.addUser2(map);if(result > 0){sqlSession.commit();}sqlSession.close();
}

在这里插入图片描述

模糊查询

UserDao接口

//模糊查询
List<User> selectUserLike(String value);

UserMapper.xml

<select id="selectUserLike">select * from mybatis.user where name like #{value};
</select>

测试类

@Test
public void selectUserLike(){SqlSession sqlSession = MybatisUtils.getsqlSession();UserMapper mapper = sqlSession.getMapper(UserMapper.class);List<User> users = mapper.selectUserLike("%微%");for (User user : users) {System.out.println(user);}sqlSession.close();
}

在这里插入图片描述

在这里插入图片描述

上面使用的方法存在sql注入的情况,优化如下:

UserMapper.xml

<select id="selectUserLike">select * from mybatis.user where name like "%" #{value}"%";
</select>

测试类

@Test
public void selectUserLike(){SqlSession sqlSession = MybatisUtils.getsqlSession();UserMapper mapper = sqlSession.getMapper(UserMapper.class);List<User> users = mapper.selectUserLike("微");for (User user : users) {System.out.println(user);}sqlSession.close();
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/4593.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【MATLAB第49期】基于MATLAB的深度学习ResNet-18网络不平衡图像数据分类识别模型

【MATLAB第49期】基于MATLAB的深度学习ResNet-18网络不平衡图像数据分类识别模型 一、基本介绍 这篇文章展示了如何使用不平衡训练数据集对图像进行分类&#xff0c;其中每个类的图像数量在类之间不同。两种最流行的解决方案是down-sampling降采样和over-sampling过采样。 在…

一元函数微分学中导数--定义--意义--基本公式--运算法则

目录 导数的定义 左导数和右导数 导数的几何意义和物理意义 几何意义 导数的几何意义--切线的斜率 物理意义 导数的物理意义——瞬时速度 基本初等函数导数公式 基本初等函数 常用基本初等函数导数公式 导数求解的四则运算法则 函数的求导法则 复合函数求导法则 导…

FullGC调优100倍,掌握这3招,吊打JVM调优

前言&#xff1a; 在40岁老架构师尼恩的读者社区&#xff08;50&#xff09;中&#xff0c;很多小伙伴拿不到offer&#xff0c;或者拿不到好的offer。 尼恩经常给大家 优化项目&#xff0c;优化简历&#xff0c;挖掘技术亮点。 在指导简历的过程中&#xff0c; 线上问题排查…

C++引用计数

文章目录 1. 什么是引用计数2. 引用计数的实现3. 示例代码 1. 什么是引用计数 引用计数&#xff08;reference count&#xff09;的核心思想是使用一个计数器来标识当前指针指向的对象被多少类的对象所使用&#xff08;即记录指针指向对象被引用的次数&#xff09;。它允许有多…

MySQL数据库——主从复制

目录 前言一、读写分离概述1. 什么是读写分离&#xff1f;2. 为什么要读写分离呢&#xff1f;3. 什么时候要读写分离&#xff1f;4. 主从复制与读写分离5. mysq支持的复制类型6. 主从复制的工作过程7. MySQL主从复制延迟 二、主从复制配置方法主服务器配置从服务器配置 前言 在…

MySQL的Join

1.Join用法 Join连接两张表,大致分为内连接,外连接,右连接,左连接,自然连接。 内连接又叫等值连接,此时的inner可以省略。 USING语句 MySQL中连接SQL语句中,ON子句的语法格式为:table1.column_name = table2.column_name。当模式设计对联接表的列采用了相同的命名样…

基于matlab使用校准相机拍摄的两张图像中恢复相机运动并重建场景的3D结构(附源码)

一、前言 运动结构 &#xff08;SfM&#xff09; 是从一组 3-D 图像估计场景的 2-D 结构的过程。此示例演示如何从两个图像估计校准相机的姿势&#xff0c;将场景的三维结构重建为未知比例因子&#xff0c;然后通过检测已知大小的对象来恢复实际比例因子。 此示例演示如何从使…

低代码平台在ERP软件中的作用

很多人认为 低代码开发平台的出现颠覆了传统的软件开发模式&#xff0c;对软件开发行业造成冲击&#xff0c;其实低代码开发平台的出现只是提高了软件开发的效率&#xff0c;并不是要颠覆软件开发的模式。低代码平台在erp软件开发中的作用还是比较明显的。下面一起来了解一下相…

《kafka 核心技术与实战》课程学习笔记(九)

客户端都有哪些不常见但是很高级的功能&#xff1f; 什么是 Kafka 拦截器&#xff1f; 拦截器基本思想就是允许应用程序在不修改逻辑的情况下&#xff0c;动态地实现一组可插拔的事件处理逻辑链。它能够在主业务操作的前后多个时间点上插入对应的“拦截”逻辑。Spring MVC 拦…

安科瑞故障电弧探测器在建筑电气的设计与应用

安科瑞 崔丽洁 【摘要】&#xff1a;电气设备是建筑中不可缺少的一部分&#xff0c;具有较为重要的作用和意义&#xff0c;在应用过程中不仅能够提升建筑本身实用性能&#xff0c;而且可为消费者提供更加优良的生活环境。但设备一旦在运行过程中出现故障&#xff0c;不仅会影响…

Flutter卡片分享功能实现:将你的内容分享给世界

前言 在app中&#xff0c;在实现分享功能的时候&#xff0c;通常会有一种以卡片形式展示和分享内容的分享方式。这种功能可以将信息以整洁、易读的方式呈现给用户&#xff0c;使他们能够快速了解内容的关键信息&#xff0c;并将其分享给其他人。那么在这篇文章中&#xff0c;就…

python: more Layer Architecture and its Implementation in Python

sql server: --学生表 DROP TABLE DuStudentList GO create table DuStudentList (StudentId INT IDENTITY(1,1) PRIMARY KEY,StudentName nvarchar(50),StudentNO varchar(50), --学号StudentBirthday datetime --学生生日 ) go mod…