public class StudentDao extends HibernateDaoSupport{
public void save(User user) {
this.getHibernateTemplate().save(user);
}
}
public class StudentLog extends HibernateDaoSupport{
public void save(Log log) {
this.getHibernateTemplate().save(log);
}
}
@Component("sss")
public class StudentService {
private StudentDao s_dao;
private StudentLog s_log;
public StudentDao getS_dao() {
return s_dao;
}
@Resource
public void setS_dao(StudentDao sDao) {
s_dao = sDao;
}
public StudentLog getS_log() {
return s_log;
}
@Resource
public void setS_log(StudentLog sLog) {
s_log = sLog;
}
@Transactional
public void add(User user,Log log) {
System.out.println("begin");
s_dao.save(user);
s_log.save(log);
System.out.println("end");
}
}
<?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:p="http://www.springframework.org/schema/p"
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-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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
">
<context:component-scan base-package="com.sl" />
<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/spring" />
<property name="username" value="root" />
<property name="password" value="qi" />
</bean>
<bean id="factory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="mydataSource" />
<property name="annotatedClasses">
<list>
<value>com.sl.entity.User</value>
<value>com.sl.entity.Log</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true
hibernate.format_sql=true
hibernate.hbm2ddl.auto=create
</value>
</property>
</bean>
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="factory" />
</bean>
<bean id="template" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="factory" />
</bean>
<tx:annotation-driven transaction-manager="txManager"/>
<bean id="t_log" class="com.sl.dao.StudentLog">
<property name="hibernateTemplate" ref="template"></property>
</bean>
<bean id="t_dao" class="com.sl.dao.StudentDao">
<property name="hibernateTemplate" ref="template"></property>
</bean>
</beans>
每继承一个hibernateDaosupport都要在xml文件里陪一个类,