Using Spring integrated Hibernate

Source: Internet
Author: User

1<?xml version= "1.0" encoding= "UTF-8"?>2<beans xmlns= "Http://www.springframework.org/schema/beans"3Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xmlns:p= "http://www.springframework.org/schema/p"4xmlns:context= "Http://www.springframework.org/schema/context"5xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"6Xsi:schemalocation= "Http://www.springframework.org/schema/beans7http//www.springframework.org/schema/beans/spring-beans-3.2.xsd8http//Www.springframework.org/schema/context9http//www.springframework.org/schema/tx/spring-context-3.2.xsdTenhttp//WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP Onehttp//www.springframework.org/schema/aop/spring-aop-3.2.xsd "> A -<!--method One, direct configuration Hibernate.cfg.xml-- -<bean id= "Sessionfactory"
class= "Org.springframework.orm.hibernate3.LocalSessionFactoryBean" > the<property name= "configlocation" value= "Classpath:hibernate.cfg.xml" ></property> -</bean> -<!--method Two uses DataSource data source-- -<bean id= "DataSource"class= "Org.apache.commons.dbcp.BasicDataSource" > +<!--database Connection-- -<property name= "url" value= "Jdbc:oracle:thin: @localhost: 1521:jbit" ></property> +<property name= "username" value= "Rong" ></property> A<property name= "password" value= "Rong" ></property> at<property name= "Driverclassname" value= "Oracle.jdbc.driver.OracleDriver" >
</property> -</bean> -<bean id= "Sessionfactory"
class= "Org.springframework.orm.hibernate3.LocalSessionFactoryBean" > -<property name= "DataSource" ref= "DataSource" ></property> -<!--add configuration-- -<property name= "Hibernateproperties" > in<props> -<prop key= "dialect" >org.hibernate.dialect.OracleDialect</prop> to<prop key= "Show_sql" >true</prop> +<prop key= "Format_sql" >true</prop> -</props> the</property> *<!--relationship Mapping-- $<property name= "Mappingresource" >Panax Notoginseng<list> -<value>cn/bdqn/jboa/entity/CheckResult.hbm.xml</value> the<value>cn/bdqn/jboa/entity/ClaimVoucher.hbm.xml</value> +<value>cn/bdqn/jboa/entity/ClaimVoucherDetail.hbm.xml</value> A<value>cn/bdqn/jboa/entity/Department.hbm.xml</value> the<value>cn/bdqn/jboa/entity/Dictionary.hbm.xml</value> +<value>cn/bdqn/jboa/entity/Employee.hbm.xml</value> -<value>cn/bdqn/jboa/entity/Position.hbm.xml</value> $</list> $</property> -</bean> - the</beans>

The Hibernate.cfg.xml in Hibernate

1<?xml version= ' 1.0 ' encoding= ' UTF-8 '?>2<! DOCTYPE hibernate-Configuration Public3"-//hibernate/hibernate Configuration DTD 3.0//en"4"Http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" >56 7<session-factory>8<property name= "Connection.url" >9Jdbc:oracle:thin: @localhost: 1521: JbitTen</property> One<property name= "Connection.username" >rong</property> A<property name= "Connection.password" >rong</property> -<property name= "Connection.driver_class" > - Oracle.jdbc.driver.OracleDriver the</property> -<property name= "dialect" > - Org.hibernate.dialect.OracleDialect -</property> +<property name= "Show_sql" >true</property> -<property name= "Format_sql" >true</property> +<mapping resource= "Cn/bdqn/jboa/entity/checkresult.hbm.xml"/> A<mapping resource= "Cn/bdqn/jboa/entity/claimvoucher.hbm.xml"/> at<mapping resource= "Cn/bdqn/jboa/entity/claimvoucherdetail.hbm.xml"/> -<mapping resource= "Cn/bdqn/jboa/entity/department.hbm.xml"/> -<mapping resource= "Cn/bdqn/jboa/entity/dictionary.hbm.xml"/> -<mapping resource= "Cn/bdqn/jboa/entity/employee.hbm.xml"/> -<mapping resource= "Cn/bdqn/jboa/entity/position.hbm.xml"/> -</session-factory> in  -

After the change, it's easy to invoke the transaction

1  Public classUserdaoimplextendsHibernatedaosupportImplementsuserdao{2 3 @Override4      PublicEmployee FindByID (Serializable id) {5         //TODO auto-generated Method Stub6         return  This. Gethibernatetemplate (). Get (Employee.class, id);7     }8 9}

Creating an Interface class

1  Public Interface Claimvoucherdao {2      Public list<claimvoucher> Find (int First,int  pageSize); 3 }

Callback mechanism

1 /**2 * Reimbursement Form category3  * @authorAdministrator4  *5  */6  Public classClaimvoucherdaoimplextendsHibernatedaosupport
Implementsclaimvoucherdao{7 8@SuppressWarnings ("Unchecked")9 @OverrideTen PublicList<claimvoucher> Find (Final intFirstFinal intpageSize) { One A - return This. Gethibernatetemplate (). Executefind (NewHibernatecallback () { - the @Override - PublicObject doinhibernate (Session arg0) - throwshibernateexception, SQLException { - //TODO auto-generated Method Stub + returnArg0.createquery ("from Claimvoucher C") - . Setfirstresult (first) + . Setmaxresults (pageSize) A . List (); at } - }); - } -}

To add an instance of DAO in XML

1 <!--DAO--2     class= "Cn.bdqn.jboa.dao.UserDaoImpl" >3         < Property Name= "Sessionfactory" ref= "Sessionfactory" ></property>4     </bean>5     Class= "Cn.bdqn.jboa.dao.ClaimVoucherDaoImpl" >6         <property name= " Sessionfactory "ref=" Sessionfactory "></property>7     </bean>

Test class

 Public class Testclaim {    @Test    publicvoid  Test () {        new  Classpathxmlapplicationcontext (                "Applicationcontext.xml");        = (Claimvoucherdao) ctx.getbean ("Claimvoucherdao");        System.out.println (Claimvoucherdao.find (0, 3));        System.out.println (Claimvoucherdao.find (4, 3));}    }

Using Spring integrated Hibernate

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.