我用的MySql数据库,Hibernate做的持久层,Spring做的业务逻辑层。
之前用Junit做DAO的测试都很好(在DAO中用HibernateSessionFactory处理的事物),写service的时候就把dao的事物去掉了,改用springAOP的声明式事物。但是用Junit就不能添加用户了。高手帮我看看啊。我查了很多资料。感觉写的没问题啊!
我用的MyEclipse自动加载的Hibernate和Spring,应该包都倒了吧高手看看我源码。applicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"> <bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
</bean> <!-- 配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<!-- 配置事务的传播特性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="modify*" propagation="REQUIRED" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 那些类的哪些方法参与事务 -->
<aop:config>
<aop:pointcut id="allManagerMethod"
expression="execution(* com.h3c.service.impl.*.*(..))" />
<aop:advisor pointcut-ref="allManagerMethod"
advice-ref="txAdvice" />
</aop:config>
<!-- DAO -->
<bean id="workDAO" class="com.h3c.dao.impl.TbWorkDAOImpl" />
<bean id="peopleDAO" class="com.h3c.dao.impl.TbPeopleDAOImpl" /> <bean id="checkUserService"
class="com.h3c.service.impl.CheckUserServiceImpl">
<property name="tpdao" ref="peopleDAO"></property>
</bean> <bean id="HumanManageService"
class="com.h3c.service.impl.HumanManageServiceImpl">
<property name="tbdao" ref="peopleDAO"></property>
</bean>
</beans>HumanManageServiceImpl.java
package com.h3c.service.impl;import java.util.ArrayList;import com.h3c.dao.TbPeopleDAO;
import com.h3c.dto.TbPeople;public class HumanManageServiceImpl {
private TbPeopleDAO tbdao;

public TbPeopleDAO getTbdao() {
return tbdao;
} public void setTbdao(TbPeopleDAO tbdao) {
this.tbdao = tbdao;
} public void add(TbPeople people){
tbdao.addPeople(people);
}

public void del(TbPeople people){
tbdao.delPeople(people);
}

public void update(TbPeople people){
tbdao.modifyPeople(people);
}

public ArrayList<TbPeople> show(){
return (ArrayList<TbPeople>)tbdao.showAllPeople();
}
}
test.java
package test.com.h3c.service.impl;import static org.junit.Assert.fail;import java.util.ArrayList;import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import com.h3c.dto.TbPeople;
import com.h3c.service.impl.HumanManageServiceImpl;public class HumanManageServiceImplTest { @Test
public void testAdd() {
ApplicationContext context = new ClassPathXmlApplicationContext(
"applicationContext.xml"); HumanManageServiceImpl impl = (HumanManageServiceImpl) context
.getBean("HumanManageService");
TbPeople people = new TbPeople();
people.setPName("H3c");
people.setPWhere("销售公司");
impl.add(people);
} @Test
public void testDel() {
fail("Not yet implemented");
} @Test
public void testUpdate() {
fail("Not yet implemented");
} @Test
public void testShow() {
ApplicationContext context = new ClassPathXmlApplicationContext(
"applicationContext.xml");
HumanManageServiceImpl impl = (HumanManageServiceImpl) context
.getBean("HumanManageService");
ArrayList<TbPeople> list = impl.show();
for (TbPeople t : list) {
System.out.println(t.getPName());
}
}}