<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd "> <context:property-placeholder location="classpath:hibernate.properties" /> <context:annotation-config></context:annotation-config> <context:component-scan base-package="com.*"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component-scan> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${hibernate.driverClassName}"></property> <property name="url" value="${hibernate.url}"></property> <property name="username" value="${hibernate.name}"></property> <property name="password" value="${hibernate.password}"></property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect </prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop> <prop key="hibernate.query.factory_class">org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory</prop> <prop key="hibernate.jdbc.fetch_size">50</prop> <prop key="hibernate.jdbc.batch_size">25</prop> <prop key="hibernate.temp.use_jdbc_metadata_defaults">false</prop> </props> </property> <property name="mappingResources"> <list> <value> com/entity/User.hbm.xml </value> </list> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 开启AOP监听 只对当前配置文件有效 --> <aop:aspectj-autoproxy expose-proxy="true" proxy-target-class="true"/> <!-- 开启注解事务 只对当前配置文件有效 --> <tx:annotation-driven transaction-manager="transactionManager" mode="aspectj" proxy-target-class="true"/> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="get*" propagation="REQUIRED"/> <tx:method name="delete*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="*" propagation="REQUIRED"></tx:method> </tx:attributes> </tx:advice> <aop:config expose-proxy="true" proxy-target-class="true"> <aop:pointcut expression="within(com.*)" id="allMethod" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="allMethod" /> </aop:config> </beans>
package com.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 org.springframework.transaction.annotation.Transactional; import com.dao.UserDao; import com.entity.User; @ContextConfiguration({"classpath:applicationContext.xml"}) @Transactional @RunWith(SpringJUnit4ClassRunner.class) public class Test { @Autowired private UserDao userDao; @org.junit.Test public void test() { User user=new User(); user.setEMail("993126013@qq.com"); user.setRealname("张三"); user.setPassword("12333333"); user.setMobile("15850597175"); user.setSex("1"); user.setUsername("zhulljk"); int i=userDao.addUser(user); System.out.println(userDao.getUsername(user.getUsername()).getId()); } }
Hibernate: select MYSEQ.nextval from dual org.hibernate.internal.SessionFactoryImpl@1de8adb Hibernate: insert into ZHUT.S_USER (USERNAME, REALNAME, PASSWORD, SEX, TELPHONE, MOBILE, E_MAIL, ID) values (?, ?, ?, ?, ?, ?, ?, ?) Hibernate: select this_.ID as ID0_0_, this_.USERNAME as USERNAME0_0_, this_.REALNAME as REALNAME0_0_, this_.PASSWORD as PASSWORD0_0_, this_.SEX as SEX0_0_, this_.TELPHONE as TELPHONE0_0_, this_.MOBILE as MOBILE0_0_, this_.E_MAIL as E8_0_0_ from ZHUT.S_USER this_ where this_.USERNAME=? 41
sql已经执行成功,但是事务没有提交,我使用spring来管理事务,不知道哪个地方出现错误,求解惑。。