SSM整合其他组件

导读:本篇文章讲解 SSM整合其他组件,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

除了把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();
        }
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/99471.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!