今天使用Spring+Hibernate进行事务管理,按照顺序也就是配置,DataSource,Sessionfactory,事务管理器以及拦截器。

DateSource可以直接使用Hibernate的配置文件,也可以单独配置,但是却遇到了莫名其妙的问题.

操作步骤如下:

1,利用MyEclipse 2014,建立Java工程;

2,添加Spring库,此时创建了Spring的配置文件applicationContext.xml;

3,添加Hibernate库,此时工程已经识别了Spring的配置文件,可以选择自己建立一个Hibernate的配置文件,也可以不用建立;我选择没有创建。

4,单独创建Hibernate的配置文件(我想尝试两种方法配置)

5,连接数据库Mysql,利用Hibernate反向工程生成每个表的hibernate的配置文件。

6在applicationContext.xml中进行配置。

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=UTF-8" />
<property name="username" value="root" />
<property name="password" value="missyou" />
<!--maxActive: 最大连接数量 -->
<property name="maxActive" value="150" />
<!--minIdle: 最小空闲连接 -->
<property name="minIdle" value="5" />
<!--maxIdle: 最大空闲连接 -->
<property name="maxIdle" value="20" />
<!--initialSize: 初始化连接 -->
<property name="initialSize" value="30" />
<!-- 连接被泄露时是否打印 -->
<property name="logAbandoned" value="true" />
<!--removeAbandoned: 是否自动回收超时连接 -->
<property name="removeAbandoned" value="true" />
<!--removeAbandonedTimeout: 超时时间(以秒数为单位) -->
<property name="removeAbandonedTimeout" value="10" />
<!--maxWait: 超时等待时间以毫秒为单位 1000等于60秒 -->
<property name="maxWait" value="1000" />
<!-- 在空闲连接回收器线程运行期间休眠的时间值,以毫秒为单位. -->
<property name="timeBetweenEvictionRunsMillis" value="10000" />
<!-- 在每次空闲连接回收器线程(如果有)运行时检查的连接数量 -->
<property name="numTestsPerEvictionRun" value="10" />
<!-- 1000 * 60 * 30 连接在池中保持空闲而不被空闲连接回收器线程 -->
<property name="minEvictableIdleTimeMillis" value="10000" />
<property name="validationQuery" value="SELECT NOW() FROM DUAL" />
</bean>
<bean id="sessionFactoryt"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <!-- 也可以这样配 -->
<!-- <property name="configLocation"> <value>classpath:hibernate.cfg.xml</value>
</property> -->
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props> <prop key="hibernate.dialect">
org.hibernate.dialect.SQLServerDialect <!-- 数据库所用的sql语句 -->
</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop> <!--启用二级缓存 -->
<prop key="hibernate.cache.use_query_cache">false</prop> <!--是否启动查询缓存 -->
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop> <!--指定缓存类 --> </props>
</property>
<!-- <property name="packagesToScan" value="com.*" /> 为什么不起作用,别人的就可以--> <!-- <property name="mappingResources">
<list>
<value>com/entity/Admin.hbm.xml</value>
<value>com/entity/Userinfo.hbm.xml</value>
</list>
</property> --> <property name="mappingDirectoryLocations">
<list>
<value>com/entity</value>
</list>
</property>
</bean> <bean id="tm"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactoryt" />
</bean> <bean id="transactionInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager" ref="tm" />
<!-- 配置事务属性 -->
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean> <!-- 配置DAO -->
<bean id="userDao" class="com.dao.imp.UserDao">
<property name="sessionFactory" ref="sessionFactoryt" />
</bean>

当使用Hibernate的配置文件进行配置,没有错误:

  <!-- 也可以这样配 -->
<property name="configLocation"> <value>classpath:hibernate.cfg.xml</value>
</property>
但是使用dataSource进行配置,运行的时候,说找不到entity,很明显是没有找到,数据库中的表的配置文件,试了好几种方法,其中
appingResources和mappingDirectoryLocations可行,这两个的区别,前者是单独的写配置,后者是写的目录。
在网上也找到了packagesToScan属性,下载了别人的一个示例,奇怪的是别人的可以运行,我自己设置为自己的com.entity就不可行,原因还不知。

    <!--   <property name="packagesToScan" value="com.*" /> 为什么不起作用,别人的就可以-->

        <!-- <property name="mappingResources">
<list>
<value>com/entity/Admin.hbm.xml</value>
<value>com/entity/Userinfo.hbm.xml</value>
</list>
</property> --> <property name="mappingDirectoryLocations">
<list>
<value>com/entity</value>
</list>
</property>

Spring中的mappingResources和mappingDirectoryLocations的更多相关文章

  1. 在spring中映射X.hbm.xml文件的小技巧

    通常在spring中会这么写代码: <bean id="sessionFactory" class="org.springframework.orm.hiberna ...

  2. spring 中 hibernate 的 2种 配置方式(新旧 2种方式)

    Spring对hibernate配置文件hibernate.cfg.xml的集成,来取代hibernate.cfg.xml的配置 Spring对hibernate配置文件hibernate.cfg.x ...

  3. Spring中的FactoryBean

    从SessionFactory说起: 在使用SSH集成开发的时候,我们有时候会在applicationContext.xml中配置Hibernate的信息,以下是配置SessionFactory的一段 ...

  4. Spring中的c3p0配置

    转载请注明出处:http://blog.csdn.net/l1028386804/article/details/51162560 今天,我们就来详细谈谈Spring中的c3p0配置问题,好了,不耽搁 ...

  5. Spring中配置DataSource数据源的几种选择

    从JNDI获得DataSource. 从第三方的连接池获得DataSource. 使用DriverManagerDataSource获得DataSource. 一.从JNDI获得DataSource ...

  6. Velocity初探小结--Velocity在spring中的配置和使用

    最近正在做的项目前端使用了Velocity进行View层的数据渲染,之前没有接触过,草草过了一遍,就上手开始写,现在又回头细致的看了一遍,做个笔记. velocity是一种基于java的模板引擎技术, ...

  7. Spring中Bean的作用域、生命周期

                                   Bean的作用域.生命周期 Bean的作用域 Spring 3中为Bean定义了5中作用域,分别为singleton(单例).protot ...

  8. Spring中Bean的实例化

                                    Spring中Bean的实例化 在介绍Bean的三种实例化的方式之前,我们首先需要介绍一下什么是Bean,以及Bean的配置方式. 如果 ...

  9. 模拟实现Spring中的注解装配

    本文原创,地址为http://www.cnblogs.com/fengzheng/p/5037359.html 在Spring中,XML文件中的bean配置是实现Spring IOC的核心配置文件,在 ...

随机推荐

  1. django 更新model

    修改models.py 中对应的class 在admin.py 中 增加 admin.site.register(WafDevice) 进入dbshell python manage.py dbshe ...

  2. RethinkDB

    RethinkDB最早是作为一个对SSD进行专门优化的MySQL存储引擎出现的,其特点在于对SSD的充分利用.而目前RethinkDB已经脱离MySQL成为一个独立的存储. RethinkDB目前支持 ...

  3. 【rest】 深入理解rest

    起因是想搞明白 ajax.rest风格和http请求数据会有什么区别 再来回顾一下概念: REST即表述性 状态 传递 满足这些约束条件和原则的应用程序或设计就是RESTful.需要注意的是,REST ...

  4. IOS开发之──应用之间调用(1)

    iphone应用之间调用步骤: 1)在plist文件中,注册对外接口 在xcode group&files 里面,展开 resources选择<app>info.plist 鼠标右 ...

  5. ASP.NET MVC +EasyUI 权限设计(一)开篇

    在前一段时间中,老魏的确非常的忙碌,Blog基本上没有更新了,非常的抱歉,那么在后面的时间中,老魏会尽量的抽时间来写的,可能时间上就不太富裕了.今天开始呢,老魏会和大家分享一下关于权限设计的有关文章, ...

  6. WPF中使用MVVM模式进行简单的数据绑定

    计划慢慢整理自己在WPF学习和工作应用中的一些心得和想法,先从一个简单的用法说起 在WPF中,XAML标记语言中绑定数据,而数据源就是指定为ViewModel类,而非界面本身的逻辑代码类 这样一定程度 ...

  7. 【Leetcode】 - Single Number II

    Problem Discription: Suppose the array A has n items in which all of the numbers apear 3 times excep ...

  8. 1561:The more, The Better - hdu

    Problem DescriptionACboy很喜欢玩一种战略游戏,在一个地图上,有N座城堡,每座城堡都有一定的宝物,在每次游戏中ACboy允许攻克M个城堡并获得里面的宝物.但由于地理位置原因,有些 ...

  9. 2186: [Sdoi2008]沙拉公主的困惑 - BZOJ

    Description 大富翁国因为通货膨胀,以及假钞泛滥,政府决定推出一项新的政策:现有钞票编号范围为1到N的阶乘,但是,政府只发行编号与M!互质的钞票.房地产第一大户沙拉公主决定预测一下大富翁国现 ...

  10. C# Socket编程笔记(转)

    C# Socket编程笔记 http://www.cnblogs.com/stg609/archive/2008/11/15/1333889.html TCP Socket:Server 端连接步骤: ...