总结一下最近整合spring4和hibernate4遇到的问题

刚开始学习javaee,把自己遇到的主要问题总结一下,做个笔记,也希望给别人带来帮助,spring和hibernate均是>基于当前最新版本的上一个版本.4.3.7和4.3.11

遇到的问题:

配置文件问题

在配置hibernate的session工厂的时候,读取不到.hbm.xml文件的原因大致如下:

这是我的配置
<property name="mappingLocations" value="classpath:estate/entity/database/*.hbm.xml"/>
  1. 少加了classpath:
  2. 如果是用的maven进行项目的依赖管理的话,检查maven是否将资源文件放入了输出目录里面,即需要对pom.xml文件进行如下配置,(我是这个问题).
  <build>
        <finalName>test</finalName>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                    <include>**/*.tld</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                    <include>**/*.tld</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>

spring不能对hibernate进行事务管理:我百度后发现出现这个问题的原因很多
1. 没有在web.xml里面配置OpenSessionInViewFilter拦截器将请求的url和hibernate会话绑定在一起
2. 在配置spring-servlet扫描的报的时候将事务管理的包页扫描进去了,导致spring不能进行事务管理,(好像是新版本的问题)需要进行如下配置

<!-- 配置spring自动扫描的包,如果开启了事务管理,一定要过滤掉相应的类 -->
    <context:component-scan base-package="estate">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        <context:exclude-filter type="regex" expression=".*ServiceImpl"/>
    </context:component-scan>

代码编写问题

spring事务管理后抛出的异常不能再service和dao里面捕捉到
我的解决方案是在控制器里面加入捕捉异常的代码:

try
        {
            noticeService.add(noticeEntity);
        }
        catch (Exception e)
        {
            LogUtil.E("上传公告图片写入数据库失败");
            basicJson.getErrorMsg().setCode("100103");
            basicJson.getErrorMsg().setDescription("公告增加失败,请检查您的输入");
            return basicJson;
        }

google了一下,发现有其他的方法可以在service和dao里面捕捉异常,但是我觉得spring这样的设计比较合理,在业务逻辑层进行处理,而service只提供业务逻辑需要调用的方法.

参考

项目目录结构

这里写图片描述

配置文件

web.xml
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <!-- 加载spring上下文配置文件 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:config/applicationContext*.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- 将网站根目录写入系统属性 -->
    <context-param>
        <param-name>webAppRootKey</param-name>
        <param-value>WEB.ROOT</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.util.WebAppRootListener</listener-class>
    </listener>

    <!-- 配置spring拦截器 -->
    <servlet>
        <servlet-name>estateOA</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:config/spring-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>estateOA</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!-- 将url请求会话和hibernate会话绑定在一起,使得spring对hibernate可以进行事务管理 -->
    <filter>
        <filter-name>openSession</filter-name>
        <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
        <init-param>
            <param-name>si
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值