Analysis and Summary: common methods for accessing the database at the DaO layer in spring to manage hibernate (with the jar package of SSH)

Source: Internet
Author: User

The previous blog has explained several common methods for configuring data sources in spring. Next, I will continue to explain this blog. After configuring the data source, how does the DaO layer access the database?

Based on the recent project using the SSH2 framework, this paper analyzes and summarizes the two common methods for accessing the database at the DaO layer after spring and Hibernate are integrated.

Why does the persistence layer use the Hibernate framework? Please refer to my previous blog "hibernate Summary 1", "hibernate Summary 2", and "hibernate Summary 3".

Why should we use the Spring framework? Please refer to my previous blog "spring -- control reversal"

Why use spring to manage hibernate? For more information, see why I used spring to manage hibernate in my previous blog?

In this example, spring2.0 and hibernate3.0 are used. The corresponding jar package will be provided later.

 

Commonly used isSessionfactoryMethod

Now we use the fourth method of hibernate data source in the previous blog. The spring configuration method is as follows:

<? 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: AOP = "http://www.springframework.org/schema/aop" xmlns: context = "http://www.springframework.org/schema/context" 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. xsdhttp: // www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd "> <bean id =" sessionfactory "class =" org. springframework. Orm. hibernate3.localsessi Onfactorybean "> <property name =" configlocation "> <value> classpath: COM/config/hibernate. cfg. XML </value> </property> <property name = "mappinglocations"> <! -- All object class ing files --> <list> <value> classpath: COM/hibernate /*. HBM. XML </value> </List> </property> </beans>

The xml configuration of the DaO layer managed by spring is as follows:

<? 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: AOP = "http://www.springframework.org/schema/aop" xsi: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <impo RT resource = "springconfig. xml"/> <! -- Authorize Dao --> <bean id = "userdao" class = "com. dao. userdao "> <property name =" sessionfactory "ref =" sessionfactory "> </property> </bean> </beans>

For clear management, the core configuration of spring and the DaO layer of spring management are put in two XML files.

If the bean file of Dao in spring is configured in this way, let's take a look at the class Writing Method of Dao layer:

package com.dao;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;import com.entity.User;public class UserDao extends HibernateDaoSupport {public void insert(User user){this.getHibernateTemplate().save(user);}}

Since the sessionfactory attribute of Dao is configured, but sessionfactory is provided to hibernatedaosupport, you can open this class and check the content in it.

Use this. gethibernatetemplate () to obtain the hibernatetemplate () template class. The Hibernate template class contains the save, update, and delete methods.

 

The other is the hibernate template class.HibernatetemplateMethod.

On the basis of sessionfactory, The hibernatetemplate class provided by spring is encapsulated again.

The spring configuration file is as follows:

<? 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: AOP = "http://www.springframework.org/schema/aop" xmlns: context = "http://www.springframework.org/schema/context" 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. xsdhttp: // www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd "> <bean id =" sessionfactory "class =" org. springframework. Orm. hibernate3.localsessi Onfactorybean "> <property name =" configlocation "> <value> classpath: COM/config/hibernate. cfg. XML </value> </property> <property name = "mappinglocations"> <! -- All object class ing files --> <list> <value> classpath: COM/hibernate /*. HBM. XML </value> </List> </property> <bean name = "hibernatetemplate" class = "org. springframework. orm. hibernate3.hibernatetemplate "> <property name =" sessionfactory "ref =" sessionfactory "/> </bean> </beans>

The configuration of the DaO layer in spring management is as follows:

<? 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: AOP = "http://www.springframework.org/schema/aop" xsi: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <impo RT resource = "springconfig. xml"/> <! -- Authorize Dao --> <bean id = "userdao" class = "com. dao. userdao "> <property name =" hibernatetemplate "ref =" hibernatetemplate "> </property> </bean> </beans>

If hibernatetemplate is used, write the DaO layer class:

package com.dao;import org.springframework.orm.hibernate3.HibernateTemplate;import com.entity.User;public class UserDao2 {private HibernateTemplate hibernateTemplate;public HibernateTemplate getHibernateTemplate() {return hibernateTemplate;}public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {this.hibernateTemplate = hibernateTemplate;}public void insert(User user){      hibernateTemplate.save(user);}}

Spring injection can be divided into four methods: Common get/set methods, constructor methods, annotation methods, and infrequently used interface methods, you can refer to my previous blog spring-control reversal, which compares these methods.

Spring is used to manage struts. For hibernate, annotations are commonly used.

The configuration of the DaO layer is as follows:

<? 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: AOP = "http://www.springframework.org/schema/aop" xsi: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <impo RT resource = "springconfig. xml"/> <! -- Authorize Dao --> <bean id = "userdao" class = "com. Dao. userdao"> </bean> </beans>

The Dao layer class is written as follows:

package com.dao;import javax.annotation.Resource;import org.springframework.orm.hibernate3.HibernateTemplate;import com.entity.User;public class UserDao {@Resourceprivate HibernateTemplate hibernateTemplate;           public void insert(User user){hibernateTemplate.save(user);}}

In this way, the DaO layer class does not need to be inherited, And the hibernatetemplate can be injected directly using annotations.

In general, I like it and use the hibernatetemplate injection method in this project.

In this way, access to the DaO layer is basically complete, but what if the access process fails? Or when a DaO error occurs while accessing multiple Dao layers, is other Dao layers stopped at the same time? This involves transaction management. Hibernate also provides transaction management, in which the transactions provided by hibernate are automatically committed. Is there any more intelligent automatic commit? In the next blog, we will continue to explain several ways spring manages transactions.

 

PS: to help you understand the packages of each framework, you can store the packages separately.

: Http://download.csdn.net/detail/llhhyy1989/4501145

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.