<
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.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
>
<
bean
id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
<property name= "configLocation">
<value> classpath:hibernate.cfg.xmlvalue >
property>
bean
>
<
bean
class= "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
<property name= "locations">
<value> classpath:jdbc.propertiesvalue >
property>
bean
>
<
bean
id="dataSource" destroy-method="close"
class= "org.apache.commons.dbcp.BasicDataSource" >
<property name= "driverClassName" value="${jdbc.driverClassName}" />
<property name= "url" value ="${jdbc.url}" />
<property name= "username" value="${jdbc.username}" />
<property name= "password" value="${jdbc.password}" />
bean
>
<
bean
id="sessionFactory1" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
<property name= "dataSource" ref ="dataSource"/>
<property name= "mappingResources">
<list>
<value> cn/itheima03/spring/ hibernate/transaction/domain/Classes.hbm.xml value>
list>
property>
<property name= "hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQLDialect
value>
property>
bean
>
<
bean
id="classesDao" class="cn.itheima03.spring.hibernate.transaction.dao.impl.ClassesDaoImpl" >
<property name= "sessionFactory">
<ref bean= "sessionFactory1"/>
property>
bean
>
<
bean
id="classesService" class="cn.itheima03.spring.hibernate.transaction.service.impl.ClassesServiceImpl" >
<property name= "classesDao">
<ref bean= "classesDao"/>
property>
bean
>
<
bean
id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" >
<property name= "sessionFactory">
<ref bean= "sessionFactory1"/>
property>
bean
>
<
tx:advice
id="tx" transaction-manager="transactionManager" >
<tx:attributes>
<tx:method name= "save*" read-only ="false"/>
tx:attributes>
tx:advice
>
<
aop:config
>
<aop:pointcut expression="execution(* cn.itheima03.spring.hibernate.transaction.service.impl.ClassesServiceImpl.*(..))" id ="perform"/>
<aop:advisor advice-ref="tx" pointcut-ref= "perform"/>
aop:config
>
beans
>
|
//继承
HibernateDaoSupport
public
class ClassesDaoImpl extends HibernateDaoSupport implements ClassesDao{
@Override
public
void saveClasses(Classes classes) {
this.getHibernateTemplate().save(classes);
}
}
====================================
public
class ClassesServiceImpl implements ClassesService{
private
ClassesDao classesDao;
public
ClassesDao getClassesDao() {
return classesDao ;
}
public
void setClassesDao(ClassesDao classesDao) {
this.classesDao = classesDao;
}
public
void saveClasses(Classes classes){
this.classesDao .saveClasses(classes);
}
}
====================================
public
class ClassesServiceTest {
@Test
public
void testSaveClasses(){
ApplicationContext context =
new ClassPathXmlApplicationContext("cn/itheima03/spring/hibernate/transaction/config/applicationContext.xml" );
ClassesService classesService = (ClassesService) context.getBean(
"classesService"
);
Classes classes =
new Classes();
classes.setCname(
"aa");
classesService.saveClasses(classes);
}
}
|