Spring+Hibernate整合

Posted fionalde

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring+Hibernate整合相关的知识,希望对你有一定的参考价值。

是DAO继承HibernateTemplate这个类,该类提供了sessionFactory()方法用于注入SessionFactory

通过spring获取DAO时,注入SessionFactory

POJO类

 1 package com.how2java.pojo;
 2  
 3 public class Category {
 4  
 5     public int getId() {
 6         return id;
 7     }
 8     public void setId(int id) {
 9         this.id = id;
10     }
11     public String getName() {
12         return name;
13     }
14     public void setName(String name) {
15         this.name = name;
16     }
17     private int id;
18     private String name;
19 }

hbm文件

<?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">
  <class name="Category" table="category_"> <id name="id" column="id"> <generator class="native"> </generator> </id> <property name="name" /> </class> </hibernate-mapping>

DAO

DAO继承HibernateTemplete,在HibernateTemplete类中有一个setSessionFactory用于就收sessionFactory的注入

package com.how2java.dao;

import org.springframework.orm.hibernate3.HibernateTemplate;

public class CategoryDAO extends HibernateTemplate{

}

编写applicationContext.xml

创建dao时候会注入sessionfactory,创建sessionFactory的时候会注入数据源ds

<?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.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/spring-context-3.0.xsd">
   <!--配置bean组件-->
    <bean name="c" class="com.how2java.pojo.Category">
        <property name="name" value="yyy" />
    </bean>
    
    <bean name="dao" class="com.how2java.dao.CategoryDAO">
        <property name="sessionFactory" ref="sf" />
    </bean>

    <bean name="sf"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="ds" />
        <property name="mappingResources">
            <list>
                <value>com/how2java/pojo/Category.hbm.xml</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <value>
                hibernate.dialect=org.hibernate.dialect.mysqlDialect
                hibernate.show_sql=true
                hbm2ddl.auto=update
               </value>
        </property>
    </bean>    
        
    <bean name="ds"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/how2java?characterEncoding=UTF-8" />
        <property name="username" value="root" />
        <property name="password" value="admin" />
    </bean>    

</beans>

 



以上是关于Spring+Hibernate整合的主要内容,如果未能解决你的问题,请参考以下文章

spring mvc+spring + hibernate 整合

Spring 整合Hibernate

10.Spring整合Hibernate_3_HibernateTemplate

Spring 整合 Hibernate 时启用二级缓存实例详解

Spring整合Hibernate实现Spring Data JPA (简单使用)

Spring整合Hibernate详细步骤