Spring+hibernate Integration

Source: Internet
Author: User
Tags aop generator

Is the DAO inherits the Hibernatetemplate class, which provides the Sessionfactory () method for injecting Sessionfactory

When you get DAO through spring, inject sessionfactory

Pojo class

1  PackageCom.how2java.pojo;2  3  Public classCategory {4  5      Public intgetId () {6         returnID;7     }8      Public voidSetId (intID) {9          This. ID =ID;Ten     } One      PublicString GetName () { A         returnname; -     } -      Public voidsetName (String name) { the          This. Name =name; -     } -     Private intID; -     PrivateString name; +}

HBM file

<?XML version= "1.0"?><!DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://www.hibernate.org /dtd/hibernate-mapping-3.0.dtd ">
<hibernate-mapping Package= "Com.how2java.pojo">
<classname= "Category"Table= "Category_"> <IDname= "id"column= "id"> <Generatorclass= "Native"> </Generator> </ID> < Propertyname= "Name" /> </class></hibernate-mapping>

DAO

DAO inherits Hibernatetemplete, and in the Hibernatetemplete class there is a setsessionfactory for the injection of sessionfactory

Package Com.how2java.dao;import Org.springframework.orm.hibernate3.hibernatetemplate;public Class CategoryDAO Extends hibernatetemplate{}

Writing Applicationcontext.xml

The sessionfactory is injected when the DAO is created, and the data source DS is injected when the sessionfactory is created

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"Xmlns:tx= "Http://www.springframework.org/schema/tx"Xmlns:context= "Http://www.springframework.org/schema/context"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/ Spring-aop-3.0.xsd Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/ Spring-tx-3.0.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/context/sp Ring-context-3.0.xsd "><!--configuring Bean components--<Beanname= "C"class= "Com.how2java.pojo.Category">        < Propertyname= "Name"value= "yyy" />    </Bean>        <Beanname= "DAO"class= "Com.how2java.dao.CategoryDAO">        < Propertyname= "Sessionfactory"ref= "SF" />    </Bean>    <Beanname= "SF"class= "Org.springframework.orm.hibernate3.LocalSessionFactoryBean">        < Propertyname= "DataSource"ref= "DS" />        < Propertyname= "Mappingresources">            <List>                <value>Com/how2java/pojo/category.hbm.xml</value>            </List>        </ Property>        < Propertyname= "Hibernateproperties">            <value>Hibernate.dialect=org.hibernate.dialect.mysqldialect Hibernate.show_sql=true Hbm2ddl.auto=update</value>        </ Property>    </Bean>                <Beanname= "DS"class= "Org.springframework.jdbc.datasource.DriverManagerDataSource">        < Propertyname= "Driverclassname"value= "Com.mysql.jdbc.Driver" />        < Propertyname= "url"value= "Jdbc:mysql://localhost:3306/how2java?characterencoding=utf-8" />        < Propertyname= "username"value= "root" />        < Propertyname= "Password"value= "Admin" />    </Bean>    </Beans>

Spring+hibernate Integration

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.