javaEE中的hibernate配置笔记
0 从web.xml出发
项目中用Spring整合Hibernate,Spring贯穿整个项目,所以先看看Spring在哪一步整合了Hibernate。先看部分web.xml。
在context-param设定参数contextConfigLocation。
在listener配置基于Web上下文级别的监听器ContextLoaderListener,它会加载contextConfigLocation中的配置文件。
其中最重要的是spring-hibernate.xml,这里面配置了hibernate。
<!-- 指定spring相关文件的位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml,classpath:spring-ehcache.xml,classpath:spring-hibernate.xml</param-value>
</context-param> <!-- 开启spring功能 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
1 spring-hibernate.xml
1.1 配置数据源
<bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="url" value="${jdbc_url}" />
<property name="username" value="${jdbc_username}" />
<property name="password" value="${jdbc_password}" /> <!-- 初始化连接大小 -->
<property name="initialSize" value="0" />
<!-- 连接池最大使用连接数量 -->
<property name="maxActive" value="20" />
<!-- 连接池最小空闲 -->
<property name="minIdle" value="0" />
<!-- 获取连接最大等待时间 -->
<property name="maxWait" value="60000" /> <!-- <property name="poolPreparedStatements" value="true" /> <property name="maxPoolPreparedStatementPerConnectionSize" value="33" /> --> <property name="validationQuery" value="${validationQuery}" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<property name="testWhileIdle" value="true" /> <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="25200000" /> <!-- 打开removeAbandoned功能 -->
<property name="removeAbandoned" value="true" />
<!-- 1800秒,也就是30分钟 -->
<property name="removeAbandonedTimeout" value="1800" />
<!-- 关闭abanded连接时输出错误日志 -->
<property name="logAbandoned" value="true" /> <!-- 监控数据库 -->
<!-- <property name="filters" value="mergeStat" /> -->
<property name="filters" value="stat" />
</bean>
1.2 配置hibernate session工厂
<!-- 配置hibernate session工厂 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<prop key="hibernate.use_sql_comments">${hibernate.use_sql_comments}</prop>
</props>
</property> <!-- 自动扫描注解方式配置的hibernate类文件 -->
<property name="packagesToScan">
<list>
<value>light.mvc.model</value>
</list>
</property> </bean>
1.3 配置事务管理(AOP)
<!-- 配置事务管理器 -->
<bean name="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean> <!-- 拦截器方式配置事物 -->
<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="append*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="modify*" propagation="REQUIRED" />
<tx:method name="edit*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="init" propagation="REQUIRED" />
<tx:method name="delAndInit" propagation="REQUIRED" /> <tx:method name="get*" propagation="REQUIRED" read-only="true" />
<tx:method name="find*" propagation="REQUIRED" read-only="true" />
<tx:method name="load*" propagation="REQUIRED" read-only="true" />
<tx:method name="search*" propagation="REQUIRED" read-only="true" />
<tx:method name="datagrid*" propagation="REQUIRED" read-only="true" /> <tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice> <aop:config>
<aop:pointcut id="transactionPointcut" expression="execution(* light.mvc.service..*Impl.*(..))" />
<aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" />
</aop:config>
2 spring-hibernate.xml
spring.xml中加载了一些数据库参数,参数在config.properties文件中进行设置,比如jdbc_url、jdbc_username、jdbc_password这些参数。
同时扫描service组件和dao组件实现自动注入。
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:config.properties</value>
</list>
</property>
</bean>
<!-- 自动扫描repository和service包(自动注入) -->
<context:component-scan base-package="light.mvc.dao,light.mvc.service" />
3 config.properties文件
hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
driverClassName=com.mysql.jdbc.Driver
validationQuery=SELECT 1
jdbc_url=jdbc:mysql://222.222.222.222:3306/xxx?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&&autoReconnect=true
jdbc_username=xxx
jdbc_password=xxx hibernate.hbm2ddl.auto=none
hibernate.show_sql=false
hibernate.format_sql=false
hibernate.use_sql_comments=false
4 如果想操作两个数据库
config.properties配置文件中,新增数据库url,username和password
spring-hibernate.xml中复制一个新的数据源<bean class=”…DataSource”>,复制的数据源需要修改bean的name,以及属性url、username和password的值
spring-hibernate.xml中复制一个新的session工厂<bean class=”…SessionFactory…”>复制的session工厂需要修改bean的id,以及属性dataSource和packagesToScan
spring-hibernate.xml中复制一个新的事务管理器,修改bean的name和sessionFactory属性
结合本项目框架,在spring-hibernate中还需配置新的tx:advice和aop:config标签
在Dao操作中使用新配置的sessionFactory进行操作,即可对新数据库进行操作
javaEE中的hibernate配置笔记的更多相关文章
- javaEE中的spring配置笔记
0 JavaEE的工程目录 0.1 WebContent 项目的主目录,在eclipse新建工程时可以自己命名,部署时会把该文件夹的内容发布到tomcat的webapps里. 该目录下可以建立 ...
- Apache运维中常用功能配置笔记梳理
Apache 是一款使用量排名第一的 web 服务器,LAMP 中的 A 指的就是它.由于其开源.稳定.安全等特性而被广泛使用.下边记录了使用 Apache 以来经常用到的功能,做此梳理,作为日常运维 ...
- 在JavaEE中使用Hibernate框架
我们必须要了解一些Hibernate基础对象,如下: 配置对象 配置对象是你在任何 Hibernate 应用程序中创造的第一个 Hibernate 对象,并且经常只在应用程序初始化期间创造.它代表了 ...
- Orcale自增/Hibernate 配置
-- 自增 create sequence SEQ_T_APP_USER start with 1 increment by 1; -- 触发器 create trigger DECTUSER_T_ ...
- 笔记49 在Spittr应用中整合Hibernate
在前边构建的Spittr应用中整合Hibernate 由于最近所学的hibernate都是使用xml方式进行配置的,所以在与以Java方式配置的Spittr应用结合时就会出现一些小问题,在此进行总结. ...
- hibernate学习笔记--可选的配置属性
3.4. 可选的配置属性 有大量属性能用来控制Hibernate在运行期的行为. 它们都是可选的, 并拥有适当的默认值. 警告: 其中一些属性是"系统级(system-level)的&qu ...
- Hibernate学习笔记之EHCache的配置
Hibernate默认二级缓存是不启动的,启动二级缓存(以EHCache为例)需要以下步骤: 1.添加相关的包: Ehcache.jar和commons-logging.jar,如果hibernate ...
- JavaEE开发之SpringMVC中的路由配置及参数传递详解
在之前我们使用Swift的Perfect框架来开发服务端程序时,聊到了Perfect中的路由配置.而在SpringMVC中的路由配置与其也是大同小异的.说到路由,其实就是将URL映射到Java的具体类 ...
- hibernate中configuration和配置文件笔记
hibernate的核心类和接口 Configuration类 作用:(1)读取hibernate.cfg.xml文件 (2)管理对象关系映射文件<mapping resource=" ...
随机推荐
- C语言 百炼成钢27
/* 题目63:编写C++程序完成以下功能: (1)声明一个纯虚函数类Shape(形状),其中包含来计算面积.计算周长的方法: (2)从Shape派生两个类矩形和圆形: (3)从矩形派生正方形: (4 ...
- Hourrank 21 Tree Isomorphism 树hash
https://www.hackerrank.com/contests/hourrank-21/challenges/tree-isomorphism 题目大意: 给出一棵树, 求有多少本质不同的子树 ...
- eclipse闪退问题
昨日闲来无事,从eclipse官网下载了最新的eclipse版本,解压安装之后,便把之前安装的eclipse删除了,随后点击新安装的eclipse出现闪退问题,几经波折终于解决. 方法 ...
- windows 中 Eclipse 打开当前文件所在文件夹
默认情况下使用eclipse打开当前文件所在文件夹很麻烦,需要右键点击 Package Explorer 中的节点选择属性,然后复制路径,再打开资源管理器,然后再把路径粘贴进去.而MyEclipse一 ...
- iOS-@2x,@3x是什么意思
当我们在公司使用UI给出的图片时候,xxx.png,xxx@2x.png,xxx@3x.png的时候,不知道分别代表着什么! 本人也是菜鸟一枚,全凭自己尝试理解而已,在尝试中得出下面的结论: xxx. ...
- Http服务器实现文件上传与下载(五)
一.引言 欢迎大家和我一起编写Http服务器实现文件的上传和下载,现在我回顾一下在上一章节中提到的一些内容,之前我已经提到过文件的下载,在文件的下载中也提到了文件的续下载只需要在响应头中填写Conte ...
- 百度订单Api注意事项
背景介绍: 申请的百度地图API,采用javascript sdk方式 页面引用 问题1:更换域名导致定位插件不能用 需要修改百度地图-应用中的白名单设置,按照规则添加新的域名 问题2:http与ht ...
- MVC自定义验证 jquery.validate.unobtrusive
MVC的验证 jquery.validate.unobtrusive 阅读目录 一.应用 二.验证规则 1.一.简单规则 2.二.复杂一点的规则 3.三.再复杂一点的规则(正则) 4.四.再再复杂一点 ...
- Windows 磁盘分区
在“我的电脑”右键,点击“管理”,打开计算机管理,然后如图操作
- Windows窗口的层次关系(转)
今天看到这篇文章,觉得蛮有用的,我之前也对这个不大了解,特转载此处. 转载地址:http://www.51testing.com/html/200804/n80848.html 在Window 的图形 ...