spring配置
<context:component-scan base-package="pre.liuxl.blog"/>
<context:property-placeholder location="classpath:/hibernate.properties"/>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- 配置session工厂 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="annotatedClasses">
<list>
<value>pre.liuxl.blog.entity.User</value>
</list>
</property>
</bean>
<!-- 最后配置 HibernateTemplate bean-->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 事物管理器配置 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
springmvc配置
<context:component-scan base-package="pre.liuxl.blog.controller"/>
<!-- 默认的注解映射,不加访问不到controller-->
<mvc:annotation-driven/>
<!-- 解除servlet对静态资源文件访问的限制 -->
<mvc:default-servlet-handler/>
<mvc:resources mapping="/**"
location="classpath:/static/"/>
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<mvc:exclude-mapping path="/login"/>
<mvc:exclude-mapping path="/loginCheck"/>
<mvc:exclude-mapping path="/layui/**"/>
<bean class="pre.liuxl.blog.interceptor.SecurityInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
<!-- SpringResourceTemplateResolver automatically integrates with Spring's own -->
<!-- resource resolution infrastructure, which is highly recommended. -->
<bean id="templateResolver"
class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
<property name="prefix" value="classpath:/templates/"/>
<property name="suffix" value=".html"/>
<property name="characterEncoding" value="UTF-8"></property>
<!-- HTML is the default value, added here for the sake of clarity. -->
<property name="templateMode" value="HTML"/>
<!-- Template cache is true by default. Set to false if you want -->
<!-- templates to be automatically updated when modified. -->
<property name="cacheable" value="false"/>
</bean>
<!-- SpringTemplateEngine automatically applies SpringStandardDialect and -->
<!-- enables Spring's own MessageSource message resolution mechanisms. -->
<bean id="templateEngine"
class="org.thymeleaf.spring5.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver"/>
<!-- Enabling the SpringEL compiler with Spring 4.2.4 or newer can speed up -->
<!-- execution in most scenarios, but might be incompatible with specific -->
<!-- cases when expressions in one template are reused across different data -->
<!-- ypes, so this flag is "false" by default for safer backwards -->
<!-- compatibility. -->
<property name="enableSpringELCompiler" value="true"/>
</bean>
<bean class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine"/>
<property name="characterEncoding" value="UTF-8"></property>
</bean>
<!-- 文件上传的配置 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.support.StandardServletMultipartResolver"/>
测试的方法
@Transactional
public void addUser() {
User user = hibernateTemplate.get(User.class, 1);
user.setUserName("bbba");
hibernateTemplate.update(user);
User user1 = new User();
user1.setPassword("aa");
user1.setUserName("aaa");
hibernateTemplate.save( user1);
throw new RuntimeException();
}
为什么只有update方法回滚了,save方法没有回滚,数据库里update没有起作用,save起作用了
@Transactional(rollbackFor = Exception.class)
还是会执行save,只有save不会回滚,delete 和update都可以回滚
@龙龙龙龙龍2333: 你把save和update顺序换一下试试
hibernateTemplate setAutoCommit 好像默认是true 说明save方法是自动提交的
https://blog.csdn.net/qq_33061377/article/details/79452590 有一些常见的错误原因
你手动抛异常看看回滚不,下面是我的配置文件提供参考
<!-- 配置mybatis SqlSessionFactoryBean --> <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:mybatis-config.xml"/> </bean> <!-- 事务管理 --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 配置事务通知,即让哪些方法需要事务支持 --> <tx:advice id="tx" transaction-manager="txManager"> <tx:attributes> <tx:method name="*" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <!-- 配置事务切面,即让哪些包下的类需要事务 --> <aop:config> <aop:pointcut id="pointcut" expression="execution(* com.wzpbk.Service.*.*(..))"/> <aop:advisor advice-ref="tx" pointcut-ref="pointcut"/> </aop:config>
此项目码云地址https://gitee.com/liuxuanlong/blog,大家可以来看一下,希望大家能帮我解答
– 龙龙龙龙龍2333 6年前