1

我发现 HibernateTemplate 已从 Hibernate 4 中删除,我应该如何配置 Dao 应用程序上下文 xml 文件?

4

1 回答 1

3

sessionFactory.getCurrentSession()在我的 DAO 课程中使用。Spring配置是这样的:

<bean id="dataSource"
      class="org.springframework.jdbc.datasource.DriverManagerDataSource"
      p:driverClassName="${jdbc.driverClassName}"
      p:url="${jdbc.url}"
      p:username="${jdbc.username}"
      p:password="${jdbc.password}"/>

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="annotatedClasses">
        <list>
            <value>my.package.entity.Account</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
        </props>
    </property>
</bean>

<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>

<tx:annotation-driven transaction-manager="transactionManager"/>

所以现在你应该使用例如:

@Autowired
private SessionFactory sessionFactory;

public void save(YourEntity entity) {
    sessionFactory.getCurrentSession().save(entity);
}

将您的更改HibernateTemplateSessionFactory.

于 2012-10-29T12:06:49.630 回答