1900_6789 2022-10-02 22:37 采纳率: 53.8%
浏览 29
已结题

测试事务配置是否成功 报[{dataSource-1} inited]

spring整合mybatis已成功,配置spring事务管理器时,用测试类测试向数据库插入数据,显示 [{dataSource-1} inited]
用代码块功能插入代码,请勿粘贴截图

1、spring-persist-tx.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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

    <context:component-scan base-package="com.atguigu.crowd.service"/>
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <aop:config>
        <aop:pointcut id="txPointcut" expression="execution(* *..*ServiceImpl.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
    </aop:config>
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="get*" read-only="true"/>
            <tx:method name="find*" read-only="true"/>
            <tx:method name="query*" read-only="true"/>
            <tx:method name="count*" read-only="true"/>

            <tx:method name="save*" propagation="REQUIRES_NEW" rollback-for="java.lang.Exception"/>
            <tx:method name="update*" propagation="REQUIRES_NEW" rollback-for="java.lang.Exception"/>
            <tx:method name="remove*" propagation="REQUIRES_NEW" rollback-for="java.lang.Exception"/>
            <tx:method name="batch*" propagation="REQUIRES_NEW" rollback-for="java.lang.Exception"/>
        </tx:attributes>
    </tx:advice>

</beans>

2、spring-persist-mybatis.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
    
    <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
    
    
    <bean id="dataSource" class="com.aliba.druid.pool.DruidDataSource">
        
        <property name="username" value="${jdbc.user}"/>
        
        <property name="password" value="${jdbc.password}"/>
        
        <property name="url" value="${jdbc.url}"/>
        
        <property name="driverClassName" value="${jdbc.driver}"/>
    </bean>
    
    <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
        
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>
        
        <property name="mapperLocations" value="classpath:mybatis/mapper/*Mapper.xml"></property>   
        
        <property name="dataSource" ref="dataSource"></property>
        
        <property name="plugins">
            <array>
                <bean  class="com.github.pagehelper.PageHelper">
                    <property name="properties">
                        <props>
                            
                            <prop key="dialect">mysql</prop>
                            
                            <prop key="reasonable">true</prop>
                        </props>
                    </property>
                </bean>
            </array>
        </property>
    </bean>
    
    <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.atguigu.crowd.mapper"></property>
    </bean>
</beans>


3、测试类(其中除了testTx方法,其他测试方法都成功了)

package com.atguigu.crowd;

import com.atguigu.crowd.entity.Admin;
import com.atguigu.crowd.mapper.AdminMapper;
import com.atguigu.crowd.service.api.AdminService;
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;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

/**
 * @author 123
 * date 2022-09-18
 */
// 在类上标记必要的注解,spring整合junit
// 指定Spirng给Junit提供的运行器类
@RunWith(SpringJUnit4ClassRunner.class)
// 加载Spring配置文件的注解
@ContextConfiguration(locations = {"classpath:spring-persist-mybatis.xml", "classpath:spring-persist-tx.xml"})
public class CrowdTest {

    @Autowired
    private DataSource dataSource;

    @Autowired
    private AdminMapper adminMapper;

    @Autowired(required = false)
    private AdminService adminService;

    @Test
    public void testConnection() throws SQLException {
        Connection connection = dataSource.getConnection();
        System.out.println(connection);
    }

    @Test
    public void testTx() {
        Admin admin = new Admin(null, "jerry", "123456", "杰瑞", "jerry@qq.com", null);
        adminService.saveAdmin(admin);
    }

    @Test
    public void testInertAdmin() {
        Admin admin = new Admin(null, "nn", "123123", "汤姆", "tom@qq.com", null);
        int count = adminMapper.insert(admin);

        // 如果实际开发中,所有想查看数值的地方都用sysout方式打印,会给项目上线运行带来问题!
        // sysout本质上是一个IO的操作,通常IO的操作是比较小号性能的。如果项目中sysout很多,对性能的影响就很大了
        // 若使用日志系统,那么通过日志级别就能批量的控制信息的打印
        System.out.println("受影响的行数:" + count);
    }

}


运行结果及报错内容
22:23:16,080 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
22:23:16,080 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
22:23:16,080 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [file:/E:/tool/JetBrains/IdeaProjects/atcrowdfunding001-admin-parent/atcrowdfunding002-admin-webui/target/classes/logback.xml]
22:23:16,291 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
22:23:16,299 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [STDOUT]
22:23:16,314 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
22:23:16,374 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to INFO
22:23:16,375 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [STDOUT] to Logger[ROOT]
22:23:16,378 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [com.atguigu.crowd.mapper] to DEBUG
22:23:16,378 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
22:23:16,381 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@3f2a3a5 - Registering current configuration as safe fallback point
[22:23:16.557] [INFO ] [main] [org.springframework.test.context.support.DefaultTestContextBootstrapper]
                [Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]]
[22:23:16.581] [INFO ] [main] [org.springframework.test.context.support.DefaultTestContextBootstrapper]
                [Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@32d2fa64, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@1d8d30f7, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@3e57cd70, org.springframework.test.context.support.DirtiesContextTestExecutionListener@9a7504c, org.springframework.test.context.transaction.TransactionalTestExecutionListener@2c039ac6, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@587d1d39]]
[22:23:16.694] [INFO ] [main] [org.springframework.beans.factory.xml.XmlBeanDefinitionReader]
                [Loading XML bean definitions from class path resource [spring-persist-mybatis.xml]]
[22:23:16.902] [INFO ] [main] [org.springframework.beans.factory.xml.XmlBeanDefinitionReader]
                [Loading XML bean definitions from class path resource [spring-persist-tx.xml]]
[22:23:17.054] [INFO ] [main] [org.springframework.context.support.GenericApplicationContext]
                [Refreshing org.springframework.context.support.GenericApplicationContext@150c158: startup date [Sun Oct 02 22:23:17 CST 2022]; root of context hierarchy]
[22:23:19.221] [INFO ] [main] [com.aliba.druid.pool.DruidDataSource]
                [{dataSource-1} inited]
com.mysql.jdbc.JDBC4Connection@21fd5faa
[22:23:19.809] [INFO ] [Thread-1] [org.springframework.context.support.GenericApplicationContext]
                [Closing org.springframework.context.support.GenericApplicationContext@150c158: startup date [Sun Oct 02 22:23:17 CST 2022]; root of context hierarchy]
[22:23:19.815] [INFO ] [Thread-1] [com.aliba.druid.pool.DruidDataSource]
                [{dataSource-1} closed]

Process finished with exit code 0


我的解答思路和尝试过的方法

1、删除target,重新生成。无效。
2、重新敲一遍spring-persist-tx.xml。无效

我想要达到的结果

哪位朋友遇到过类似的问题呀,求解 ~

  • 写回答

2条回答 默认 最新

  • 1900_6789 2022-10-06 18:54
    关注

    问题解决啦 最后发现是servise写的有问题

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 10月14日
  • 已采纳回答 10月6日
  • 创建了问题 10月2日

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效