SSM学习4:spring整合mybatis、spring整合Junit

spring整合mybatis

之前的内容是有service层(业务实现层)、dao层(操作数据库),现在新添加一个domain(与业务相关的实体类)

依赖配置
pom.xml

<?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.itheima</groupId><artifactId>spring_15_spring_mybatis</artifactId><version>1.0-SNAPSHOT</version><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.2.10.RELEASE</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.16</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.16</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.47</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>5.2.10.RELEASE</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>1.3.0</version></dependency></dependencies>
</project>

注解配置
resource/jdbc.properties
mysql数据库相关的配置信息

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring_db?useSSL=false
jdbc.username=root
jdbc.password=

config/JdbcConfig.class

package com.itheima.config;import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;import javax.sql.DataSource;public class JdbcConfig {@Value("${jdbc.driver}")private String driver;@Value("${jdbc.url}")private String url;@Value("${jdbc.username}")private String userName;@Value("${jdbc.password}")private String password;@Beanpublic DataSource dataSource(){DruidDataSource ds = new DruidDataSource();ds.setDriverClassName(driver);ds.setUrl(url);ds.setUsername(userName);ds.setPassword(password);return ds;}
}

config/MybatisConfig.class

public class MybatisConfig {//定义bean,SqlSessionFactoryBean,用于产生SqlSessionFactory对象// DataSource dataSource,@Bean定义的方法,形参会根据类型自动注入,也就是上面的JdbcConfig中的DataSource @Beanpublic SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource){SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean();// 扫描所有的实体类ssfb.setTypeAliasesPackage("com.itheima.domain");ssfb.setDataSource(dataSource);return ssfb;}//定义bean,返回MapperScannerConfigurer对象@Beanpublic MapperScannerConfigurer mapperScannerConfigurer(){MapperScannerConfigurer msc = new MapperScannerConfigurer();// 扫描与数据库操作的类msc.setBasePackage("com.itheima.dao");return msc;}
}

config/SpringConfig.class

@Configuration
@ComponentScan("com.itheima")
//@PropertySource:加载类路径jdbc.properties文件
@PropertySource("classpath:jdbc.properties")
@Import({JdbcConfig.class,MybatisConfig.class})
public class SpringConfig {
}

业务类
AccountDao
操作数据库,应该是通过MyBatis操作数据库

public interface AccountDao {@Insert("insert into tbl_account(name,money)values(#{name},#{money})")void save(Account account);@Delete("delete from tbl_account where id = #{id} ")void delete(Integer id);@Update("update tbl_account set name = #{name} , money = #{money} where id = #{id} ")void update(Account account);@Select("select * from tbl_account")List<Account> findAll();@Select("select * from tbl_account where id = #{id} ")Account findById(Integer id);
}

AccountServiceImpl

@Service
public class AccountServiceImpl implements AccountService {@Autowiredprivate AccountDao accountDao;public void save(Account account) {accountDao.save(account);}public void update(Account account){accountDao.update(account);}public void delete(Integer id) {accountDao.delete(id);}public Account findById(Integer id) {return accountDao.findById(id);}public List<Account> findAll() {return accountDao.findAll();}
}

App

public class App {public static void main(String[] args) {ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);AccountService accountService = ctx.getBean(AccountService.class);Account ac = accountService.findById(1);System.out.println(ac);}
}

在这里插入图片描述

spring整合Junit

pom.xml

<?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.itheima</groupId><artifactId>spring_16_spring_junit</artifactId><version>1.0-SNAPSHOT</version><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.2.10.RELEASE</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.16</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.6</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.47</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>5.2.10.RELEASE</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>1.3.0</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>5.2.10.RELEASE</version></dependency></dependencies>
</project>

测试类
test/java/com.itheima.service/AccountServiceTest

//设置类运行器
@RunWith(SpringJUnit4ClassRunner.class)
//设置Spring环境对应的配置类
@ContextConfiguration(classes = SpringConfig.class)
public class AccountServiceTest {//支持自动装配注入bean@Autowiredprivate AccountService accountService;@Testpublic void testFindById() {System.out.println(accountService.findById(1));}@Testpublic void testFindAll() {System.out.println(accountService.findAll());}
}

在这里插入图片描述

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

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

相关文章

解决ScaleBox来实现大屏自适应时,页面的饼图会变形的问题

封装一个公用组件pieChartAdaptation.vue 代码如下&#xff1a; <template><div :style"styleObject" class"pie-chart-adaptation"><slot></slot></div> </template><script setup lang"ts"> impo…

2.2.3 C#中显示控件BDPictureBox 的实现----控件实现

2.2.3 C#中显示控件BDPictureBox 的实现----控件实现 1 界面控件布局 2图片内存Mat类说明 原始图片&#xff1a;m_raw_mat ,Display_Mat()调用时更新或者InitDisplay_Mat时更新局部放大显示图片&#xff1a;m_extract_zoom_mat&#xff0c;更新scale和scroll信息后更新overla…

2024年精选100道软件测试面试题(内含文档)

测试技术面试题 1、我现在有个程序&#xff0c;发现在 Windows 上运行得很慢&#xff0c;怎么判别是程序存在问题还是软硬件系统存在问题&#xff1f; 2、什么是兼容性测试&#xff1f;兼容性测试侧重哪些方面&#xff1f; 3、测试的策略有哪些&#xff1f; 4、正交表测试用…

Eureka与Spring Cloud Bus的协同:打造智能服务发现新篇章

Eureka与Spring Cloud Bus的协同&#xff1a;打造智能服务发现新篇章 在微服务架构中&#xff0c;服务发现是实现服务间通信的关键机制。Eureka作为Netflix开源的服务发现框架&#xff0c;与Spring Cloud Bus的集成&#xff0c;提供了一种动态、响应式的服务治理解决方案。本文…

市场规模5万亿,护理员缺口550万,商业护理企业如何解决服务供给难题?

干货抢先看 1. 据统计&#xff0c;我国失能、半失能老人数量约4400万&#xff0c;商业护理服务市场规模达5万亿。然而&#xff0c;当前养老护理员缺口巨大&#xff0c;人员的供需不匹配是很多养老服务企业需要克服的难题。 2. 当前居家护理服务的主要市场参与者分为两类&…

利用GPT 将 matlab 内置 bwlookup 函数转C

最近业务需要将 matlab中bwlookup 的转C 这个函数没有现成的m文件参考&#xff0c;内置已经打成库了&#xff0c;所以没有参考源代码 但是它的解释还是很清楚的&#xff0c;可以根据这个来写 Nonlinear filtering using lookup tables - MATLAB bwlookup - MathWorks 中国 A…

python请求报错::requests.exceptions.ProxyError: HTTPSConnectionPool

在发送网页请求时&#xff0c;发现很久未响应&#xff0c;最后报错&#xff1a; requests.exceptions.ProxyError: HTTPSConnectionPool(hostsvr-6-9009.share.51env.net, port443): Max retries exceeded with url: /prod-api/getInfo (Caused by ProxyError(Unable to conne…

秒懂设计模式--学习笔记(5)【创建篇-抽象工厂】

目录 4、抽象工厂4.1 介绍4.2 品牌与系列&#xff08;针对工厂泛滥&#xff09;(**分类**)4.3 产品规划&#xff08;**数据模型**&#xff09;4.4 生产线规划&#xff08;**工厂类**&#xff09;4.5 分而治之4.6 抽象工厂模式的各角色定义如下4.7 基于此抽象工厂模式以品牌与系…

vue启动时的错误

解决办法一&#xff1a;在vue.config.js中直接添加一行代码 lintOnSave:false 关闭该项目重新运行就可启动 解决办法二&#xff1a; 修改组件名称

Python容器 之 通用功能

1.切片 1.格式&#xff1a; 数据[起始索引:结束索引:步长 2.适用类型&#xff1a; 字符串(str)、列表(list)、元组(tuple) 3.说明&#xff1a; 通过切片操作, 可以获取数据中指定部分的内容 4.注意 : 结束索引对应的数据不会被截取到 支持正向索引和逆向索引 步长用于设置截取…

配音软件有哪些?分享五款超级好用的配音软件

随着嫦娥六号的壮丽回归&#xff0c;举国上下都沉浸在这份自豪与激动之中。 在这样一个历史性的时刻&#xff0c;我们何不用声音记录下这份情感&#xff0c;让这份记忆以声音的形式流传&#xff1f; 无论是制作视频分享这份喜悦&#xff0c;还是创作音频讲述探月故事&#xff…

Oracle数据库中RETURNING子句

RETURNING子句允许您检索插入、删除或更新所修改的列&#xff08;以及基于列的表达式&#xff09;的值。如果不使用RETURNING&#xff0c;则必须在DML语句完成后运行SELECT语句&#xff0c;才能获得更改列的值。因此&#xff0c;RETURNING有助于避免再次往返数据库&#xff0c;…

Plotly:原理、使用与数据可视化的未来

文章目录 引言Plotly的原理Plotly的基本使用安装Plotly创建基本图表定制图表样式 Plotly的高级特性交互式图表图表动画图表集成 结论 引言 在当今的数据驱动世界中&#xff0c;数据可视化已经成为了一个至关重要的工具。它允许我们直观地理解数据&#xff0c;发现数据中的模式…

CXL-GPU: 全球首款实现百ns以内的低延迟CXL解决方案

数据中心在追求更高性能和更低总拥有成本&#xff08;TCO&#xff09;的过程中面临三大主要内存挑战。首先&#xff0c;当前服务器内存层次结构存在局限性。直接连接的DRAM与固态硬盘&#xff08;SSD&#xff09;存储之间存在三个数量级的延迟差异。当处理器直接连接的内存容量…

VideoPrism——探索视频分析领域模型的算法与应用

概述 论文地址:https://arxiv.org/pdf/2402.13217.pdf 视频是我们观察世界的生动窗口&#xff0c;记录了从日常瞬间到科学探索的各种体验。在这个数字时代&#xff0c;视频基础模型&#xff08;ViFM&#xff09;有可能分析如此海量的信息并提取新的见解。迄今为止&#xff0c;…

【vuejs】vue-router 路由跳转参数传递详解和应用场景及技巧

1. Vue2 Router 路由基础 1.1 路由定义 路由定义是Vue Router中实现页面路由跳转的基础。在Vue2中&#xff0c;路由的定义通常在应用的入口文件或路由配置文件中进行。路由定义涉及到路径模式&#xff08;path&#xff09;、视图组件&#xff08;component&#xff09;以及一…

【数据分析思维--史上最全最牛逼】

前言&#xff1a; &#x1f49e;&#x1f49e;大家好&#xff0c;我是书生♡&#xff0c;主要和大家分享一下数据分析的思维&#xff01;怎么提好我们对于业务的判断是非常重要的&#xff01;&#xff01;&#xff01;希望对大家有所帮助。 &#x1f49e;&#x1f49e;代码是你…

采煤机作业3D虚拟仿真教学线上展示增强应急培训效果

在化工行业的生产现场&#xff0c;安全永远是首要之务。为了加强从业人员的应急响应能力和危机管理能力&#xff0c;纷纷引入化工行业工艺VR模拟培训&#xff0c;让应急演练更加生动、高效。 化工行业工艺VR模拟培训软件基于真实的厂区环境&#xff0c;精确还原了各类事件场景和…

医疗器械FDA | 医疗器械软件如何做源代码审计?

医疗器械网络安全测试https://link.zhihu.com/?targethttps%3A//www.wanyun.cn/Support%3Fshare%3D24315_ea8a0e47-b38d-4cd6-8ed1-9e7711a8ad5e 医疗器械源代码审计是一个确保医疗器械软件安全性和可靠性的重要过程。以下是医疗器械源代码审计的主要步骤和要点&#xff0c;以…

Vue3 sortablejs 表格拖拽后,表格无法更新的问题处理

实用sortablejs在vue项目中实现表格行拖拽排序 你可能会发现&#xff0c;表格排序是可以实现&#xff0c;但是我们基于数据驱动的vue中关联的数据并没有发生变化&#xff0c; 如果你的表格带有列固定(固定列实际上在dom中有两个表格&#xff0c;其中固定的列在一个表格中&…