maven搭建SSH工程

这是农历新年后的第一篇博文,呵呵小小的标记一下!

在这一年主攻的知识:(与君共勉)

1,海量数据的存储与海量数据的分析

2,如何构建高并发的大型网站

3,移动领域的开发

这篇文章主要是介绍如何用maven搭建经典的SSH工程。SSH对码农来说是老朋友中的老朋友啊,为此就不细讲过程,我只是将重要的配置文件亮出来,同时提供工程供下载。

关键配置文件pom.xml

个人觉得该文件不是最精确的,望各位指点,使其变成精确的SSH的pom文件,即做到没有一个多余的jar包

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.cloud.oa</groupId>
  <artifactId>oa</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>oa Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

    <!-- Struts2配置 -->
    <dependency>
      <groupId>org.apache.struts</groupId>
      <artifactId>struts2-core</artifactId>
      <version>2.3.16</version>
      <exclusions>
        <exclusion>
          <groupId>asm</groupId>
          <artifactId>asm</artifactId>
        </exclusion>
        <exclusion>
          <groupId>asm</groupId>
          <artifactId>asm-commons</artifactId>
        </exclusion>
        <exclusion>
          <groupId>asm</groupId>
          <artifactId>asm-tree</artifactId>
        </exclusion>
      </exclusions>
    </dependency>

    <!-- Spring配置 -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>3.1.1.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
      <version>3.1.1.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>3.1.1.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>3.1.1.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-orm</artifactId>
      <version>3.1.1.RELEASE</version>
    </dependency> 
    <dependency>
      <groupId>org.apache.struts</groupId>
      <artifactId>struts2-spring-plugin</artifactId>
      <version>2.3.16</version>
    </dependency>

    <!-- Hibernate配置 -->
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-core</artifactId>
      <version>3.6.10.Final</version>
    </dependency>
    <dependency>
      <groupId>com.mchange</groupId>
      <artifactId>c3p0</artifactId>
      <version>0.9.5-pre6</version>
    </dependency> 
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.9</version>
    </dependency>

    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.14</version>
    </dependency>
    

  </dependencies>
  <build>
    <finalName>oa</finalName>
    <plugins>
      <plugin>
        <groupId>org.mortbay.jetty</groupId>
        <artifactId>maven-jetty-plugin</artifactId>
        <version>6.1.10</version>
         <configuration> 
            <contextPath>/oa</contextPath>
            <connectors>
                <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
                    <port>8080</port>
                </connector>
            </connectors>
            <scanIntervalSeconds>3</scanIntervalSeconds>
         </configuration>
      </plugin>
    </plugins>
  </build>
</project>
  

struts2的核心配置文件:

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="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="user_*" class="userAction" method="{1}">
            <result name="list">/WEB-INF/jsp/user/list.jsp</result>
            <result name="saveUI">/WEB-INF/jsp/user/saveUI.jsp</result>
            <result name="toList" type="redirectAction">user_list</result>
        </action>

    </package>

</struts>

hibernate核心配置文件:

 hibernate.cfg.xm

<?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">
        org.hibernate.dialect.MySQL5InnoDBDialect
    </property>

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

    <!-- 3,导入映射文件 -->
    <mapping resource="com/cloud/oa/model/User.hbm.xml"/>

</session-factory>

</hibernate-configuration>

 spring核心配置文件:

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:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <!-- 自动扫描与装配bean -->
    <context:component-scan base-package="com.cloud.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="classpath:hibernate.cfg.xml"></property>
        <!-- 配置c3p0数据库连接池 -->
        <property name="dataSource">
            <bean class="com.mchange.v2.c3p0.ComboPooledDataSource">
                <!-- 数据连接信息 -->
                <property name="jdbcUrl" value="${jdbcUrl}"></property>
                <property name="driverClass" value="${driverClass}"></property>
                <property name="user" value="${user}"></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.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <tx:annotation-driven transaction-manager="txManager"/>
</beans>

 jdbc的配置文件:

jdbc.properties

jdbcUrl=jdbc:mysql://localhost:3306/oa
driverClass=com.mysql.jdbc.Driver
user=root
password=abc123

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

  <display-name>OA Web Application</display-name>

  <!-- 配置Spring的用于初始化容器对象的监听器 -->
    <listener>
        <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>

  <!-- 配置Struts2的核心的过滤器 -->
    <filter>
        <filter-name>struts2</filter-name>
        <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>

附件为工程源代码

猜你喜欢

转载自09094224.iteye.com/blog/2017812
今日推荐