Configuring HibernateTemplate in springmvc

The
first is to manually configure HibernateTemplate, that is, add the following sentence to the implemented Dao
private HibernateTemplate hibernateTemplate;  
 public HibernateTemplate getHibernateTemplate() {
  return hibernateTemplate;
 }
 @Resource
 public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
  this.hibernateTemplate = hibernateTemplate;
 }

And add the following configuration in applicationContext.xml
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
    <property name="sessionFactory" ref="sessionFactory"></property>
</bean>

The second is to use HibernateDaoSupport provided by Hibernate, that is, let the implemented Dao inherit HibernateDaoSupport. At this time, there is no need to manually configure HibernateTemplate, just add the following configuration in applicationContext.xml
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory">
   <ref bean="sessionFactory"/>
  </property>
 </bean>

Regarding the above xml configuration, in fact, it is also used when configuring transactions, that is, the first configuration already includes the second configuration.
PS: The main purpose of configuring HibernateTemplate is to use Hibernate to encapsulate the method of dealing with the database, but in fact, many times, we still need to use JDBC ordinary sql statements. At this time, it is a bit difficult to use HibernateTemplate alone. The best way is to obtain it manually. Session, create a connection, and then pass in the sql statement.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326420535&siteId=291194637