spring 集成 mybatis
spring 集成 mybatis
- 1、spring对junit的支持
- 1.1、对junit4的支持
- 1.2 对junit5的支持
- 2、Spring6集成MyBatis3.5
- 2.1 实现步骤
- 2.2 实现
1、spring对junit的支持
1.1、对junit4的支持
依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.powernode</groupId><artifactId>spring6-015-junit</artifactId><version>1.0-SNAPSHOT</version><packaging>jar</packaging><!--仓库--><repositories><!--spring里程碑版本的仓库--><repository><id>repository.spring.milestone</id><name>Spring Milestone Repository</name><url>https://repo.spring.io/milestone</url></repository></repositories><dependencies><!--spring context依赖--><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>6.0.0-M2</version></dependency><!--spring对junit的支持相关依赖--><dependency><groupId>org.springframework</groupId>既支持junit4也支持5<artifactId>spring-test</artifactId><version>6.0.0-M2</version></dependency><!--junit4依赖--> 记得要4.12及以上<dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.13.2</version><scope>test</scope></dependency></dependencies><properties><maven.compiler.source>17</maven.compiler.source><maven.compiler.target>17</maven.compiler.target></properties></project>
package com.cky.pojo;import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;@Component
public class User {@Value("cky")private String name;public String getName() {return name;}public void setName(String name) {this.name = name;}@Overridepublic String toString() {return "User{" +"name='" + name + '\'' +'}';}public User(String name) {this.name = name;}public User() {}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><context:component-scan base-package="com.cky.pojo"/>
</beans>
package com.cky.test;import com.cky.pojo.User;
import jakarta.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring.xml")
public class springjunit4 {@Autowiredprivate User user;@Testpublic void test(){System.out.println(user.getName());}
}

1.2 对junit5的支持
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.powernode</groupId><artifactId>spring6-015-junit</artifactId><version>1.0-SNAPSHOT</version><packaging>jar</packaging><!--仓库--><repositories><!--spring里程碑版本的仓库--><repository><id>repository.spring.milestone</id><name>Spring Milestone Repository</name><url>https://repo.spring.io/milestone</url></repository></repositories><dependencies><!--spring context依赖--><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>6.0.0-M2</version></dependency><!--spring对junit的支持相关依赖--><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>6.0.0-M2</version></dependency><!--junit5依赖--><dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter</artifactId><version>5.9.0</version><scope>test</scope></dependency></dependencies><properties><maven.compiler.source>17</maven.compiler.source><maven.compiler.target>17</maven.compiler.target></properties></project>
package com.cky.test;import com.cky.pojo.User;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
@ExtendWith(SpringExtension.class)
@ContextConfiguration("classpath:spring1.xml")
public class springjunit5 {@Autowiredprivate User user;@Testpublic void testUser(){System.out.println(user.getName());}
}

2、Spring6集成MyBatis3.5
2.1 实现步骤
● 第一步:准备数据库表
○ 使用t_act表(账户表)
● 第二步:IDEA中创建一个模块,并引入依赖
○ spring-context
○ spring-jdbc
○ mysql驱动
○ mybatis
○ mybatis-spring:mybatis提供的与spring框架集成的依赖
○ 德鲁伊连接池
○ junit
● 第三步:基于三层架构实现,所以提前创建好所有的包
○ com.powernode.bank.mapper
○ com.powernode.bank.service
○ com.powernode.bank.service.impl
○ com.powernode.bank.pojo
● 第四步:编写pojo
○ Account,属性私有化,提供公开的setter getter和toString。
● 第五步:编写mapper接口
○ AccountMapper接口,定义方法
● 第六步:编写mapper配置文件
○ 在配置文件中配置命名空间,以及每一个方法对应的sql。
● 第七步:编写service接口和service接口实现类
○ AccountService
○ AccountServiceImpl
● 第八步:编写jdbc.properties配置文件
○ 数据库连接池相关信息
● 第九步:编写mybatis-config.xml配置文件
○ 该文件可以没有,大部分的配置可以转移到spring配置文件中。
○ 如果遇到mybatis相关的系统级配置,还是需要这个文件。
● 第十步:编写spring.xml配置文件
○ 组件扫描
○ 引入外部的属性文件
○ 数据源
○ SqlSessionFactoryBean配置
■ 注入mybatis核心配置文件路径
■ 指定别名包
■ 注入数据源
○ Mapper扫描配置器
■ 指定扫描的包
○ 事务管理器DataSourceTransactionManager
■ 注入数据源
○ 启用事务注解
■ 注入事务管理器
● 第十一步:编写测试程序,并添加事务,进行测试
2.2 实现

具体的不在这里写了
主要写一下配置文件
mybatis-config.xml
<?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>
<settings>
<!-- 开启mybaits日志输出 这里是标准控制台输出 有助于看到sql语句--><setting name="logImpl" value="STDOUT_LOGGING"/>
</settings><!--其他都可以放在spring中写-->
</configuration>
spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"><!--包扫描--><context:component-scan base-package="com.cky"></context:component-scan><!--外部属性配置文件--><context:property-placeholder location="jdbc.properties" /><!--数据源--><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"><property name="driverClassName" value="${jdbc.driver}"/><property name="url" value="${jdbc.url}"/><property name="username" value="${jdbc.name}"/><property name="password" value="${jdbc.pwd}"/></bean>
<!-- sqlsessionfactorybean--><bean class="org.mybatis.spring.SqlSessionFactoryBean"><!--mybatis核心配置文件路径--><property name="configLocation" value="mybatis-config.xml"/><!--注入数据源--><property name="dataSource" ref="dataSource"/><!--起别名--><property name="typeAliasesPackage" value="com.cky.pojo"/></bean><!--Mapper扫描器--><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="com.cky.mapper"/></bean><!--事务管理器--><bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"/></bean><!--开启事务注解--><tx:annotation-driven transaction-manager="txManager"></tx:annotation-driven></beans>
jabc.properties
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.name=ckytest
jdbc.pwd=123456
pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.example</groupId><artifactId>demo</artifactId><version>1.0-SNAPSHOT</version><properties><maven.compiler.source>17</maven.compiler.source><maven.compiler.target>17</maven.compiler.target></properties><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>6.1.5</version></dependency><!--log4j2的依赖--><dependency><groupId>org.apache.logging.log4j</groupId><artifactId>log4j-core</artifactId><version>2.19.0</version></dependency><dependency><groupId>org.apache.logging.log4j</groupId><artifactId>log4j-slf4j2-impl</artifactId><version>2.19.0</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.13.2</version><scope>test</scope></dependency></dependencies></project>
测试
package com.cky.test;import com.cky.service.AccountService;
import com.cky.service.impl.AccountServiceimpl;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class test {@Testpublic void test1(){ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring.xml");AccountService accountservice = applicationContext.getBean("accountservice", AccountService.class);accountservice.transfer("1001","1022",10000.0);}
}
注意:由于service 有接口 所以这里必须用service 对象 不能用实现类对象,因为有接口,底层用cglib来实现动态代理。

相关文章:
spring 集成 mybatis
spring 集成 mybatis 1、spring对junit的支持1.1、对junit4的支持1.2 对junit5的支持 2、Spring6集成MyBatis3.52.1 实现步骤2.2 实现 1、spring对junit的支持 1.1、对junit4的支持 依赖 <?xml version"1.0" encoding"UTF-8"?> <project xml…...
rtpengine 的端点学习模式
端点学习模式(endpoint-learning) delayed|immediate|off|heuristic delayed 延迟模式,等待 3 秒钟,然后再提交到端点地址 immediate 立即模式,收到第一个 rtp 包之后立即学习,不等 3 秒 off 关闭模式…...
Windows 安装 A UDP/TCP Assistant 网络调试助手
Windows 安装 A UDP/TCP Assistant 网络调试助手 0. 引言1. 下载地址2. 安装和使用 0. 引言 需要调试一个实时在线聊天程序,安装一个UDP/TCP Assistant 网络调试助手,方便调试。 1. 下载地址 https://github.com/busyluo/NetAssistant/releases 2. 安…...
web自动化系列-selenium的3种等待方式(十一)
在ui自动化测试中,几乎出现问题最多的情况就是定位不到元素 ,当你的自动化在运行过程中 ,突然发现报错走不下去了 。很大概率就是因为找不到元素 ,而找不到元素的一个主要原因就是页面加载慢 ,代码运行速度快导致 。 …...
每日OJ题_完全背包④_力扣279. 完全平方数(一维和二维)
目录 力扣279. 完全平方数 问题解析 解析代码 优化代码(相同子问题分析和滚动数组) 力扣279. 完全平方数 279. 完全平方数 难度 中等 给你一个整数 n ,返回 和为 n 的完全平方数的最少数量 。 完全平方数 是一个整数,其值…...
web项目中jsp页面不识别el表达式
如果使用el表达式出现下图问题 ** 解决办法 ** 这是因为maven创建项目时,web.xml头部声明默认是2.3,这个默认jsp关闭el表达式 修改web.xml文件开头的web-app的版本 <?xml version"1.0" encoding"UTF-8"?> <web-app x…...
【Python基础】字典
文章目录 [toc]什么是字典键值对示例键异常 遍历列表什么是遍历遍历字典的键keys()方法 遍历字典的值values()方法 遍历字典的键值对items()方法 字典操作增加键值对修改键值对查询键值对get()方法 删除键值对delclear()方法 个人主页:丷从心 系列专栏:…...
2024HW --> 安全产品 Powershell无文件落地攻击
在HW中,除了了解中间件,web漏洞,这些攻击的手法,还得了解应急响应,安全产品,入侵排查,溯源反制...... 那么今天,就来说一下安全产品(安全公司我就不说了,这个…...
力扣哈哈哈哈
public class MyStack {int top;Queue<Integer> q1;Queue<Integer> q2;public MyStack() {q1new LinkedList<Integer>();q2new LinkedList<Integer>();}public void push(int x) {q2.offer(x);//offer是入队方法while (!q1.isEmpty()){q2.offer(q1.pol…...
RUM 最佳实践-视觉稳定性的探索与实践
写在前面的话 在当今数字时代,网页的视觉稳定性对于提供良好的用户体验至关重要。其中一个衡量视觉稳定性的关键指标就是累积布局偏移(Cumulative Layout Shift,简称 CLS)。CLS 作为 Web Vitals 指标之一,它衡量的是网…...
PostgreSQL的学习心得和知识总结(一百三十八)|深入理解PostgreSQL数据库之Protocol message构造和解析逻辑
目录结构 注:提前言明 本文借鉴了以下博主、书籍或网站的内容,其列表如下: 1、参考书籍:《PostgreSQL数据库内核分析》 2、参考书籍:《数据库事务处理的艺术:事务管理与并发控制》 3、PostgreSQL数据库仓库…...
爬虫开发教程
一、爬虫概述 爬虫(也称为网络爬虫或蜘蛛)是一种自动化程序,能够模拟人类在互联网上浏览和抓取数据的行为。它通过发送HTTP请求,获取网页的HTML代码,然后解析这些代码以提取有用的数据。爬虫在数据分析、价格监测、竞…...
【Python】高级进阶(专版提升3)
Python 1 程序结构1.1 模块 Module1.1.1 定义1.1.2 作用1.1.3 导入1.1.3.1 import1.1.3.2 from import 1.1.4 模块变量1.1.5 加载过程1.1.6 分类 1.2 包package1.2.1 定义1.2.2 作用1.2.3 导入1.1.3.1 import1.1.3.2 from import 2 异常处理Error2.1 异常2.2 处理 3 迭代3.1 可…...
LeetCode 1378、1277、2944
1378 二级排序,compare函数必须是static的 class Solution { public:struct node {int val;int priority;};static bool compare(const node &n1, const node &n2) {if (n1.priority n2.priority) {return n1.val < n2.val;}return n1.priority < n…...
【缓存常见问题】
在使用缓存时特别是在高并发场景下会遇到很多问题,常用的问题有缓存穿透、缓存击穿、缓存雪崩以及缓存一致性问题。 1、缓存穿透 首先,什么是缓存穿透呢? 缓存穿透是指请求一个不存在的数据,缓存层和数据库层都没有这个数据&…...
Python爬取猫眼电影票房 + 数据可视化
目录 主角查看与分析 爬取可视化分析猫眼电影上座率前10分析猫眼电影票房场均人次前10分析猫眼电影票票房占比分析 主角查看与分析 爬取 对猫眼电影票房进行爬取,首先我们打开猫眼 接着我们想要进行数据抓包,就要看网站的具体内容,通过按F12…...
Spring Boot深度解析:是什么、为何使用及其优势所在
在Java企业级应用开发的漫长历史中,Spring框架以其卓越的依赖注入和面向切面编程的能力,赢得了广大开发者的青睐。然而,随着技术的不断进步和项目的日益复杂,传统的Spring应用开发流程逐渐显得繁琐和低效。为了解决这一问题&#…...
面向对象——类与对象
文章目录 类与对象构造函数、析构函数get/set方法函数:类内声明、类外定义static 类与对象 #include<iostream> #include<string> using namespace std; /* 类与对象 */ class Person{public:string name;// 固有属性,成员变量 int age;pu…...
Golang的[]interface{}为什么不能接收[]int?
在 Go 中,[]interface{} 和 []int 是两种不同的类型,虽然它们的底层数据结构都是切片,但是它们的元素类型不同。[]interface{} 是一个空接口切片,可以容纳任意类型的元素,而 []int 是一个整数切片,只能容纳…...
重启服务器或重启docker,导致emqx的Dashboard的密码重置为public
最近在项目中突然发现重启服务器,或者重启docker 修改好的emqx的Dashboard的密码重置为public 技术博客 http://idea.coderyj.com/ 1.解决办法就是固定 emqx的节点 # 拉取镜像 docker pull emqx/emqx# 创建目录,进行目录挂载 mkdir -p /docker/emqx/{etc,lib,data,…...
<6>-MySQL表的增删查改
目录 一,create(创建表) 二,retrieve(查询表) 1,select列 2,where条件 三,update(更新表) 四,delete(删除表…...
Spark 之 入门讲解详细版(1)
1、简介 1.1 Spark简介 Spark是加州大学伯克利分校AMP实验室(Algorithms, Machines, and People Lab)开发通用内存并行计算框架。Spark在2013年6月进入Apache成为孵化项目,8个月后成为Apache顶级项目,速度之快足见过人之处&…...
【入坑系列】TiDB 强制索引在不同库下不生效问题
文章目录 背景SQL 优化情况线上SQL运行情况分析怀疑1:执行计划绑定问题?尝试:SHOW WARNINGS 查看警告探索 TiDB 的 USE_INDEX 写法Hint 不生效问题排查解决参考背景 项目中使用 TiDB 数据库,并对 SQL 进行优化了,添加了强制索引。 UAT 环境已经生效,但 PROD 环境强制索…...
在rocky linux 9.5上在线安装 docker
前面是指南,后面是日志 sudo dnf config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo sudo dnf install docker-ce docker-ce-cli containerd.io -y docker version sudo systemctl start docker sudo systemctl status docker …...
如何在看板中体现优先级变化
在看板中有效体现优先级变化的关键措施包括:采用颜色或标签标识优先级、设置任务排序规则、使用独立的优先级列或泳道、结合自动化规则同步优先级变化、建立定期的优先级审查流程。其中,设置任务排序规则尤其重要,因为它让看板视觉上直观地体…...
CMake基础:构建流程详解
目录 1.CMake构建过程的基本流程 2.CMake构建的具体步骤 2.1.创建构建目录 2.2.使用 CMake 生成构建文件 2.3.编译和构建 2.4.清理构建文件 2.5.重新配置和构建 3.跨平台构建示例 4.工具链与交叉编译 5.CMake构建后的项目结构解析 5.1.CMake构建后的目录结构 5.2.构…...
BCS 2025|百度副总裁陈洋:智能体在安全领域的应用实践
6月5日,2025全球数字经济大会数字安全主论坛暨北京网络安全大会在国家会议中心隆重开幕。百度副总裁陈洋受邀出席,并作《智能体在安全领域的应用实践》主题演讲,分享了在智能体在安全领域的突破性实践。他指出,百度通过将安全能力…...
vue3+vite项目中使用.env文件环境变量方法
vue3vite项目中使用.env文件环境变量方法 .env文件作用命名规则常用的配置项示例使用方法注意事项在vite.config.js文件中读取环境变量方法 .env文件作用 .env 文件用于定义环境变量,这些变量可以在项目中通过 import.meta.env 进行访问。Vite 会自动加载这些环境变…...
三分算法与DeepSeek辅助证明是单峰函数
前置 单峰函数有唯一的最大值,最大值左侧的数值严格单调递增,最大值右侧的数值严格单调递减。 单谷函数有唯一的最小值,最小值左侧的数值严格单调递减,最小值右侧的数值严格单调递增。 三分的本质 三分和二分一样都是通过不断缩…...
ThreadLocal 源码
ThreadLocal 源码 此类提供线程局部变量。这些变量不同于它们的普通对应物,因为每个访问一个线程局部变量的线程(通过其 get 或 set 方法)都有自己独立初始化的变量副本。ThreadLocal 实例通常是类中的私有静态字段,这些类希望将…...
