- 浏览: 520257 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (422)
- 重要 (12)
- BUG解决备忘录 (32)
- 环境搭建 (17)
- 开源组件 (4)
- 数据库 (16)
- 设计模式 (4)
- 测试 (3)
- javascript (5)
- Android (14)
- jdk相关 (9)
- struts2 (10)
- freemark (3)
- 自定义扩展及工具类 (5)
- jdk5新特性及java基础 (13)
- ssh及其他框架 (15)
- linux (32)
- tcp-ip http协议 (8)
- 服务器集群与负载均衡 (34)
- 项目管理相关 (11)
- 实用小技术 (10)
- 架构相关 (14)
- firefox组件 (11)
- spider (6)
- 产品设计 (11)
- PHP (1)
- ws (4)
- lucene (10)
- 其他 (2)
- BI (1)
- NoSQL (3)
- gzip (1)
- ext (4)
- db (6)
- socket (1)
- 源码阅读 (2)
- NIO (2)
- 图片处理 (1)
- java 环境 (2)
- 项目管理 (4)
- 从程序员到项目经理(一):没有捷径 (1)
- bug (1)
- JAVA BASE (8)
- 技术原理 (0)
- 新框架新技术 (1)
- 量化与python (1)
- 系统编程 (0)
- C语言 (0)
- 汇编 (0)
- 算法 (0)
最新评论
-
hyspace:
别逗了,最后一个算法根本不是最优的,sort(function ...
数组去重——一道前端校招试题 -
washingtin:
楼主能把策略和路由的类代码贴出来吗
Spring + iBatis 的多库横向切分简易解决思路 -
sdyjmc:
初略看了一下,没有闹明白啊,均衡负载使用Nginx,sessi ...
J2EE集群原理 I -
shandeai520:
谢谢大神!请教大神一个问题:假如我有三台服务器,连接池的上限是 ...
集群和数据库负载均衡的研究 -
hekuilove:
给lz推荐一下apache commonsStringUtil ...
request 获取 ip
前面已经讲述了Struts2+Spring整合,这里我们接着整合Hibernate
整合Hibernate
1)导入Hibernate3.1的jar包:antlr-2.7.6.jar,commons-collections-
3.1.jar,dom4j-1.6.1.jar,javassist-3.4.GA.jar,jta-
1.1.jar,hibernate3.jar,slf4j-api-1.5.6.jar,slf4j-
log4j12-1.5.6.jar,log4j-1.2.13.jar。暂时导入这些jar包,到时候需要再倒入。
2)将spring的配置文件applicationContext-*.xml文件放置在OA工程的src目录下,这里我们有三个:
applicationContext-action.xml,applicationContext-beans.xml,applicationContext-common.xml
3)在web.xml里面配置
- <!-- when application server started,loading the applicationContext-*.xml -->
- < context-param >
- < param-name > contextConfigLoaction </ param-name >
- < param-value > classpath*:applicationContext-*.xml </ param-value >
- </ context-param >
- <!-- integerate spring -->
- < listener >
- < listener-class > org.springframework.web.context.ContextLoaderListener </ listener-class >
- </ listener >
- <!-- let spring manage hibernate's session,that we can focus bussiness layer -->
- < filter >
- < filter-name > hibernateFilter </ filter-name >
- < filter-class > org.springframework.orm.hibernate3.support.OpenSessionInViewFilter </ filter-class >
- </ filter >
- < filter-mapping >
- < filter-name > hibernateFilter </ filter-name >
- < url-pattern > *.action </ url-pattern >
- </ filter-mapping >
- <!-- configrate struts2 core filter -->
- < filter >
- < filter-name > struts2 </ filter-name >
- < filter-class > org.apache.struts2.dispatcher.FilterDispatcher </ filter-class >
- </ filter >
- < filter-mapping >
- < filter-name > struts2 </ filter-name >
- < url-pattern > /* </ url-pattern >
- </ filter-mapping >
- <!-- configurate web character encoding -->
- < filter >
- < filter-name > encodingFilter </ filter-name >
- < filter-class > org.springframework.web.filter.CharacterEncodingFilter </ filter-class >
- < init-param >
- < param-name > encoding </ param-name >
- < param-value > utf-8 </ param-value >
- </ init-param >
- </ filter >
- < filter-mapping >
- < filter-name > encodingFilter </ filter-name >
- < url-pattern > /* </ url-pattern >
- </ filter-mapping >
<!-- when application server started,loading the applicationContext-*.xml --> <context-param> <param-name>contextConfigLoaction</param-name> <param-value>classpath*:applicationContext-*.xml</param-value> </context-param> <!-- integerate spring --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- let spring manage hibernate's session,that we can focus bussiness layer --> <filter> <filter-name>hibernateFilter</filter-name> <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>hibernateFilter</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping> <!-- configrate struts2 core filter --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- configurate web character encoding --> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
现在新出了gb18030,它的范围比gbk大一点,而gbk又比gb2312大一点
gb18030>gbk>gb2312
4)spring里面有声明式事务,它对hibernate做了封装,在applicationContext-common.xml里面配置spring的声明式事务:
- <!-- configure sessionFactory -->
- < bean id = "sessionFactory" class = "org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
- < property name = "configLocation" >
- < value > classpath:hibernate.cfg.xml </ value >
- </ property >
- </ bean >
- <!-- configure transaction manager-->
- < bean id = "transactionManager" class = "org.springframework.orm.hibernate3.HibernateTransactionManager" >
- < property name = "sessionFactory" >
- < ref bean = "sessionFactory" />
- </ property >
- </ bean >
- <!-- configure transaction's propagational feature -->
- < tx:advice id = "txAdvice" transaction-manager = "transactionManager" >
- < tx:attributes >
- < tx:method name = "add*" propagation = "REQUIRED" />
- < tx:method name = "delete*" propagation = "REQUIRED" />
- < tx:method name = "modify*" propagation = "REQUIRED" />
- < tx:method name = "*" read-only = "true" />
- </ tx:attributes >
- </ tx:advice >
- <!-- configure which class's which method take part in transaction -->
- < aop:config >
- < aop:pointcut id = "allManagerMethod" expression = "execution(* com.struts2.server.*.*(..))" />
- < aop:advisor pointcut-ref = "allManagerMethod" advice-ref = "txAdvice" />
- </ aop:config >
<!-- configure sessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation"> <value>classpath:hibernate.cfg.xml</value> </property> </bean> <!-- configure transaction manager--> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory"> <ref bean="sessionFactory"/> </property> </bean> <!-- configure transaction's propagational feature --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED"/> <tx:method name="delete*" propagation="REQUIRED"/> <tx:method name="modify*" propagation="REQUIRED"/> <tx:method name="*" read-only="true"/> </tx:attributes> </tx:advice> <!-- configure which class's which method take part in transaction --> <aop:config> <aop:pointcut id="allManagerMethod" expression="execution(* com.struts2.server.*.*(..))"/> <aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice"/> </aop:config>
用到了spring的aop,因此我们把spring的aop的jar包
aspectjrt.jar和aspectjweaver.jar加入到lib目录。spring没有开发自己的aop的jar包,它也是利用别人已经开
发好的jar包aspectjrt.jar和aspectjweaver.jar。
5)测试Struts2+Spring+Hibernate整合是否成功
第一步:测试Spring+Hibernate整合是否成功
写一个需要映射的值对象Person
- package com.oa.model;
- /**
- * @author lukuijun@hanqinet.com
- * @hibernate.class talbe="t_person"
- */
- public class Person {
- /**
- * @hibernate.id generator-class="native"
- */
- private int id;
- /**
- * @hibernate.property
- */
- private String name;
- /**
- * @hibernate.property
- */
- private String sex;
- /**
- * @hibernate.property
- */
- private String address;
- /**
- * @hibernate.property
- */
- private String duty;
- /**
- * @hibernate.property
- */
- private String phone;
- /**
- * @hibernate.property
- */
- private String description;
- public int getId() {
- return id;
- }
- public void setId( int id) {
- this .id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this .name = name;
- }
- public String getSex() {
- return sex;
- }
- public void setSex(String sex) {
- this .sex = sex;
- }
- public String getAddress() {
- return address;
- }
- public void setAddress(String address) {
- this .address = address;
- }
- public String getDuty() {
- return duty;
- }
- public void setDuty(String duty) {
- this .duty = duty;
- }
- public String getPhone() {
- return phone;
- }
- public void setPhone(String phone) {
- this .phone = phone;
- }
- public String getDescription() {
- return description;
- }
- public void setDescription(String description) {
- this .description = description;
- }
- }
package com.oa.model; /** * @author lukuijun@hanqinet.com * @hibernate.class talbe="t_person" */ public class Person { /** * @hibernate.id generator-class="native" */ private int id; /** * @hibernate.property */ private String name; /** * @hibernate.property */ private String sex; /** * @hibernate.property */ private String address; /** * @hibernate.property */ private String duty; /** * @hibernate.property */ private String phone; /** * @hibernate.property */ private String description; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getDuty() { return duty; } public void setDuty(String duty) { this.duty = duty; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } }
运行ant生成Person.hbm.xml映射文件:
- <? xml version = "1.0" encoding = "UTF-8" ?>
- <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- < hibernate-mapping >
- < class table = "t_person" name = "com.oa.model.Person" >
- < id access = "field" name = "id" >
- < generator class = "native" />
- </ id >
- < property name = "name" access = "field" />
- < property name = "sex" access = "field" />
- < property name = "address" access = "field" />
- < property name = "duty" access = "field" />
- < property name = "phone" access = "field" />
- < property name = "description" access = "field" />
- </ class >
- </ hibernate-mapping >
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class table="t_person" name="com.oa.model.Person"> <id access="field" name="id"> <generator class="native"/> </id> <property name="name" access="field"/> <property name="sex" access="field"/> <property name="address" access="field"/> <property name="duty" access="field"/> <property name="phone" access="field"/> <property name="description" access="field"/> </class> </hibernate-mapping>
运行ant生成hiberante.cfg.xml文件:
- <? xml version = "1.0" encoding = "UTF-8" ?>
- <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
- < hibernate-configuration >
- < session-factory >
- < property name = "hibernate.connection.driver_class" > com.mysql.jdbc.Driver </ property >
- < property name = "hibernate.connection.url" > jdbc:mysql://localhost:3306/oa </ property >
- < property name = "hibernate.connection.username" > root </ property >
- < property name = "hibernate.connection.password" > hanqinet </ property >
- < property name = "hibernate.dialect" > org.hibernate.dialect.MySQLDialect </ property >
- < property name = "hibernate.show_sql" > true </ property >
- < property name = "hibernate.hbm2ddl.auto" > update </ property >
- < mapping resource = "com/oa/model/Person.hbm.xml" />
- </ session-factory >
- </ hibernate-configuration >
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/oa</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">hanqinet</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.show_sql">true</property> <property name="hibernate.hbm2ddl.auto">update</property> <mapping resource="com/oa/model/Person.hbm.xml"/> </session-factory> </hibernate-configuration>
接着启动Tomcat,启动完毕检查数据库里面新增加了表t_person,Spring2+Hibernate整合成功
第二步:测试Struts2+Spring整合是否成功
参考1、Struts2+Spring整合,测试成功
完成这个两步成功,说明我们的框架整合成功!
发表评论
-
Struts2快速后台验证框架使用说明书
2010-12-14 19:17 2145http://code.google.com/u/beans ... -
Struts2 文件上传 之 文件类型 allowedTypes
2010-12-12 16:03 1166'.a' : 'applicatio ... -
struts2 注释配置
2010-12-09 00:04 1764<struts> <include f ... -
Struts2+Spring整合
2010-12-07 19:52 848Struts2和Spring整合,创建一个OA工程 1、 ... -
struts 2 中增加拦截器
2010-11-19 02:48 1119吃完午饭了,公司食堂真垃圾,点了份白切鸡 尽是鸡皮和鸡脖子,就 ... -
struts 2中当SESSION过期后返回登陆页面
2010-11-19 02:04 1238在struts2+spring+hibernate 的体系中, ... -
struts2 消息提示或错误提示
2010-09-07 00:33 1764>>action中调用 this ... -
Struts2 标签应用
2010-08-28 12:57 782<%@ taglib prefix="c&qu ... -
struts2 annotation 配置
2010-08-26 22:58 1600>> ------ ...
相关推荐
整合使用最新版本的三大框架(即Struts2、Spring4和Hibernate4),搭建项目架构原型。 项目架构原型:Struts2.3.16 + Spring4.1.1 + Hibernate4.3.6。 此外,还有:log4j、slf4j、junit4、ehcache等知识点。 项目...
轻量级JavaEE企业应用实战_Struts2+Spring3+Hibernate整合开发_第3版.part2
轻量级JavaEE企业应用实战_Struts2+Spring3+Hibernate整合开发_第3版.part1
轻量级JavaEE企业应用实战_Struts2+Spring3+Hibernate整合开发_第3版.part4
共分四个压缩包,JavaEE企业应用实战-Struts2+Spring3+Hibernate整合开发(第3版).part3
Struts2、Spring2.5和Hibernate3.2是Java Web开发中经典的三大框架,它们的整合使用在过去的许多年里被广泛应用于企业级应用系统。这个完整的项目提供了从开发环境到运行环境的所有必要组件,包括数据库脚本,使得...
轻量级JavaEE企业应用实战_Struts2+Spring3+Hibernate整合开发_第3版.part5
总的来说,"最新项目系统:Struts2+Spring4+Hibernate4三大框架整合"为学习和实践Java Web开发提供了一个实用的平台,对于提升开发者的技术水平和项目经验具有极大的帮助。通过深入研究和实践,开发者可以掌握Java ...
《轻量级Java EE企业应用实战(第4版)》这本书深入探讨了Struts 2、Spring 4和Hibernate这三大框架的整合开发,旨在帮助读者掌握如何在实际项目中高效构建Java EE应用。SSH(Struts 2、Spring、Hibernate)是Java ...
Struts 2+Spring 3+Hibernate框架技术精讲与整合案例3Struts 2+Spring 3+Hibernate框架技术精讲与整合案例3Struts 2+Spring 3+Hibernate框架技术精讲与整合案例3
Struts 2+Spring 3+Hibernate框架技术精讲与整合案例Struts 2+Spring 3+Hibernate框架技术精讲与整合案例Struts 2+Spring 3+Hibernate框架技术精讲与整合案例Struts 2+Spring 3+Hibernate框架技术精讲与整合案例...
《轻量级Java EE企业应用实战 第3版》是一本深度探讨Java EE开发技术的书籍,专注于Struts 2、Spring 3和Hibernate这三大框架的整合应用。这本书旨在帮助开发者掌握如何在实际项目中有效地集成这三个流行的技术,...
《轻量级Java EE企业应用实战:Struts2+Spring3+Hibernate整合开发(第3版)》介绍了Java EE领域的三个开源框架:Struts 2、Spring和Hibernate。其中Struts 2升级到2.2.1,Spring升级到3.0.5,Hibernate升级到了...
整合Struts2、Spring4和Hibernate5,通常遵循以下步骤: 1. **配置环境**:确保项目中包含这三个框架的库文件,并在pom.xml或build.gradle中添加相应的依赖。 2. **配置Struts2**:在struts.xml文件中定义Action类...
轻量级JavaEE企业应用实战_Struts2+Spring3+Hibernate整合开发_第3版.part3
struts2+spring2.5+hibernate3.2 + freemarker 全新功能实现的增删改查+freemarker 摸版 struts2 的方式自己去看简单。 spring2.5 是用注释来注入 hibernate3.2 是用ejb3注解映射关系 hibernate3 +个属性可以自动...
整合Struts1、Spring2和Hibernate2的过程主要包括以下步骤: 1. **配置环境**:确保所有依赖库已添加到项目的类路径中,如struts-core.jar、spring-framework.jar和hibernate-core.jar等。 2. **配置Struts1**:...
struts2.0+spring2.5+hibernate3.0整合框架,下载导入到eclipse下即可。。启动tomcat前先修改jdbc.properties文件。由于lib文件较大,所以请自行导入相关lib包。
介绍了Java EE领域的三个开源框架:Struts 2、Spring和Hibernate。 这是第三部分,我上传的上限是60MB,所以不得不分包额!
共分四个压缩包,JavaEE企业应用实战-Struts2+Spring3+Hibernate整合开发(第3版).part2