Spring3 整合Hibernate4

Spring3 整合Hibernate4

注意事项:

1. 导入jar包 参考工程中的,其中需要jaxen-1.1-beta-6.jar和commons-logging-api.jar,

   commons-logging-1.1.1.jar比较特殊

2. Hibernate4 中dao组件不需要在继承HibernateDaoSupport了.

   可以在Dao组件中直接注入sessionFactory.然后调用

   //Session session = sessionFactory.getCurrentSession();

   Session session = sessionFactory.openSession(); 

   openSession()方法,注意不是getCurrentSession();方法.

扫描二维码关注公众号,回复: 700037 查看本文章

   否则报异常:java.lang.NoSuchMethodError异常

   参考:http://www.cnblogs.com/lihuiyy/archive/2013/03/21/2972641.html

   (hibernate4 和 spring3 整合注意事项 否则java.lang.NoSuchMethodError异常)

   得到sessionFactory后,可以new 一个(或者由Spring注入一个)HIbernateTemplate.

   

3. 配置hibernate属性的时候

   <!--配置Hibernate的属性--> 

             <property name="hibernateProperties"> 

                <value> 

                  hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect

                  hibernate.hbm2ddl.auto=update

                  hibernate.show_sql=true

                  hibernate.format_sql=true

                </value> 

             </property>

             

    注意上面的格式,一行一个属性

    hibernate.hbm2ddl.auto=update必须配置为update,配置为create报异常(表不存在).不知道什么原因.

    这里,只能先准备好数据库的表,然后配置为update.

    

 4. hibernate 4.0 缺少CacheProvider类

    CacheProvider从hibernate 3.3的时候就不建议使用了,这次只不过是在4.0里面删掉了。

    sessionFactory的Bean配置如下:

    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

    注意class属性中的 hibernate4

    参考:http://blog.csdn.net/geekjoker/article/details/7899890

    

5. 调用load时,id的类型要实现配置好.比如id是int型的,则在定义持久化类时也要定义为int的.

6. Spring配置文件

<?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"
        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" 
> 
           <!-- 1. 配置datasource -->
           <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
                <!-- 指定连接数据库的驱动 -->
                <property name="driverClass" value="com.mysql.jdbc.Driver"/>
                <!-- 指定连接数据库的URL -->
                <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/hibernate?useunicode=true&amp;characterEncoding=utf8"/>
                <!-- 指定连接数据库的用户名 -->
                <property name="user" value="root"/>
                <!-- 指定连接数据库的密码 -->
                <property name="password" value="root"/>
                <!-- 指定连接数据库连接池的最大连接数 -->
                <property name="maxPoolSize" value="40"/>
                <!-- 指定连接数据库连接池的最小连接数 -->
                <property name="minPoolSize" value="1"/>
                <!-- 指定连接数据库连接池的初始化连接数 -->
                <property name="initialPoolSize" value="1"/>
                <!-- 指定连接数据库连接池的连接的最大空闲时间 -->
                <property name="maxIdleTime" value="20"/>
                
           </bean>
           
           <!-- 2. 配置sessionFactory -->
           <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
             <!--dataSource属性指定要用到的连接池--> 
             <property name="dataSource" ref="dataSource"/> 
           
             <!--指定要用到的实体映射文件--> 
             <property name="mappingResources"> 
                <list> 
                  <value>com/test/hibernate/mapping/News.hbm.xml</value> 
                </list> 
             </property> 
           
             <!--配置Hibernate的属性--> 
             <property name="hibernateProperties"> 
                <value> 
                  hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect
                  hibernate.hbm2ddl.auto=update
                  hibernate.show_sql=true
                  hibernate.format_sql=true
                </value> 
             </property>
           </bean> 
           
           <!-- 3. 配置HibernateTemplate,用于完成数据操作 -->
           <bean id="hibernateTemplete" class="org.springframework.orm.hibernate3.HibernateTemplate">
             <property name="sessionFactory" ref="sessionFactory"></property>
           </bean>
           
           <!-- 4. 配置Dao层 Bean,该Dao继承自HibernateDaoSupport,需要注入HibernateTemplate -->
           <bean id="newsDao" class="test.hibernate.pojo.dao.NewsDao">
             <property name="sessionFactory" ref="sessionFactory"></property>
           </bean>
           
           <!-- 5. 配置service业务逻辑层Bean,需要注入Dao层Bean -->
           <bean id="newsService" class="test.hibernate.pojo.dao.service.NewsService">
             <property name="newsDao" ref="newsDao"></property>
           </bean>
           
</beans> 

猜你喜欢

转载自jackyin5918.iteye.com/blog/1935486