当前位置:网站首页>SSM整合其他组件
SSM整合其他组件
2022-07-21 07:03:00 【路过Coder】
除了把Mybatis和Spring整合在一起之外,为了辅助工程能够更好的开发,还需要额外配置三项:
- 配置logback日志输出:通过logback,可以在程序运行过程中,更清晰的看到程序的执行过程,以及有哪些潜在问题
- 声明式事务配置:声明式事务比编程式事务更简单和灵活,可以简化事务控制代码
- 整合JUnit单元测试:我们编写的程序,在正式发布前需要单元测试;这个组件能够提高测试的效率
整合Junit单元测试
引入依赖
<!--单元测试依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
创建一个测试用的Mapper接口:
public interface TestMapper {
public void insert();
}
创建一个供测试用的xml文件(存放sql的mapper):
mapper中的namespace需要写接口的全类名
SQL标签中的id要与接口中的方法名称保持一致。
由于我们配置了Mapper扫描器,此时,在运行时候,就可以自动生成接口的实现类了
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.imooc.reader.mapper.TestMapper">
<insert id="insert">
insert into test(content) values ('测试内容')
</insert>
</mapper>
创建一个测试用的TestService类:
@Resource用来注入刚才运行时所产生的testMapper对象
Mapper扫描器扫描到TestMapper接口,会生成该接口的实现类,然后该接口实现类的对象也被loc容器管理。
这儿配置了Mapper扫描器,其会自动实例化Mapper接口,(同时也配置了SqISessionFactory等 )我们在调用的时候,直接调用接口中的方法行了,我们以前接触的如先获取sqlsession,然后通过getMapper()方法去实例化接口对象的代码,都不用写了。
@Service
public class TestService {
@Resource
private TestMapper testMapper;
public void batchImport() {
for (int i = 0; i < 5; i++) {
testMapper.insert();
}
}
}
选中类名,【Alt + insert】快速生成test
@RunWith注解是JUnit4运行时会 自动初始化loC容器,同时把JUnit4的控制权交给Spring Test;
@ContextConfiguration注解是:指明spring的配置文件的位置
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class TestServiceTest {
@Resource
private TestService testService;
@Test
public void batchImport() {
testService.batchImport();
System.out.println("测试数据批量导入成功。");
}
}
整合logback日志
引入依赖,Spring会自动识别logback的存在,对其进行集成。在resources目录下,创建logback.xml文件,并通过基本配置。
<!--引入logback-->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
logback.xml:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%d{HH:mm:ss} %-5level [%thread] %logger{30} - %msg%n</pattern>
<chasert>UTF-8</chasert>
</encoder>
</appender>
<root level="debug">
<appender-ref ref="console"/>
</root>
</configuration>
配置声明式事务
在applicationContext中配置DataSourceTransactionManager事务管理器对象
<!--声明式事务,配置事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
启用注解形式声明式事务
<!-- 启用注解形式声明式事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
只要配置了这两项,loC容器就默认提供了声明事务的控制。
声明式事务的使用
在需要事务控制的Service方法上使用[@Transactional]注解;使用这个注解后: 如果方法执行成功,则进行全局提交,如果方法抛出了运行时异常,则进行全局的回滚,即通过这个注解,就可以做到要么全做,要么一个也不做的目的。
@Service
public class TestService{
private TestMapper testMapper;
@Transactional
public void batchImport(){
for (int i = 0;i < 5; i++){
if (i == 3){
throw new RuntimeException("运行时异常");
}
testMapper.insert();
}
}
}
边栏推荐
- 【JVM】垃圾收集器的选择
- Diversified system to carry out maker education activities
- mysql docker安装 主从部署
- FreeRTOS -- a method to detect the usage of task stack
- Diffusion Model
- [paper notes] 3D point cloud segmentation pointnet
- [terminal _1]-xshell 5 the hottest terminal software!
- Learn the necessary tools of automation selenium think about automated testing in the pit again
- 氟尼辛肽核酸寡聚体复合物|规活性基团Alkyne炔烃,SH Thiol炔基修饰肽核酸
- Word2vec simple summary
猜你喜欢
随机推荐
The classification of artificial neural network includes: the classification of artificial neural network includes
Statistics for each month of the year
Steve Aoki's Avatar will come to the sandbox metauniverse!
【SemiDrive源码分析】【模块BringUp】37 - LCM 驱动 Bringup 流程
First knowledge of loop and branch statements in C language
音频自动增益控制 AGC 解决的问题及原理解析
VScode运行C语言文件
Uniapp introduces Tencent map
Difference between two lists
Oracle sets the maximum number of connections
LeetCode 1911. 最大子序列交替和
OpenGL (1)顶点缓冲
nslookup命令使用
JS get the server IP, port and protocol
ITK median filter
奇葩!面试题目竟设置如厕习惯、吃饭时长、入睡时间
44:第四章:开发文件服务:5:在【files】文件服务中,整合FastDFS,实现【上传头像】的逻辑;(包括:项目中整合FastDFS;跨域问题;创建资源文件,并在代码中获取属性;异常处理;)
近红外染料CY7.5标记PNA多肽实验步骤CY7.5-PNA|188Re标记反基因肽核酸(AGPNA)
A collection of style problems of uniapp applets using the u-view framework
NetworkX的基本用法