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

【MyBtis】各种查询功能

目录

   【MyBtis】配置和映射

 11.1 示例:实现表数据的增、删、改、查

1.创建工程mybatis_DML demo

 2.创建数据库操作的工具类:DBOperatorMgr.java

3.创建映射接口

4.创建XML映射文件

5.测试


   【MyBtis】配置和映射

        MyBatis 的真正强大之外在于它的映射语句,这也是它的魅力所在。由于它的异常强大,映射器的XML文件就显得相对简单。如果将其与具有相同功能的 JDBC 代码进行对比,会立即发现其省掉了将近 95% 的代码。MyBatis 就是针对 SQL 构建的并且比普通的方法效果更好。        

      

        (1)实现表数据的增、删、改、查;

        (2)MyBatis主配置文件;

        (3)XML映射文件;

        (4)高级结果映射。

 11.1 示例:实现表数据的增、删、改、查

        1.创建工程mybatis_DML demo

        在 idea 中创建 Maven Project,如图11-1所示,“GroupID“ 选项中输入 “com.mialab” ,在 “ArtifactId” 选项输入“student”最终完成的 student 工程目录和 student 初始表数据如图11-1和图11-2所示。

        

                                                图11-1 mybatisDML_demo 工程目录结构

                                                        图11-2student表中的数据

        2.创建数据库操作的工具类:DBOperatorMgr.java

        在MyBatis 使用中,如果每个方法执行时都需要读取配置文件,并根据配置文件的信息构建SqlSessionFactory对象,然后创建SqlSession 对象,这会导致类大量的重复代码。为了简化开发,需要将重复代码封装到一个工具类中。如下:

public class DBOperatorMgr {static Logger logger = Logger.getLogger(DBOperatorMgr.class.getName());private static DBOperatorMgr dbMgr;private SqlSessionFactory sqlSessionFactory;private DBOperatorMgr() {String resource = "mybatis-config.xml";InputStream inputStream;try {inputStream = Resources.getResourceAsStream(resource);sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);} catch (Exception e) {logger.error(e.toString());}}public static DBOperatorMgr getInstance() {if (dbMgr == null) {dbMgr = new DBOperatorMgr();}return dbMgr;}public SqlSessionFactory getSqlSessionFactory() {return sqlSessionFactory;}	
}

        3.创建映射接口

        

public interface StudentMapper {public Student getStudent(String sno);public int addStudent(Student student);public List<Student> getSudentAll();public int updateStudent(Student student);public int deleteStudent(String sno);
}

        4.创建XML映射文件

        在包com.mialab.mybatis_DML_demomapper 中创建 StudentMapper.xml文件。

<mapper namespace="com.mialab.mybatis_DML_demo.mapper.StudentMapper"><select id="getStudent" resultType="com.mialab.mybatis_DML_demo.domain.Student">select * from student wheresno = #{sno}</select><insert id="addStudent" parameterType="student">insert intostudent(sno,name,sex,age,dept_no)values(#{sno},#{name},#{sex},#{age},#{dept_no})</insert><resultMap id="studentResultMap" type="student"><id property="sno" column="sno" /><result property="name" column="name" /><result property="sex" column="sex" /><result property="age" column="age" /><result property="dept_no" column="dept_no" /></resultMap><select id="getSudentAll" resultMap="studentResultMap">select * from student</select><update id="updateStudent" parameterType="student">update student set name= #{name}, sex = #{sex}, age = #{age}, dept_no = #{dept_no}where sno =#{sno}</update><delete id="deleteStudent" parameterType="String">delete from studentwhere sno = #{sno}</delete></mapper>

       

         5.测试

        如图11-1所示,在src/main/java 中创建 Package“com.mialab.mybatis_DML demo.main”。在此包中创建测试类DML_Mainjava,主要代码:

public class DML_Main {	public static void main(String[] args) {//testInsert();//testSelectAll();testSelect("20171509");//testUpdate();
//		testDelete("20171622");}	private static void testDelete(String sno) {Logger log = Logger.getLogger(DML_Main.class);SqlSession session = null;try {session = DBOperatorMgr.getInstance().getSqlSessionFactory().openSession();StudentMapper mapper = session.getMapper(StudentMapper.class);			mapper.deleteStudent(sno);			session.commit();} catch(Exception ex) {session.rollback();ex.printStackTrace();} finally {if (session != null) {session.close();}}		}private static void testSelectAll() {Logger log = Logger.getLogger(DML_Main.class);SqlSession session = null;try {session = DBOperatorMgr.getInstance().getSqlSessionFactory().openSession();StudentMapper mapper = session.getMapper(StudentMapper.class);List<Student> stu_list = mapper.getSudentAll();for(Student stu:stu_list) {//System.out.println(stu);log.info(stu);}} finally {if (session != null) {session.close();}}}private static void testInsert() {Logger log = Logger.getLogger(DML_Main.class);SqlSession session = null;try {session = DBOperatorMgr.getInstance().getSqlSessionFactory().openSession();StudentMapper mapper = session.getMapper(StudentMapper.class);Student student = new Student();student.setSno("20171622");student.setName("李白");student.setAge(88);student.setSex("男");student.setDept_no("2609");log.info(student);mapper.addStudent(student);session.commit();} catch(Exception ex) {session.rollback();ex.printStackTrace();} finally {if (session != null) {session.close();}}}private static void testSelect(String sno) {Logger log = Logger.getLogger(DML_Main.class);SqlSession session = null;try {session = DBOperatorMgr.getInstance().getSqlSessionFactory().openSession();StudentMapper mapper = session.getMapper(StudentMapper.class);Student student = mapper.getStudent(sno);//System.out.println(student);log.info(student);} finally {if (session != null) {session.close();}}		}private static void testUpdate() {Logger log = Logger.getLogger(DML_Main.class);SqlSession session = null;try {session = DBOperatorMgr.getInstance().getSqlSessionFactory().openSession();StudentMapper mapper = session.getMapper(StudentMapper.class);Student student = new Student();student.setSno("20171622");student.setName("苏东坡");student.setAge(68);student.setSex("女");student.setDept_no("2612");log.info(student);mapper.updateStudent(student);session.commit();} catch(Exception ex) {session.rollback();ex.printStackTrace();} finally {if (session != null) {session.close();}}}
}

        

        先测试 testSelectA11() 方法。如图 11-1 所示,右击comm.ialab.mybatis _DML_demo.main包中的DML_Main.java,在弹出的快捷菜单中选择“Run DML_Main.java”选项,在控制台中可以得到以下的结果:

        

 INFO [main] - Student [sno=20171508, name=李勇, sex=男, age=20, dept_no=2601]INFO [main] - Student [sno=20171509, name=刘娟, sex=女, age=19, dept_no=2602]INFO [main] - Student [sno=20171622, name=李白, sex=男, age=88, dept_no=2609]

        可分别对增、删、改、查的方法testInsert()、testUpdate()等进行测试。

相关文章:

【MyBtis】各种查询功能

目录 【MyBtis】配置和映射 11.1 示例:实现表数据的增、删、改、查 1.创建工程mybatis_DML demo 2.创建数据库操作的工具类&#xff1a;DBOperatorMgr.java 3.创建映射接口 4.创建XML映射文件 5.测试 【MyBtis】配置和映射 MyBatis 的真正强大之外在于它的映射语句&#xf…...

H5打包封装小程序系统开发

H5打包封装小程序系统开发 H5打包封装小程序系统开发是指将H5页面打包封装成小程序的开发过程。下面是一个简单的步骤&#xff1a; 准备工作&#xff1a;首先&#xff0c;需要准备好H5页面的代码和资源文件。确保H5页面在浏览器中正常运行&#xff0c;并且没有依赖于浏览器特…...

SpringBoot集成jasypt,加密yml配置文件

SpringBoot集成jasypt&#xff0c;加密yml配置文件 一、pom配置二、生成密文代码三、配置3.1、yml加密配置3.2、密文配置3.3、启动配置3.4、部署配置 四、遇到的一些坑 最新项目安全检测&#xff0c;发现配置文件中数据库密码&#xff0c;redis密码仍处理明文状态 一、pom配置…...

【C++】模板(初阶)

1、泛型编程 泛型编程&#xff1a;编写与类型无关的通用代码&#xff0c;是代码复用的一种手段。模板是泛型编程的基础 2、函数模板 函数模板代表了一个函数家族&#xff0c;该函数模板与类型无关&#xff0c;在使用时被参数化&#xff0c;根据实参类型产生函数的特定类型版本…...

windows下的txt文档,传到ubuntu后,每行后面出现^M,怎么处理?

问题背景&#xff1a;windows下pycharm生成的txt文档&#xff0c;传到ubuntu后&#xff0c;每行后面出现^M 用vim打开显示 使用cat -A filename显示如下 参考https://www.lmlphp.com/user/16697/article/item/579325/给出的几种方法 方法一、dos2unix filename。服务器没装…...

LabVIEW FPGA开发实时滑动摩擦系统

LabVIEW FPGA开发实时滑动摩擦系统 由于非线性摩擦效应的建模和补偿的固有困难&#xff0c;摩擦系统的运动控制已被广泛研究。最近&#xff0c;人们更加关注滑动动力学和滑动定位&#xff0c;作为传统机器人定位的低成本和更灵活的驱动替代方案。摩擦控制器设计和适当选择基础…...

Prometheus服务器、Prometheus被监控端、Grafana、Prometheus服务器、Prometheus被监控端、Grafana

day03 day03Prometheus概述部署Prometheus服务器环境说明&#xff1a;配置时间安装Prometheus服务器添加被监控端部署通用的监控exporterGrafana概述部署Grafana展示node1的监控信息监控MySQL数据库配置MySQL配置mysql exporter配置mysql exporter配置prometheus监控mysql自动…...

常见的锁策略(面试八股文)

1.乐观锁vs悲观锁 乐观锁&#xff1a;预测该场景中不太会出现锁冲突的情况。&#xff08;后续做的工作会更少&#xff09; 悲观锁&#xff1a;预测该场景非常容易出现锁冲突&#xff08;后续做的工作会更多&#xff09; 锁冲突&#xff1a;多个线程同时尝试去获得同一把锁&…...

SO_KEEPALIVE、TCP_KEEPIDLE、TCP_KEEPINTVL、保活包

SO_KEEPALIVE SO_KEEPALIVE 是一个套接字选项&#xff0c;用于设置是否启用 keepalive 机制。在这段代码中没有涉及到 SO_KEEPALIVE 选项的设置。 当 SO_KEEPALIVE 被设置为非零值时&#xff0c;表示启用 keepalive 机制。keepalive 是一种用于检测连接是否仍然有效的机制。通…...

【phaser微信抖音小游戏开发005】画布上添加图片

特别注意&#xff1a;真机模拟的时候&#xff0c;尽量使用网络图片资源&#xff0c;不要在小程序源文件里面使用图片&#xff0c;会出现真机加载不成功&#xff0c;小程序包体积过大的问题。我们学习过程中&#xff0c;只是作为演示使用。 推荐使用场景&#xff1a; 背景图片…...

【设计模式——学习笔记】23种设计模式——外观模式Facade(原理讲解+应用场景介绍+案例介绍+Java代码实现)

文章目录 案例引入介绍基本介绍类图出场角色 案例实现案例一类图代码实现 案例二类图代码实现 外观模式在Mybatis源码中的应用总结文章说明 案例引入 在家庭影院中&#xff0c;要享受一场电影&#xff0c;需要如下步骤&#xff1a; 直接用遥控器&#xff1a;统筹各设备开关开…...

消息队列 -提供上层服务接口

目录 前言封装数据库封装内存操作内存的设计思想 应答模式 代码实现测试代码 前言 我们之前已经将 数据库 的操作 和文件的操作 都完成了, 但是对于上层调用来说, 并不关心是于数据库中存储数据还是往文件中存储数据, 因此 我们提供一个类, 封装一下 上述俩个类中的操作, 并将…...

maven引入本地jar包的简单方式【IDEA】【SpringBoot】

前言 想必点进来看这篇文章的各位&#xff0c;都是已经习惯了Maven从中央仓库或者阿里仓库直接拉取jar包进行使用。我也是&#x1f921;&#x1f921;。 前两天遇到一个工作场景&#xff0c;对接三方平台&#xff0c;结果对方就是提供的一个jar包下载链接&#xff0c;可给我整…...

【爬虫逆向案例】某易云音乐(评论)js逆向—— params、encSecKey解密

声明&#xff1a;本文只作学习研究&#xff0c;禁止用于非法用途&#xff0c;否则后果自负&#xff0c;如有侵权&#xff0c;请告知删除&#xff0c;谢谢&#xff01; 【爬虫逆向案例】某易云音乐&#xff08;评论&#xff09;js逆向—— params、encSecKey解密 1、前言2、行动…...

【uni-app】【Android studio】手把手教你运行uniapp项目到Android App

运行到Android App基座 选择运行到Android App基座 选择运行项目 1、连接手机&#xff0c;在手机上选择 传输文件。 2、打开 设置-> 关于本机 -> 版本信息->连续点击4-5次版本号 &#xff0c;输入手机密码&#xff0c;系统就进入了开发者模式。 3、设置 > 其他设…...

多线程(JavaEE初阶系列6)

目录 前言&#xff1a; 1.什么是线程池 2.标准库中的线程池 3.实现线程池 结束语&#xff1a; 前言&#xff1a; 在上一节中小编带着大家了解了一下Java标准库中的定时器的使用方式并给大家实现了一下&#xff0c;那么这节中小编将分享一下多线程中的线程池。给大家讲解一…...

shell清理redis模糊匹配的多个key

#!/bin/bash# 定义Redis服务器地址和端口 REDIS_HOST"localhost" REDIS_PORT6380# 获取匹配键的数量 function get_matching_keys() {local key_pattern"$1"redis-cli -h $REDIS_HOST -p $REDIS_PORT -n 0 KEYS "$key_pattern" }# 删除匹配的键 …...

【电网异物检测硕士论文摘抄记录】电力巡检图像中基于深度学习的异物检测方法研究

根据国家电力行业发展报告统计&#xff0c;截止到 2018 年&#xff0c;全国电网 35 千伏及以上的输电线路回路长度达到 189 万千米&#xff0c;220 千伏及以上输电线路回路长度达73 万千米。截止到 2015年&#xff0c;根据国家电网公司的统计 330 千伏及以上输电线路故障跳闸总…...

C++共享数据的保护

虽然数据隐藏保护了数据的安全性&#xff0c;但各种形式的数据共享却又不同程度地破坏了数据的安全。因此&#xff0c;对于既需要共享有需要防止改变的数据应该声明为常量。因为常量在程序运行期间不可改变&#xff0c;所以可以有效保护数据。 1.常对象 常对象&#xff1a;它…...

MyBatisPlus学习记录

MyBatisPlus(简称MP&#xff09;是基于MyBatis框架基础上开发的增强型工具&#xff0c;旨在简化开发、提高效率 MyBatisPlus简介 入门案例 创建新模块&#xff0c;选择Spring初始化&#xff0c;并配置模块相关基础信息选择当前模块需要使用的技术集&#xff08;仅选择MySQL …...

如何开启一个java微服务工程

安装idea IDEA常用配置和插件&#xff08;包括导入导出&#xff09; https://blog.csdn.net/qq_38586496/article/details/109382560安装配置maven 导入source创建项目 修改项目编码utf-8 File->Settings->Editor->File Encodings 修改项目的jdk maven import引入…...

libhv之hio_t分析

上一篇文章解析了fd是怎么与io模型关联。其中最主要的角色扮演者&#xff1a;hio_t 1. hio_t与hloop的关系 fd的值即hio_t所在loop ios变量中所在的index值。 hio_t ios[fd] struct hloop_s { ...// ios: with fd as array.index//io_array保存了和hloop关联的所有hio_t&…...

C语言的转义字符

转义字符也叫转移序列&#xff0c;包含如下&#xff1a; 转移序列 \0oo 和 \xhh 是 ASCII 码的特殊表示。 八进制数示例&#xff1a; 代码&#xff1a; #include<stdio.h> int main(void) {char beep\007;printf("%c\n",beep);return 0; }结果&#xff1a; …...

【腾讯云 Cloud Studio 实战训练营】CloudStudio体验真正的现代化开发方式,双手插兜不知道什么叫对手!

CloudStudio体验真正的现代化开发方式&#xff0c;双手插兜不知道什么叫对手&#xff01; 文章目录 CloudStudio体验真正的现代化开发方式&#xff0c;双手插兜不知道什么叫对手&#xff01;前言出现的背景一、CloudStudio 是什么&#xff1f;二、CloudStudio 的特点三、CloudS…...

Pandas时序数据分析实践—时序数据集

1. 跑步运动为例&#xff0c;对运动进行时序分析 时序数据是指时间序列数据&#xff0c;是按照时间顺序排列的数据集合&#xff0c;每个数据点都与一个特定的时间戳相关联。在跑步活动中&#xff0c;我们可以将每次跑步的数据记录作为一个时序数据样本&#xff0c;每个样本都包…...

use strict 是什么意思?使用它区别是什么?

use strict 是什么意思&#xff1f;使用它区别是什么&#xff1f; use strict 代表开启严格模式&#xff0c;这种模式下使得 JavaScript 在更严格的条件下运行&#xff0c;实行更严格解析和错误处理。 开启“严格模式”的优点&#xff1a; 消除 JavaScript 语法的一些不合理…...

常见OOM异常分析排查

常见OOM异常分析排查 Java内存溢出Java堆溢出原因解决思路总结 Java内存溢出 java堆用于存储对象实例,如果不断地创建对象,并且保证GC Root到对象之间有可达路径,垃圾回收机制就不会清理这些对象,对象数量达到最大堆的容量限制后就会产生内存溢出异常. Java堆溢出原因 无法在…...

kubernetes网络之网络策略-Network Policies

Kubernetes 中&#xff0c;Network Policy&#xff08;网络策略&#xff09;定义了一组 Pod 是否允许相互通信&#xff0c;或者与网络中的其他端点 endpoint 通信。 NetworkPolicy 对象使用标签选择Pod&#xff0c;并定义规则指定选中的Pod可以执行什么样的网络通信&#xff0…...

交换机VLAN技术和实验(eNSP)

目录 一&#xff0c;交换机的演变 1.1&#xff0c;最小网络单元 1.2&#xff0c;中继器&#xff08;物理层&#xff09; 1.3&#xff0c;集线器&#xff08;物理层&#xff09; 1.4&#xff0c;网桥&#xff08;数据链路层&#xff09; 二&#xff0c;交换机的工作行为 2.…...

8.Winform界面打包成DLL提供给其他的项目使用

背景 希望集成一个Winform的框架&#xff0c;提供权限菜单&#xff0c;根据权限出现各个Winform子系统的菜单界面。不希望把所有的界面都放放在同一个解决方案下面。用各个子系统建立不同的解决方案&#xff0c;建立代码仓库&#xff0c;进行管理。 实现方式 将Winform的UI界…...