struts2、Spring、Hibernate的整合-配置文件

来源:互联网 发布:ubuntu 15.10 源 编辑:程序博客网 时间:2024/05/07 16:37

最近又重新看了下ssh框架的配置,做个笔记。

1.首先在web.xml文件中注册Struts2和Spring。

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="3.0" 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_3_0.xsd">

<!-- 指定Spring配置文件的位置和名称 -->

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:applicationContext.xml</param-value>

</context-param>

<!-- 注册监听器,用于初始化Spring容器对象 -->

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

<!-- 配置Struts2 -->

<filter>

<filter-name>Struts2</filter-name>

<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>

</filter>

<filter-mapping>

<filter-name>Struts2</filter-name>

<url-pattern>/*</url-pattern><!-- 此处/*表示过滤所有请求 -->

</filter-mapping>

</web-app>


2.Struts2的配置文件.

Struts2的配置文件为struts.xml,放在根目录下。

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC

    "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"

    "http://struts.apache.org/dtds/struts-2.1.7.dtd">

<struts>

<!-- 是否开启开发者模式 -->

<constant name="struts.devMode" value="true"/>

<!-- 把扩展名配置为action -->

<constant name="struts.action.extension" value="action"/>

<!-- 把主题配置为simple -->

<constant name="struts.ui.theme=" value="simple"/>

<!-- —————————————————————— -->

<package name="default" namespace="/" extends="struts-default">


<action name="actionname" method="调用action的哪个方法" class="action的全名">
<result name="方法中返回的字段" type="访问的方式 默认为转发">访问的目标地址</result>
</action>

</package>

</struts>

3.Hibernate的配置

配置文件为hibernate.cfg.xml

<!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>
<property name="dialect">
org.hibernate.dialect.MySQL5InnoDBDialect
</property>
<!-- 
<property name=""></property>
<property name="connection.driver_class">com.jdbc.mysql.Driver</property>
<property name="connection.url">jdbc:mysql:///demo</property>
<property name="connection.username">root</property>
<property name="connection.password">1234</property>

此部分为hibernate的sessionfactory的连接数据库的配置

实际采用Spring注入的方式
-->



<!-- 其他配置 -->
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>


<!-- 导入映射文件 -->
<mapping resource="*.hbm.xml" />

</session-factory>

</hibernate-configuration>

4.Spring的配置

<?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">


<context:component-scan base-package="cn.oa"></context:component-scan>
<!-- 不知道为什么用不了 -->

<!-- 导入外部的properties文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置sessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 指定hibernate配置文件的路径 -->
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
<!-- 配置c3p0数据库连接池 -->
<property name="dataSource">
<bean class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- s数据库连接信息 -->
<property name="jdbcUrl" value="${jdbcUrl}"></property>
<property name="driverClass" value="${driverClass}"></property>
<property name="user" value="${userName}"></property>
<property name="password" value="${password}"></property>
<!-- 其他配置 -->
<!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
<property name="initialPoolSize" value="3"></property>
<!--连接池中保留的最小连接数。Default: 3 -->
<property name="minPoolSize" value="3"></property>
<!--连接池中保留的最大连接数。Default: 15 -->
<property name="maxPoolSize" value="5"></property>
<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
<property name="acquireIncrement" value="3"></property>
<!-- 控制数据源内加载的PreparedStatements数量。如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0 -->
<property name="maxStatements" value="8"></property>
<!--maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0 -->
<property name="maxStatementsPerConnection" value="5"></property>
<!--最大空闲时间,1800秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
<property name="maxIdleTime" value="1800"></property>
</bean>
</property>
</bean>
<!-- 配置声明式事务管理 -->
<bean id="txManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:annotation-driven transaction-manager="txManager"/>

</beans>



0 0
原创粉丝点击