Spring entry Blog [15. encapsulation of HibernateDaoSupport in Spring]

Source: Internet
Author: User

Encapsulate HibernateDaoSupport:

We inherit this class when using HibernateDaoSupport. However, we cannot rewrite the setSessionFactory and setHibernateTemplate methods in it. However, these two attributes are required by HibernateDaoSupport. This leads to the problem of how to inject these two attributes. Below we will use two methods to solve this problem to complete the use of hibernateDaoSupport:

1. inherit HibernateDaoSupport as the parent class and use the super set method to inject it during initialization:

A. Configure sessionFactory in the container:


[Html]
<! -- Inject the configured dataSource into 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>
B. Call the set Method of super to inject sessionFactory.

[Java]
@ Component ("userLogDaoImpl ")
Public class UserLogDaoImpl extends HibernateDaoSupport implements UserLogDao {
// The super keyword calls the parent class method injection.
@ Resource
Public void setSuperSessionFactory (SessionFactory sessionFactory ){
Super. setSessionFactory (sessionFactory );
}
@ Override
Public void save (UserLog l ){
This. getHibernateTemplate (). save (l );
}
}
Disadvantages:

The above method writes injection-related code in every DaoImpl. This is a lot of trouble. The second type

2. do not inherit the HibernateDaoSupport and create a parent class to inject the hibernateTemplate as an attribute.

A. configure A hibernateTemplate in the container:

[Html]
<! -- Inject the configured dataSource into 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>
 
<! -- Configure hibernateTemplate -->
<Bean id = "hibernateTemplate" class = "org. springframework. orm. hibernate3.HibernateTemplate">
<Property name = "sessionFactory" ref = "sessionFactory"> </property>
</Bean>
B. Create a parent class to inject hibernateTemplate

[Java]
Public class SuperDaoSupport {
@ Resource
Private HibernateTemplate hibernateTemplate;
 
Public HibernateTemplate getHibernateTemplate (){
Return hibernateTemplate;
}
 
Public void setHibernateTemplate (HibernateTemplate hibernateTemplate ){
This. hibernateTemplate = hibernateTemplate;
}
}
C. inherit the parent class.


[Java]
@ Component ("userLogDaoImpl ")
Public class UserLogDaoImpl extends SuperDaoSupport implements UserLogDao {

@ Override
Public void save (UserLog l ){
This. getHibernateTemplate (). save (l );
}
}
Author: zhang6622056


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.