Spring Integrated Hibernate XML configuration

Source: Internet
Author: User

Spring Integration Hibernate:

Hibernate: Interacting with the database

SPRING:IOC AOP

Integration points:

1.sessionFactory objects are no longer generated by hibernate and are generated by spring, that is, the configuration of the database connection information global configuration map file is done by spring

2.IOC managing DAO Objects Basedao objects

Control of 3.AOP transactions

Steps:

1. General Engineering Copy JAR package

2. Configure Applicationcontext-resource.xml

Configuring the Sessionfactory configuration Datasourse

Database connection Configuration The configuration property name of the global configuration map file is derived from the underlying attribute, fixed notation

3. How to write the PO and the mapping file before how to write it now

4. Write DAO DAO inside must have Sessionfactory attribute

5. Configure Applicationcontext-dao.xml

6. Test can

Po

Public class Students {
private Integer stuid;
Private String Stuname;
Private String Email;
Private Integer age;
Private Integer sex;
Private Integer CID;  
Public Integer Getstuid () {
return stuid;
  
public void Setstuid (Integer stuid) {
This.stuid = Stuid;
  
Public String Getstuname () {
return stuname;
  
public void Setstuname (String stuname) {
This.stuname = stuname;
  
Public String Getemail () {
return email;
  
public void Setemail (String email) {
This.email = email;
  
Public Integer Getage () {
return age;
  
public void Setage (Integer age) {
This.age = age;
  
Public Integer Getsex () {
return sex;
  
public void Setsex (Integer sex) {
This.sex = sex;
  
Public Integer getcid () {
return cid;
  
public void Setcid (Integer cid) {
This.cid = CID;
}
}

Students.hbm.xml:

<?xml version= "1.0"?>
<! DOCTYPE hibernate-mapping Public
"-//hibernate/hibernate Mapping DTD 3.0//en"
"Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
Package= "Com.huawei.sh.po" >
<class name= "Students" table= "test_students" >
<id name= "Stuid" column= "stu_id" >
<generator class= "Sequence" >
<param name= "sequence" >seq_test_students</param>
</generator>
</id>
<property name= "Stuname" column= "Stu_name"/>
<property name= "Email"/>
<property name= "Age"/>
<property name= "Sex"/>
<property name= "CID"/>
</class>

Dao

Public class Studentsdao {
private sessionfactory sessionfactory;
    public void Addstudents (Students stu) {
Session session = Sessionfactory.opensession ();
Transaction TX =session.begintransaction ();
Session.save (STU);
Tx.commit ();
Session.close ();  
}
Public sessionfactory getsessionfactory () {
return sessionfactory;
  
public void Setsessionfactory (Sessionfactory sessionfactory) {
This.sessionfactory = sessionfactory;
    
public static void Main (string[] args) {
Students stu = new Students ();
Stu.setstuname ("Zhang San");
Stu.setemail ("[email protected]");
String[] res = new string[]{"Applicationcontext-dao.xml", "Applicationcontext-resource.xml"};
ApplicationContext context = new Classpathxmlapplicationcontext (res);
Studentsdao Studao = (Studentsdao) context.getbean ("Studentsdao");
Studao.addstudents (STU);
}
}

Applicationcontext-dao.xml:

<?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:p= "http://www.springframework.org/schema/p"
Xsi:schemalocation= "Http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans-3.2.xsd ">
<bean id= "Studentsdao" class= "Com.huawei.sh.dao.StudentsDao" >
<property name= "Sessionfactory" ref= "Sessionfactory"/>
</bean>
</beans>

Applicationcontext-resource.xml:

<?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: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.2.xsd
Http://www.springframework.org/schema/tx
Http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
Http://www.springframework.org/schema/aop
Http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
Http://www.springframework.org/schema/context
Http://www.springframework.org/schema/context/spring-context-3.2.xsd ">
<!--<context:component-scan base-package= "COM.CHDSXT"/>
<aop:aspectj-autoproxy/> Support for AOP annotation methods use the annotation scheme must write these two items--
<bean id= "DataSource" class= "Org.apache.commons.dbcp.BasicDataSource" >
<property name= "Driverclassname" value= "Oracle.jdbc.driver.OracleDriver"/>
<property name= "url" value= "Jdbc:oracle:thin: @localhost: 1521:orcl"/>
<property name= "username" value= "Scott"/>
<property name= "password" value= "Tiger"/>
</bean>
<!--database Core configuration--
<bean id= "Sessionfactory" class= "Org.springframework.orm.hibernate4.LocalSessionFactoryBean" >
<!--database Connection configuration--
<property name= "DataSource" ref= "DataSource"/>
<!--global configuration--
<property name= "Hibernateproperties" >
<props>
<prop key= "Hibernate.dialect" >org.hibernate.dialect.Oracle10gDialect</prop>
<prop key= "Hibernate.show_sql" >true</prop>
<prop key= "Hibernate.format_sql" >true</prop>
<prop key= "Hibernate.hbm2ddl.auto" >update</prop>
</props>
</property>
<!--mapping File configuration--
<property name= "Mappingresources" >
<list>
<value>com/huawei/sh/po/Students.hbm.xml</value>
</list>
</property>
</bean>
</beans>

Spring Integrated Hibernate XML configuration

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.