hibernate+spring的整合思路加实例

首先框架整合我感觉最难的是jar包的引入。因为不同框架的jar容易产生冲突。如果能排除这个因素我想说整合框架还是相对比较容易的。

我整合的框架的一个思想就是:各司其职。因为每个框架处理的事务或者是层次是不一样的。

也就说我们可以这么想。Hibernate就是操纵数据库的是持久层的。而spring就是利用ioc的bean对类进行实例化化的。Spring就是一个容器。

 

 

 

基于这个思想我想到的就是最传统的思想。我写一个往数据库里面添加记录的实例。

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

第一步:搭建hibernate环境(包括引入hibernate的jar,包配置数据源,建立类和表的映射),为什么这么做。我觉得hibernate是最重要的。因为没有spring不影响我往数据里面添加记录。Spring仅仅是一个容器。

第二步:配置spring的环境(引入jar和写一个spring.XML的配置信息)

第三步:在spring里面注册hibernate。

第四步:测试


接着就是写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映射类类名
<span style="white-space:pre">	</span><id name="id">
<span style="white-space:pre">	</span><generator class="native" />
<span style="white-space:pre">	</span></id>
<property name="name"></property>
<span style="white-space:pre">		</span><property name="passwrod"></property>
<span style="font-family: 宋体;"></class></span>
</hibernate-mapping>

当然我们写对应的person映射类类


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类的。而且往里面设置了值,就是以后网表中的数据,如果能成功数据库就会显示、tom和330127

 

    <bean id="sessionfactroy"

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

        <property name="configLocation" value="hibernate.cfg.xml"></property>     //这个bean是spring内部的一个类                         // LocalSessionFactoryBean这个类实例化我们可以得到sessionfactory。该类中有个属性configLocation通过这个属性我们就可以hibernate.cfg.xml建立联系了。

 

    </bean>
</beans>

下面我们来一个测试类。

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();
    }

}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值