即使在刷新Hibernate之后,当使用保存但不将数据插入MySQL表中时,Junit成功

pxq42qpu  于 2023-04-12  发布在  Mysql
关注(0)|答案(2)|浏览(99)

简单的DAO层与applicationContext.xml请在下面找到我的applicationContect.xml请帮助我理解为什么我的Junit说成功,但数据没有插入到数据库。

<?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-3.0.xsd  
http://www.springframework.org/schema/context  
http://www.springframework.org/schema/context/spring-context-3.0.xsd  
http://www.springframework.org/schema/tx  
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<context:component-scan base-package="com.VideoRental.*" />
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<bean id="mySessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource" />
<property name="annotatedClasses">
<list>
<value>com.VideoRental.domain.Category</value>
<value>com.VideoRental.domain.Customer</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect"> org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="hibernate.hbm2ddl.import_files">import.sql</prop>  
<prop key="connection.autocommit">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.connection.autocommit">true</prop> 

</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
  <property name="sessionFactory" ref="mySessionFactory"/>
</bean>

</beans>
ljo96ir5

ljo96ir51#

默认情况下,@Transactional测试将在测试方法完成时回滚所有更改。因此,即使测试通过,更改也仅对当前运行的事务有效。要持久化更改,您需要提交事务。
查看此答案的示例以了解更多细节。
您可以将@Transactional替换为transactionTemplate,以便在完成测试之前提交事务。

zvms9eto

zvms9eto2#

如果在项目中使用@Transactional注解驱动,则只需设置

@TransactionConfiguration(defaultRollback = false, transactionManager = "YourTransactionManager")

我已经研究了几天,最后在我的本地测试用例中工作得很好,希望这对你也有帮助。

相关问题