spring4.3.7整合hibernate5.2.9问题

http://www.jianshu.com/p/153dd3960b40 如何下载jar
去官网下好spring的包和hibernate的包,另外导入common-logging的日志包。
commons-pool和dbcp,mysql的驱动包

1、spring最初注解注入失败,原因是下面这句话的包路径不对
(使用的是jsr250注解,学习的时候看的马士兵的老视频需要去下载jsr250的包,新版本是不需要的,以致方向错误解决了好久。)
使用

<!-- <context:annotation-config/> 

这个的时候报错
改为下面(特别注意包的路劲和配置的路径)

<context:component-scan base-package="com.spring.*" />
    <context:property-placeholder location="classpath:jdbc_hibernate.properties,classpath:druid.properties" />

以及xml的路劲不对,xml直接放在src下可以这样下,但是如果放在包下需要些完整(如classpath:*/beans.xml)

ApplicationContext context = new ClassPathXmlApplicationContext( "beans.xml");
UserManager mgr = context.getBean("mgr", UserManager.class);
mgr.save();

2、整合hibernate失败
(1)sessionFactory注入失败,一直以为是配置错误或者包的版本不匹配
因为下的最新的hibernate5以上的包,在官网只看到core包。以为那些注解和基础依赖包都整合到core包里面了之引入了core包,导致的失败。其实官网还有个路径可以下载全部的jar包仔细找。

09B2FBCC-FE3A-474F-B96E-6E164E41E1B8.png

(2)导入后注解还是失败,试了一下xml方式成功了。以为是配制问题。

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

这里的hibernate4.LocalSessionFactoryBean以前的注解配置用的

hibernate3.Annotation.AnnotationSessionFactoryBean

这个类,现在不需要了上面就可以。其实只是基础不好,不了解错误原因。

最后xml成功,注解失败后,总是提示语法错误,其实提示的是主键不匹配开始一直没查到原因弄了好久。后来发现是主键生成策略和数据库不匹配,没有插入主键自增方式导致失败。去掉实体类的自增方式,手动插入主键值成功。

一直被视频中的 jpa和jsr250纠结,其实失败跟导入这个包没有关系。仔细看jar包,内部有jpa的包和jsr250的其实不需要额外导入
附上架包图 spring中有几个jar没用到,暂时全部导入的

31FB1BC5-239B-4F15-9ED2-26C1AF1B39C8.png

上面操作已经成功的整合hibernate的sessionFactory可以正常使用

接着使用hibernateTemplate出错总是提示没有事务处理,报错。
然后照着文档和百度资料配好事务,报错下面信息,提示没有aspectjweaver.jar包,下载导入错误提示消失

could not find class that it depends on; nested exception is java.lang.NoClassDefFoundError: org/aspectj/weaver/reflect/ReflectionWorld$ReflectionWorldException

中间报错也报错事务配置的路径不对,修改好正常;但是还是提示没有事务操作不成功。
最后百度事务配置找到文章
http://www.blogjava.net/robbie/archive/2009/04/05/264003.html
我template注入的是sessionFactory但是事务配的是

<bean id="hibernateTemplate"   class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="dataSource">    </property>
</bean>
修改成
<bean id="transactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
</bean>

后正常。

中间也猜测template写法问题,最后写法是

public class UserDao  {
    SessionFactory sessionFactory;
    HibernateTemplate hibernateTemplate;

    @Resource
    public void setSessionFactory(SessionFactory sessionFactory) {
        hibernateTemplate = new HibernateTemplate(sessionFactory);
    }
  
    public void save(User u) {
        this.hibernateTemplate.save(u);
    }
}

配置文件

<?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"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-4.0.xsd 
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
    ">

    <!-- 载入常用的配置 -->
    <context:property-placeholder location="classpath:jdbc_hibernate.properties,classpath:druid.properties" />

    <!-- 注解扫描所有包 -->
    <context:component-scan base-package="com.spring.*" />

    <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
          <property name="driverClassName" value="${jdbc.driverClassName}"/>
          <property name="url" value="${jdbc.url}"/>
          <property name="username" value="${jdbc.username}"/>
          <property name="password" value="${jdbc.password}"/>
    </bean>

    <!-- 创建sessionFactory -->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.show_sql">
                    ${hibernate.show_sql}
                </prop>
                <prop key="hibernate.use_sql_comments">
                    ${hibernate.use_sql_comments}
                </prop>
                <prop key="hibernate.dialect">
                    ${hibernate.dialect}
                </prop>
                <prop key="hibernate.jdbc.fetch_size">
                    ${hibernate.jdbc.fetch_size}
                </prop>
                <prop key="hibernate.jdbc.batch_size">
                    ${hibernate.jdbc.batch_size}
                </prop>
                <prop key="hibernate.use_outer_join">
                    ${hibernate.use_outer_join}
                </prop>
            </props>
        </property>
        <property name="dataSource">
            <ref bean="dataSource" />
        </property>
        
        <!--  
        <property name="mappingResources">    
            <value>com/spring/model/User.hbm.xml </value>    
        </property>  
        -->

        <!-- 自动扫描注解方式配置的hibernate类文件 -->
        <!-- 
        <property name="packagesToScan">
            <list>
                <value>com.spring.model</value>
            </list>
        </property>
         -->
         <property name="packagesToScan">
            <list>
                <value>com.spring.model</value>
            </list>
        </property>
        
        <!--  <property name="annotatedClasses">
            <list>
                <value>com.spring.model.User</value>
            </list>
        </propert > -->
        
    </bean>
    
    <!-- 配置Jdbc模板 -->
    <!-- <bean id="hibernateTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean> -->
    <!-- Hibernate Template Bean配置 -->
    <!-- <bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean> -->
    
    <!-- 事务配置 start -->
    <!-- <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource">
        </property>
    </bean>

    <tx:advice id="txadvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="save*" propagation="REQUIRED" />
            <tx:method name="delete*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="transfer*" propagation="REQUIRED" />
            <tx:method name="upgrade*" propagation="REQUIRED" />
            <tx:method name="change*" propagation="REQUIRED" />
            <tx:method name="do*" propagation="REQUIRED" />
            <tx:method name="*" propagation="REQUIRED" read-only="true" />
        </tx:attributes>
    </tx:advice>
    <aop:config>
        <aop:pointcut expression="execution(* com.spring.manager.save(..))"
            id="txpointcut" />
        <aop:advisor advice-ref="txadvice" pointcut-ref="txpointcut" />
    </aop:config> -->
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <tx:advice id="txadvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="save" propagation="REQUIRED" />
            <tx:method name="delete*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="transfer*" propagation="REQUIRED" />
            <tx:method name="upgrade*" propagation="REQUIRED" />
            <tx:method name="change*" propagation="REQUIRED" />
            <tx:method name="do*" propagation="REQUIRED" />
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>
    <aop:config>
        <aop:pointcut expression="execution(* com.spring.manager.*.*(..))"
            id="txpointcut" />
        <aop:advisor advice-ref="txadvice" pointcut-ref="txpointcut" />
    </aop:config>

    <!-- 事务配置 end -->
        <!-- 强制使用cglib代理 -->
    <!--<aop:aspectj-autoproxy proxy-target-class="true" />  -->
    </beans>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,974评论 6 342
  • 本文包括: 1、CRM 项目的整体介绍 2、Hibernate 框架概述 3、Hibernate 快速入门 4、H...
    廖少少阅读 3,485评论 9 66
  • hibernate(20170731) 1.导包:hibernate-distribution-3.5.6-Fin...
    潇湘雨smile阅读 570评论 0 0
  • 1. 简介 1.1 什么是 MyBatis ? MyBatis 是支持定制化 SQL、存储过程以及高级映射的优秀的...
    笨鸟慢飞阅读 5,700评论 0 4
  • 2017-03-24 获嘉县第一初级中学 ——获嘉县第一初级中学举行课题结题研讨会 2017年3月23日下午,获嘉...
    郑素梅阅读 1,333评论 0 2