SSH基本配置文件示例

2016-11-04  本文已影响0人  暗香抚动

一,web.xml配置

主要需要配置struts2的过滤器和spring的配置文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns="http://java.sun.com/xml/ns/javaee"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
      http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<!-- struts2的过滤器 -->
<display-name>struts_day03</display-name>
<filter>
<filter-name>struts2Filter</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 加载配置文件 -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 监听器 -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
  <welcome-file>index.html</welcome-file>
  <welcome-file>index.htm</welcome-file>
  <welcome-file>index.jsp</welcome-file>
  <welcome-file>default.html</welcome-file>
  <welcome-file>default.htm</welcome-file>
  <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

二,spring.xml配置

主要配置的是数据源和sessionFactory,以及一些需要扫描的类

<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
 
   <!-- 导入资源文件 -->
   <context:property-placeholder location="classpath:db.properties"/>
 
 <!-- 配置数据源 -->
 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
     <property name="user" value="${jdbc.user}"></property>
     <property name="password" value="${jdbc.password}"></property>
     <property name="driverClass" value="${jdbc.driverCalss}"></property>
     <property name="jdbcUrl" value="${jdbc.url}"></property>
     <property name="initialPoolSize" value="${jdbc.initialPoolSize}"></property>
     <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
 </bean>
 
 <!-- 配置hibernate的SessionFactory实例:通过spring的LocalSesionFactoryBean进行配置 -->
 <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
     <!-- 关联数据源 -->
     <property name="dataSource" ref="dataSource"></property>
     <!-- 配置hibernate.cfg.xml文件的位置和名称 -->
     <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
     <!-- 实体类的配置(*.hbm.xml)的配置 -->   
     <property name="mappingLocations" value="classpath:com/wxhl/edu/entity/*.hbm.xml"></property>
 </bean>
 <bean id="userDao" class="com.wxhl.edu.dao.impl.UserDaoImpl">
        <property name="sessionFactory" ref="sessionFactory"></property>
 </bean>
 <bean id="userService" class="com.wxhl.edu.service.impl.UserServiceImpl">
        <property name="userDao" ref="userDao"></property>
 </bean>
 <bean id="user-manager" class="com.wxhl.edu.action.UserManagerAction" scope="prototype">
    <property name="userService" ref="userService"></property>
 </bean>
 
 <bean class="com.wxhl.edu.entity.User"></bean>
</beans>

三,hibernate.cfg.xml配置

主要是sessionFactory的初始化属性

<?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>
        <!-- hibernate的基本属性: 方言,sql显示方式,格式化sql,表的生成策略 -->
        <property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>
        <property name="hbm2ddl.auto">update</property>
</session-factory>
</hibernate-configuration>

四,struts.xml配置

主要是一些action的配置

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
  "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
  "http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<constant name="struts.configuration.xml.reload" value="true" />
<constant name="struts.custom.i18n.resources" value="i18n" />
<package name="custom-default" namespace="/user" extends="struts-default">
    <action name="add" class="user-manager" method="add">
        <result name="Add">/addUser.jsp</result>
    </action>
    <action name="update" class="user-manager" method="update">
        <result name="Update">/updateUser.jsp</result>
    </action>
    <action name="delete" class="user-manager" method="delete">
        <result name="Delete">/listUser.jsp</result>
    </action>
    <action name="list" class="user-manager" method="list">
        <result name="list">/listUser.jsp</result>
    </action>
    <action name="saveUser" class="user-manager" method="save">
        <result name="save">/listUser.jsp</result>
    </action>
    <action name="updateUser" class="user-manager" method="updateUser">
        <result name="updateUser">/listUser.jsp</result>
    </action>
</package>
</struts>

五,持久化层的类名.hbm.xml配置

主要是一些查询语句

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.wxhl.edu.entity.User" table="tb_USER">
    <id name="userId" type="java.lang.Integer">
        <column name="USERID" />
        <generator class="native" />
    </id>
    <property name="userName" type="java.lang.String">
        <column name="USER_NAME" />
    </property>
    <property name="age" type="java.lang.Integer">
        <column name="AGE" />
    </property>
    <property name="no" type="java.lang.String">
        <column name="NO" />
    </property>
    <property name="class_grade" type="java.lang.String">
        <column name="CLASS_GRADE" />
    </property>
</class>
</hibernate-mapping>
上一篇 下一篇

猜你喜欢

热点阅读