Implementing a transaction persistence layer using Hibernate and Spring

Source: Internet
Author: User
Tags aop implement interface key sql mysql return

1. First implement an interface.

============================================================

Package Com.cqtele.tnbos;import java.util.*;p ublic interface Iuserdao {public void Insertuser (userInfo user); public Lis T Finduser (String SQL);

==============================================================

2. Implement this interface.

This simplifies the processing of Hibernate sessions using Spring's Aop-driven hibernatetemplate.

================================================================package Com.cqtele.tnbos;import java.util.*;

Import Net.sf.hibernate.*;import Net.sf.hibernate.cfg.*;import Java.io.serializable;import org.springframework.orm.hibernate.*;//. Hibernatecallback;import Org.springframework.orm.hibernate.support.HibernateDaoSupport;

public class Userdao extends Hibernatedaosupport implements iuserdao{//public List name=null; List List=null;  public void Insertuser (UserInfo user) {gethibernatetemplate (). saveorupdate (user);} public List Finduser (String sql) {  Return Gethibernatetemplate (). find (SQL); }}

=================================================================

3. Declarative management transactions.

Bean.xml

==========================================================================

<?xml version= "1.0" encoding= "UTF-8"?> <! DOCTYPE beans Public "-//spring/dtd bean/en" "Http://www.springframework.org/dtd/spring-beans.dtd" >

<beans> <bean id= "DataSource" class= "Org.apache.commons.dbcp.BasicDataSource"    Destroy-method= "Close" >  <property name= "Driverclassname" >   <value> Com.mysql.jdbc.driver</value>  </property>  <property name= "url" >    <value>jdbc:mysql://127.0.0.1:3306/opertselect?useUnicode=true&characterEncoding=GBK< /value>  </property>   <property name= "username" >   < Value>root</value>  </property>   <property name= "Password" >    <value></value>  </property> </bean>  < Bean id= "sessionfactory" class= "Org.springframework.orm.hibernate.LocalSessionFactoryBean" >  < Property Name= "DataSource" >   <ref local= "DataSource"/>  </property>   <propertyName= "Mappingresources" >   <list>    <value>userinfo.hbm.xml </value>    <!--value>fruits.hbm.xml</value-->   </ List>  </property>  <property name= "Hibernateproperties" >    <props>    <prop key= "Hibernate.dialect" >      Net.sf.hibernate.dialect.mysqldialect    </prop>    <prop key= "Hibernate.show_sql" >true</prop>   </props>  </property>  </bean>   <bean id= "TransactionManager" class= " Org.springframework.orm.hibernate.HibernateTransactionManager ">  <property name=" sessionfactory ">   <ref local=" Sessionfactory "/>  </property> </bean>  <bean id= "Userdao" class= "Com.cqtele.tnbos.userDAO" &GT;  <property name= "sessionfactory" >   <ref local= "SessionFactory"/>   </property> </bean> <bean id= "Daoproxy" class= " Org.springframework.transaction.interceptor.TransactionProxyFactoryBean ">  <property name=" TransactionManager ">   <ref bean=" TransactionManager "/>  </property>   <property name= "target" >   <ref local= "Userdao"/>  </ Property>  <property name= "Transactionattributes" >   <props>     <prop key= "insert*" >propagation_required</prop>    <prop key= "get*" >propagation_required,readonly</prop>   </props>  </ property> </bean></beans>================================================================= =============

4. Use. (This is an example of a shopping cart.) ==============================================================================

Package Com.cqtele.tnbos;

Import Java.util.*;import Java.io.*;import Org.springframework.beans.factory.beanfactory;import Org.springframework.beans.factory.xml.xmlbeanfactory;import Org.aopalliance.aop.Advice;

public class login{Private List list=null, private String sql= "from UserInfo";   Public List getallmessagelist () {try{InputStream is = new FileInputStream ("Bean.xml");   Xmlbeanfactory factory = new Xmlbeanfactory (IS);   Iuserdao user = (Iuserdao) factory.getbean ("Daoproxy");  List = User.finduser (SQL); }catch (IOException IoE) {System.out.println ("Getallmessagelist Ocurr error!!  Message: "+ioe.getmessage ()); } return list; }}

======================================================================

5. Summary

======================================================================

Hibernate is an advanced OR mapping tool, and Spring is an AOP framework and an IOC container. The combination of these two technologies allows developers to write code comparable to database vendors, which can be run in the Java EE container or run separately.


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.