Analysis and summary of the common ways of the DAO layer Access database in the Spring management hibernate (with SSH jar package) __ Database

Source: Internet
Author: User
Tags aop xmlns ssh access database

The previous blog has explained several common ways in which spring configures data sources. Next, this blog continues to explain. After configuring the data source, how does the DAO layer access the database?

Based on the most recent project using the SSH2 framework, the analysis summarizes the two common ways in which the DAO layer accesses the database after spring and hibernate are integrated.

As for why the persistence layer uses the Hibernate framework. Please refer to my previous blog "Hibernate summary One" "Hibernate summary Two" "Hibernate summary Three"

As to why use the spring framework. Please refer to my previous blog "spring--Control inversion"

Why use spring to manage hibernate? Please refer to my previous blog why use spring to manage hibernate.

This example uses spring2.0,hibernate3.0, and the corresponding jar package is given later

commonly used is the sessionfactory Way

Now we use the fourth method of hibernate data source in the previous blog. Spring is configured in the following manner:

<?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.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/ Spring-aop-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 "> <bean id=" sessionfactory "class=" Org.springframework.orm.hibernate3.LocalSessionFactoryBean "> <property name=" configlocation "> <value
	>classpath:com/config/hibernate.cfg.xml</value>	</property> <property name= "Mappinglocations" > <!--All entity class mapping files--<list> <value>
 classpath:com/hibernate/*.hbm.xml</value> </list> </property> </beans>

The XML configuration of the Spring management 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.xsd
	HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/ Schema/aop/spring-aop-2.5.xsd ">
	<import resource=" Springconfig.xml "/>
	<!--authorized Administration DAO--
   <bean id= "Userdao" class= "Com.dao.UserDao" > 
      <property name= "sessionfactory" ref= "Sessionfactory" ></property>
    </bean> 

</beans>

Because for clarity of management, the core configuration of spring and the Spring Management DAO layer are placed in two XML files.

If the DAO bean file in spring is configured so, let's look at the DAO layer's class notation:

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 configuration of DAO's properties is Sessionfactory, but Sessionfactory is provided to Hibernatedaosupport, you can open this class to see what's inside.

Use This.gethibernatetemplate () to get the Hibernatetemplate () template class, where there are save,update,delete methods in the Hibernate templates class

the other is Hibernate template class hibernatetemplate Way.

This is based on Sessionfactory, which encapsulates the Hibernatetemplate class provided by spring.

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.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/ Spring-aop-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 "> <bean id=" sessionfactory "class=" Org.springframework.orm.hibernate3.LocalSessionFactoryBean "> <property name=" configlocation "> <value
	>classpath:com/config/hibernate.cfg.xml</value>	</property> <property name= "Mappinglocations" > <!--All entity class mapping 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 Spring Management DAO layer is configured 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.xsd
	HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/ Schema/aop/spring-aop-2.5.xsd ">
	
	<import resource=" Springconfig.xml "/>
	<!--authorized Administration DAO--
   <bean id= "Userdao" class= "Com.dao.UserDao" > 
         <property name= "hibernatetemplate" ref= " Hibernatetemplate "></property>
   </bean> 
</beans>
If you use hibernatetemplate, the DAO layer class is written as follows:
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);
	}
}

The spring injection is divided into four ways: Common is the Get/set method, the constructor, annotations, and the interface is not commonly used, you can refer to my previous blog "spring--Control Inversion", in which several ways to make a comparison.

Spring to manage struts,hibernate, commonly used is the annotation method, for the above use annotation way.

The DAO layer is configured 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.xsd
	HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/ Schema/aop/spring-aop-2.5.xsd ">
	<import resource=" Springconfig.xml "/>
	<!--authorized Administration DAO--
   <bean id= "Userdao" class= "Com.dao.UserDao" > </bean> 
</beans>

The DAO layer class is spelled as follows:

Package Com.dao;
Import Javax.annotation.Resource;
Import org.springframework.orm.hibernate3.HibernateTemplate;
Import Com.entity.User;
public class Userdao {
	@Resource
	private hibernatetemplate hibernatetemplate;
           public void Insert (user user) {
		hibernatetemplate.save (user);
	}
}

So this DAO layer class does not inherit, the direct use annotation method injects hibernatetemplate to be able.

Overall, I still like and use the injected Hibernatetemplate method in this project.

This access to the DAO layer is basically done, but if the access process goes wrong, it should be. or a DAO error in the process of accessing multiple DAO layers, is it not at this time that the other DAO layers stop at the same time. Oh, this piece is related to the management of affairs. Hibernate itself also provides transaction management, in which the transactions provided in Hibernate are automatically submitted, and there is no more intelligent autocommit. Oh, next blog, we will continue to explain the spring management of several ways of business.

PS: In order to facilitate everyone on the package of each frame clear, now the package is stored separately.

Download Address: 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.