Spring中hibernateTemplate的使用

x33g5p2x  于2021-03-14 发布在 Spring  
字(4.5k)|赞(0)|评价(0)|浏览(86)

HibernateTemplate的配置和使用:

1、配置bean文件:因为要用到sessionFactory索性就都复制了过来.也方便大家看

<?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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/tx
           http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
           http://www.springframework.org/schema/aop
           http://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方法:

@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();
		}
	}
}

相关文章