hibernate+spring的整合思路加實例(配圖解)


首先框架整合我感覺最難的是jar包的引入。因為不同框架的jar容易產生沖突。如果能排除這個因素我想說整合框架還是相對比較容易的。

我整合的框架的一個思想就是:各司其職。因為每個框架處理的事務或者是層次是不一樣的。

也就說我們可以這么想。Hibernate就是操縱數據庫的是持久層的。而spring就是利用iocbean對類進行實例化化的。Spring就是一個容器。

 

 

基於這個思想我想到的就是最傳統的思想。我寫一個往數據庫里面添加記錄的實例。

所以我整合的步驟就是四步

第一步:搭建hibernate環境(包括引入hibernatejar,包配置數據源,建立類和表的映射),為什么這么做。我覺得hibernate是最重要的。因為沒有spring不影響我往數據里面添加記錄。Spring僅僅是一個容器。

第二步:配置spring的環境(引入jar和寫一個spring.XML的配置信息)

第三步:在spring里面注冊hibernate

第四步:測試

 

那么我們按照這個步驟。(我的是spring3.2hibernate3.2這里兩個版本一起很少見。但是只要里面jar不沖突是一點問題沒有)

首先我們引入所有jar包:這里我就順便把springhibernate 的全引進去了。

 

 

接着就是寫hibernate.cgf.xml

 

<!DOCTYPE hibernate-configuration PUBLIC

    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"

    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

 

<hibernate-configuration>

    <session-factory name="foo">

        <property name="myeclipse.connection.profile">mysql</property>

        <property name="connection.url">

            jdbc:mysql://127.0.0.1:3306/test

        </property>

        <property name="connection.username">root</property>

        <property name="connection.password">330127</property>

        <property name="connection.driver_class">

            com.mysql.jdbc.Driver

        </property>

        <property name="dialect">

            org.hibernate.dialect.MySQLDialect

        </property>

        <property name="hbm2ddl.auto">update</property>//上面都是配置數據源,和本級數據庫建立連接的。

        <mapping resource="com/fish/dao/Person.hbm.xml" />//把映射表xml往這里注冊。

    </session-factory>

</hibernate-configuration>

 

那么我們寫Person.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">

<hibernate-mapping package="com.fish.dao">

    <class  name="Person">//person映射類類名

 

        <id name="id">

            <generator class="native" />

        </id>

        <property name="name"></property>

        <property name="passwrod"></property>

    </class>

</hibernate-mapping>

當然我們寫對應的person映射類類

package com.fish.dao;

 

publicclass Person {

String name;

String passwrod;

Integer id;

public String getName() {

    returnname;

}

publicvoid setName(String name) {

    this.name = name;

}

public String getPasswrod() {

    returnpasswrod;

}

publicvoid setPasswrod(String passwrod) {

    this.passwrod = passwrod;

}

public Integer getId() {

    returnid;

}

publicvoid setId(Integer id) {

    this.id = id;

}

 

 

}

 

 

上面是對hibernate搭建。

 

接着我們來配置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:context="http://www.springframework.org/schema/context"

    xsi:schemaLocation="

        http://www.springframework.org/schema/beans

        http://www.springframework.org/schema/beans/spring-beans.xsd

        http://www.springframework.org/schema/context

        http://www.springframework.org/schema/context/spring-context.xsd">

 

 

 

    <bean id="person" class="com.fish.dao.Person">

        <property name="name" value="tom"></property>

        <property name="passwrod" value="330127"></property>

    </bean>   //bean是初始化person類的。而且往里面設置了值,就是以后網表中的數據,如果能成功數據庫就會顯示、tom330127

 

    <bean id="sessionfactroy"

        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

        <property name="configLocation" value="hibernate.cfg.xml"></property>     //這個beanspring內部的一個類                         // LocalSessionFactoryBean這個類實例化我們可以得到sessionfactory。該類中有個屬性configLocation通過這個屬性我們就可以hibernate.cfg.xml建立聯系了。

 

    </bean>

 

 

 

 

</beans>

下面我們來一個測試類。

package com.fish.dao;

 

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.Transaction;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

 

publicclass Test {

    publicstaticvoid main(String[] args) {

        // Configuration configuration=new Configuration();

        // configuration.configure("hibernate.cfg.xml");

        //

        // SessionFactory factory=configuration.buildSessionFactory();

        // Session session=factory.openSession();

        //

        // Person person=new Person();

        // person.setName("yaku");

        // person.setPasswrod("123456");

        // Transaction transaction=session.beginTransaction();

        // session.save(person);

        // transaction.commit();

        // session.close();

上面注釋的是我們沒有加入springhibernate的測試。我們可以對比下面,看看spring給我做了什么;

        ApplicationContext context = new ClassPathXmlApplicationContext(

                "spring.xml");  

        SessionFactory factory = (SessionFactory) context   

                .getBean("sessionfactroy");   //spring直接幫我們加載了hibernate.cgf.xml文件,讓我們直接操作了sessionfactory。其實下面的事務管理我們也可以通過spring的來管理的。但是由於沒寫一個一個代理類所以就沒寫。

        Session session = factory.openSession();

        Transaction transaction = session.beginTransaction();

        Person person = (Person) context.getBean("person");

        session.save(person);

        transaction.commit();

        session.close();

 

    }

}

 其實還有一種關聯spring和hibernate的方法那就是將hibernate.cgx.XML放到spring來注冊數據源,這樣可以省去寫hibernate.cgx.XML文件,但是這個個人感覺會給spring文件造成臃腫,所以我用的是分開的這種。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM