首页 > 代码库 > spring+struts+hibernate项目配置
spring+struts+hibernate项目配置
applicationContext.xml
<?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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- 自动扫描与装配bean --> <context:component-scan base-package="cn.itcast.oa"></context:component-scan> <!-- 导入外部的properties文件 --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 配置SessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <!-- 指定hibernate的配置文件位置 --> <property name="configLocation" value="http://www.mamicode.com/classpath:hibernate.cfg.xml"></property> <!-- 配置c3p0数据库连接池 --> <property name="dataSource"> <bean class="com.mchange.v2.c3p0.ComboPooledDataSource"> <!-- 数据连接信息 --> <property name="jdbcUrl" value="http://www.mamicode.com/${jdbcUrl}"></property> <property name="driverClass" value="http://www.mamicode.com/${driverClass}"></property> <property name="user" value="http://www.mamicode.com/${user}"></property> <property name="password" value="http://www.mamicode.com/${password}"></property> <!-- 其他配置 --> <!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 --> <property name="initialPoolSize" value="http://www.mamicode.com/3"></property> <!--连接池中保留的最小连接数。Default: 3 --> <property name="minPoolSize" value="http://www.mamicode.com/3"></property> <!--连接池中保留的最大连接数。Default: 15 --> <property name="maxPoolSize" value="http://www.mamicode.com/5"></property> <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 --> <property name="acquireIncrement" value="http://www.mamicode.com/3"></property> <!-- 控制数据源内加载的PreparedStatements数量。如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0 --> <property name="maxStatements" value="http://www.mamicode.com/8"></property> <!--maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0 --> <property name="maxStatementsPerConnection" value="http://www.mamicode.com/5"></property> <!--最大空闲时间,1800秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 --> <property name="maxIdleTime" value="http://www.mamicode.com/1800"></property> </bean> </property> </bean> <!-- 配置声明式事务管理(采用注解的方式) --> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <tx:annotation-driven transaction-manager="txManager"/> </beans>
hibernate.cfg.xml
<?xml version=‘1.0‘ encoding=‘utf-8‘?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- 1,数据库连接信息 --> <property name="dialect"> <!-- hql语句 --> org.hibernate.dialect.MySQL5InnoDBDialect </property> <!-- <property name="connection.url">jdbc:mysql:///itcastoa0720</property> <property name="connection.driver_class">com.jdbc.mysql.Driver</property> <property name="connection.username">root</property> <property name="connection.password">root</property> --> <!-- 2,其他配置 --> <property name="show_sql">true</property> <!-- <property name="hbm2ddl.auto">update</property> --><!-- 根据持久化类和映射文件生成数据库表 --> <!-- 3,导入映射文件 --> <mapping resource="cn/itcast/oa/domain/User.hbm.xml" /> <mapping resource="cn/itcast/oa/domain/Role.hbm.xml" /> <mapping resource="cn/itcast/oa/domain/Department.hbm.xml" /> <mapping resource="cn/itcast/oa/domain/Privilege.hbm.xml" /> <mapping resource="cn/itcast/oa/domain/Forum.hbm.xml" /> <mapping resource="cn/itcast/oa/domain/Reply.hbm.xml" /> <mapping resource="cn/itcast/oa/domain/Topic.hbm.xml" /> </session-factory> </hibernate-configuration>
jdbc.properties
jdbcUrl = jdbc:mysql:///itcastoa0720
driverClass = com.mysql.jdbc.Driver
user = root
password = root
log4j.properties
### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### direct messages to file hibernate.log ###
#log4j.appender.file=org.apache.log4j.FileAppender
#log4j.appender.file.File=hibernate.log
#log4j.appender.file.layout=org.apache.log4j.PatternLayout
#log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### set log levels - for more verbose logging change ‘info‘ to ‘debug‘ ###
log4j.rootLogger=warn, stdout
#log4j.logger.org.hibernate=info
#log4j.logger.org.hibernate=debug
log4j.logger.cn.itcast.oa=debug
struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <!-- 配置为开发模式 --> <constant name="struts.devMode" value="http://www.mamicode.com/true" /> <!-- 把扩展名配置为action --> <constant name="struts.action.extension" value="http://www.mamicode.com/action" /> <!-- 把主题配置为simple --> <constant name="struts.ui.theme" value="http://www.mamicode.com/simple" /> <package name="default" namespace="/" extends="struts-default"> <interceptors> <!-- 声明 权限拦截器 --> <interceptor name="checkPrivilege" class="cn.itcast.oa.util.CheckPrivilegeInterceptor"></interceptor> <!-- 重新定义默认的拦截器栈 --> <interceptor-stack name="defaultStack"> <interceptor-ref name="checkPrivilege"></interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </interceptor-stack> </interceptors> <!-- 全局的Result配置 --> <global-results> <result name="loginUI">/WEB-INF/jsp/userAction/loginUI.jsp</result> <result name="noPrivilegeError">/noPrivilegeError.jsp</result> </global-results> <!-- 配置测试用的Action,未与Spring整合,class属性写类的全名 --> <!-- 当Struts2与Spring整合后,class属性可以写bean的名称 --> <action name="test" class="testAction"> <result name="success">/test.jsp</result> </action> <!-- 岗位管理 --> <action name="role_*" class="roleAction" method="{1}"> <result name="list">/WEB-INF/jsp/roleAction/list.jsp</result> <result name="saveUI">/WEB-INF/jsp/roleAction/saveUI.jsp</result> <result name="setPrivilegeUI">/WEB-INF/jsp/roleAction/setPrivilegeUI.jsp</result> <result name="toList" type="redirectAction">role_list</result> </action> <!-- 部门管理 --> <!-- departmentAction 通过spring容器监听器和整合包里面的struts-pluging.xml设置了启动的时候,就创建了这个Action对象 --> <action name="department_*" class="departmentAction" method="{1}"> <result name="list">/WEB-INF/jsp/departmentAction/list.jsp</result> <result name="saveUI">/WEB-INF/jsp/departmentAction/saveUI.jsp</result> <result name="toList" type="redirectAction">department_list?parentId=${parentId}</result> </action> <!-- 用户管理 --> <action name="user_*" class="userAction" method="{1}"> <result name="list">/WEB-INF/jsp/userAction/list.jsp</result> <result name="saveUI">/WEB-INF/jsp/userAction/saveUI.jsp</result> <result name="toList" type="redirectAction">user_list</result> <result name="logout">/WEB-INF/jsp/userAction/logout.jsp</result> <result name="toIndex" type="redirect">/index.jsp</result> </action> <!-- 首页 /home_index.action--> <action name="home_*" class="homeAction" method="{1}"> <result name="{1}">/WEB-INF/jsp/homeAction/{1}.jsp</result> </action> <!-- 论坛:版块管理 --> <action name="forumManage_*" class="forumManageAction" method="{1}"> <result name="list">/WEB-INF/jsp/forumManageAction/list.jsp</result> <result name="saveUI">/WEB-INF/jsp/forumManageAction/saveUI.jsp</result> <result name="toList" type="redirectAction">forumManage_list</result> </action> <!-- 论坛:版块相关功能 --> <action name="forum_*" class="forumAction" method="{1}"> <result name="list">/WEB-INF/jsp/forumAction/list.jsp</result> <result name="show">/WEB-INF/jsp/forumAction/show.jsp</result> </action> <!-- 论坛:主题相关功能 --> <action name="topic_*" class="topicAction" method="{1}"> <result name="show">/WEB-INF/jsp/topicAction/show.jsp</result> <result name="addUI">/WEB-INF/jsp/topicAction/addUI.jsp</result> <result name="toShow" type="redirectAction">topic_show?id=${id}</result> </action> <!-- 论坛:回复相关功能 --> <action name="reply_*" class="replyAction" method="{1}"> <result name="addUI">/WEB-INF/jsp/replyAction/addUI.jsp</result> <result name="toTopicShow" type="redirectAction">topic_show?id=${topicId}</result> </action> </package> <!-- Add packages here --> </struts>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- 配置Spring的用于初始化容器对象的监听器 --> <listener> <!-- 启动web容器,加载spring配置文件,生成applicationContext对象 --> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext*.xml</param-value> </context-param> <!-- 用于做初始化工作的监听器,一定要配置到Spring的ContextLoaderListener之后,因为要用到Spring的容器对象 --> <listener> <listener-class>cn.itcast.oa.util.InitListener</listener-class> </listener> <!-- 配置Spring的用于解决懒加载问题的过滤器 --> <filter> <!-- 这个过滤器直接打开了session,当action执行结束之后再关闭session,延长了session的寿命,注意这个Filter要在struts的Filter的前面 --> <filter-name>OpenSessionInViewFilter</filter-name> <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>OpenSessionInViewFilter</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping> <!-- 配置Struts2的核心的过滤器 --> <filter> <filter-name>struts2</filter-name> <!-- 从请求的url上面找到配置文件的action,利用反射生成action,如果和spring结合之后,spring早早的创建了action,struts拿来用就可以了哦 --> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
Spring为我们解决最让人头痛的难题之一,Hibernate的Session的关闭与开启问题。 Hibernate 允许对关联对象、属性进行延迟加载,但是必须保证延迟加载的操作限于同一个 Hibernate Session 范围之内进行。如果 Service 层返回一个启用了延迟加载功能的领域对象给 Web 层,当 Web 层访问到那些需要延迟加载的数据时,由于加载领域对象的 Hibernate Session 已经关闭,这些导致延迟加载数据的访问异常。而Spring为我们提供的OpenSessionInViewFilter过滤器为我们很好的解决了这个问题。OpenSessionInViewFilter的主要功能是使每个请求过程绑定一个 Hibernate Session,即使最初的事务已经完成了,也可以在 Web 层进行延迟加载的操作。OpenSessionInViewFilter 过滤器将 Hibernate Session 绑定到请求线程中,它将自动被 Spring 的事务管理器探测到。所以 OpenSessionInViewFilter 适用于 Service 层使用HibernateTransactionMana<wbr>ger 或 JtaTransactionManager 进行事务管理的环境,也可以用于非事务只读的数据操作中
本文出自 “你若盛开,清风自来” 博客,谢绝转载!
spring+struts+hibernate项目配置