HibernateTemplate的配置和使用:
1、配置bean文件:因為要用到sessionFactory索性就都復制瞭過來.也方便大傢看
[html]
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="https://www.springframework.org/schema/beans"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns:context="https://www.springframework.org/schema/context"
xmlns:aop="https://www.springframework.org/schema/aop"
xmlns:tx="https://www.springframework.org/schema/tx"
xsi:schemaLocation="https://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans-2.5.xsd
https://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context-2.5.xsd
https://www.springframework.org/schema/tx
https://www.springframework.org/schema/tx/spring-tx-2.5.xsd
https://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<context:annotation-config />
<!– 配置容器資源掃描的包 –>
<context:component-scan base-package="com.spring" />
<!– 將前面類寫入容器 –>
<bean id="logInterceptor" class="com.spring.aop.LogInterceptor" />
<!–
配置數據源 <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/sms"/> <property name="username"
value="root"/> <property name="password" value="root"/> </bean>
–>
<!– placeholder 占位符 –>
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:jdbc.properties</value>
</property>
</bean>
<!– 配置dataSource –>
<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>
<!– 將配置好的dataSource註入到SessionFactory中–>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingResources">
<list>
<value>com/spring/model/user.hbm.xml</value>
<value>com/spring/model/userlog.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true
hibernate.hbm2ddl.auto=create
</value>
</property>
</bean>
<!– 聲明式事務管理,事務需要數據源,從sessionFactory中拿到
這是一個AOP的應用 –>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!– 配置事務要管理的方法 –>
<tx:advice transaction-manager="transactionManager" id="txManager">
<tx:attributes>
<tx:method name="save"/>
</tx:attributes>
</tx:advice>
<!– 配置aop設置切面和織入點邏輯 –>
<aop:config>
<aop:pointcut id="entryPointMethod" expression="execution(public * com.spring.service..*.*(..))"/>
<aop:advisor
advice-ref="txManager"
pointcut-ref="entryPointMethod"
/>
</aop:config>
<!– 配置一個hibernateTemplate –>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
</beans>
2、使用@resource註入,然後調用save方法:
[java]
@Component("userDaoImpl")
public class UserDaoImpl implements UserDao{
@Resource
private HibernateTemplate hibernateTemplate;
public HibernateTemplate getHibernateTemplate() {
return hibernateTemplate;
}
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
@Override
public void save(User u) {
try {
hibernateTemplate.save(u);
} catch (HibernateException e) {
e.printStackTrace();
}
}
}
作者:zhang6622056