Struts2+Spring3+Hibernate3+Maven构建(基于Eclipse)

长时间不做后台了,整理一下资料,以便翻阅。

 

Eclipse、JDK安装略……

Maven下载地址:http://maven.apache.org/download.cgi

版本比较新的Eclipse基本自带了Maven,但建议自行下载安装。

 

1、新建一个Maven项目,项目结构:

2、配置POM,Oracle因为涉及版权问题,需要自行注册在本地Maven仓库

<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>demo</groupId>

    <artifactId>ssh-maven</artifactId>

    <packaging>war</packaging>

    <version>0.0.1-SNAPSHOT</version>

    <name>ssh-maven Maven Webapp</name>

    <url>http://maven.apache.org</url>

    <!-- add repositories -->

    <repositories>

        <repository>

            <id>Java.Net</id>

            <url>http://download.java.net/maven/2/</url>

        </repository>

        <!-- hibernate -->

        <repository>

            <id>JBoss repository</id>

            <url>http://repository.jboss.com/maven2/</url>

        </repository>

        <repository>

            <id>maven</id>

            <url>http://repo1.maven.org/maven2/</url>

        </repository>

    </repositories>

    <!-- bind params -->

    <properties>

        <org.apache.struts.version>2.1.8.1</org.apache.struts.version>

        <org.springframework.version>3.0.7.RELEASE</org.springframework.version>

        <springscurity.version>3.1.0.RELEASE</springscurity.version>

    </properties>

    <dependencies>

        <!-- struts core -->

        <dependency>

            <groupId>org.apache.struts</groupId>

            <artifactId>struts2-core</artifactId>

            <version>${org.apache.struts.version}</version>

        </dependency>

        <dependency>

            <groupId>org.apache.struts</groupId>

            <artifactId>struts2-json-plugin</artifactId>

            <version>${org.apache.struts.version}</version>

        </dependency>

        <!-- spring jar list start -->

        <dependency>

            <groupId>org.springframework</groupId>

            <artifactId>spring-test</artifactId>

            <version>${org.springframework.version}</version>

        </dependency>

        <dependency>

            <groupId>org.springframework</groupId>

            <artifactId>spring-context</artifactId>

            <version>${org.springframework.version}</version>

        </dependency>

        <dependency>

            <groupId>org.springframework</groupId>

            <artifactId>spring-context-support</artifactId>

            <version>${org.springframework.version}</version>

        </dependency>

        <dependency>

            <groupId>org.springframework</groupId>

            <artifactId>spring-core</artifactId>

            <version>${org.springframework.version}</version>

        </dependency>

        <dependency>

            <groupId>org.springframework</groupId>

            <artifactId>spring-web</artifactId>

            <version>${org.springframework.version}</version>

        </dependency>

        <dependency>

            <groupId>org.springframework</groupId>

            <artifactId>spring-webmvc</artifactId>

            <version>${org.springframework.version}</version>

        </dependency>

        <dependency>

            <groupId>org.springframework</groupId>

            <artifactId>spring-beans</artifactId>

            <version>${org.springframework.version}</version>

        </dependency>

        <!-- 下面的三个包是在配置事务的时候用到的 spring 的依赖包 -->

        <dependency>

            <groupId>org.aspectj</groupId>

            <artifactId>aspectjweaver</artifactId>

            <version>1.7.0</version>

        </dependency>

        <dependency>

            <groupId>aopalliance</groupId>

            <artifactId>aopalliance</artifactId>

            <version>1.0</version>

        </dependency>

        <dependency>

            <groupId>cglib</groupId>

            <artifactId>cglib-nodep</artifactId>

            <version>2.2.2</version>

        </dependency>

        <!-- spring jar list end -->

        <!-- struts + spring plugin -->

        <dependency>

            <groupId>org.apache.struts</groupId>

            <artifactId>struts2-spring-plugin</artifactId>

            <version>${org.apache.struts.version}</version>

        </dependency>

        <!-- hibernate core -->

        <dependency>

            <groupId>org.hibernate</groupId>

            <artifactId>hibernate</artifactId>

            <version>3.2.1.ga</version>

        </dependency>

        <!-- spring + hibernate plugin -->

        <dependency>

            <groupId>org.springframework</groupId>

            <artifactId>spring-orm</artifactId>

            <version>${org.springframework.version}</version>

        </dependency>

        <!-- test jar -->

        <dependency>

            <groupId>junit</groupId>

            <artifactId>junit</artifactId>

            <version>3.8.1</version>

            <scope>test</scope>

        </dependency>

        <!-- oracle jar -->

        <dependency>

            <groupId>com.oracle</groupId>

            <artifactId>ojdbc14</artifactId>

            <version>10.2.0.2.0</version>

        </dependency>

        <!-- database connection pool jar -->

        <dependency>

            <groupId>commons-dbcp</groupId>

            <artifactId>commons-dbcp</artifactId>

            <version>1.4</version>

        </dependency>

        <!-- md5密码提要jar包 -->

        <dependency>

            <groupId>commons-codec</groupId>

            <artifactId>commons-codec</artifactId>

            <version>1.5</version>

        </dependency>

        <!-- 日志打印 log4j 包 -->

        <dependency>

            <groupId>log4j</groupId>

            <artifactId>log4j</artifactId>

            <version>1.2.14</version>

            <scope>runtime</scope>

        </dependency>

        <!-- 日志记录依赖包,很多都依赖此包,像 log4j,json-lib 等等 -->

        <dependency>

            <groupId>commons-logging</groupId>

            <artifactId>commons-logging-api</artifactId>

            <version>1.1</version>

        </dependency>

        <!-- dom4j 解析 XML 文件的包 -->

        <dependency>

            <groupId>dom4j</groupId>

            <artifactId>dom4j</artifactId>

            <version>1.6.1</version>

        </dependency>

        <!-- JSTL 标签库 -->

        <dependency>

            <groupId>javax.servlet</groupId>

            <artifactId>servlet-api</artifactId>

            <version>2.5</version>

        </dependency>

        <dependency>

            <groupId>javax.servlet.jsp</groupId>

            <artifactId>jsp-api</artifactId>

            <version>2.2</version>

        </dependency>

    </dependencies>

    <build>

        <finalName>ssh-maven</finalName>

    </build>

</project>

 


/////////////////////////////////////////////////////////////////////////////////////////////////////

以下内容摘抄自:http://blog.163.com/xueling1231989@126/blog/static/10264080720137211923432/

oracle 驱动本地路径:
C:\ ojdbc14.jar
 
Maven手动安装Oracle的jar包
在命令行窗口执行(注意更改路径,需要在本地安装Maven):
mvn install:install-file - DgroupId=com.oracle - DartifactId=ojdbc14 -Dversion=10.2.0.1.0 -Dpackaging=jar -Dfile= C:\ ojdbc14.jar
oracle的jar包 收费所以不能通过配置pom.xml获得,需要按照以上方法 进行配置

/////////////////////////////////////////////////////////////////////////////////////////////////////

 

2、右击项目,Run as - Maven install,开始自动下载jar包,过程根据网络情况,有可能失败。

成功的话会在控制台输出成功提示

 

3、添加ssh配置文件

  • struts.xml
  • app-srping.xml
  • jdbc.properties
  • log4j.properties
  • web.xml

struts.xml

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

<!DOCTYPE struts PUBLIC

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

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

<struts>

    <package name="web-default" extends="struts-default">

        <action name="/*">

            <param name="model">index</param>

            <result>/WEB-INF/view/index.jsp</result>

        </action>

    </package>

</struts>

 

app-srping.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"

    xmlns:jee="http://www.springframework.org/schema/jee"

    xsi:schemaLocation=" http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd

            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd

            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/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">

    <!-- 自动扫描Bean -->

    <context:component-scan base-package="com.garbin.demo.web" />

    <!-- 组件自动扫描 -->

    <context:component-scan base-package="com.garbin.demo.web.*"></context:component-scan>

    <!-- 加载配置参数 -->

    <context:property-placeholder location="classpath:/com/garbin/demo/config/*.properties"/>

    <!-- 数据库连接池初始化 -->

    <bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">

        <property name="driverClassName" value="${driver}" />

        <property name="url" value="${url}" />

        <property name="username" value="${user}" />

        <property name="password" value="${password}" />

        <!-- data source configuration -->

        <property name="initialSize" value="60" />

        <property name="maxActive" value="100" />

        <property name="maxIdle" value="50" />

        <property name="minIdle" value="10" />

    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

        <!-- 注入dataSource连接资源 -->

        <property name="dataSource" ref="dataSource">

        </property>

        <!-- 注入hibernate配置参数 -->

        <property name="hibernateProperties">

            <props>

                <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>

                <prop key="hibernate.show_sql">true</prop>

                <prop key="hibernate.format_sql">true</prop>

            </props>

        </property>

        <!-- 注入hibernate映射描述文件 -->

        <property name="mappingResources">

            <list>

                <!-- 

                <value>com/garbin/demo/mapper/*.hbm.xml</value>

                 -->

            </list>

        </property>

    </bean>

    <!-- 事务控制 -->

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

        <property name="dataSource" ref="dataSource" />

    </bean>

    <tx:advice id="userTxAdvice" transaction-manager="transactionManager">

        <tx:attributes>

            <tx:method name="*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" />

        </tx:attributes>

    </tx:advice>

    <aop:config>

        <!-- 把事务控制在 Business 层 -->

        <aop:pointcut id="pc" expression="execution(* com.garbin.demo.web.service.*.*(..))" />

        <aop:advisor pointcut-ref="pc" advice-ref="userTxAdvice" />

    </aop:config>

</beans>

jdbc.properties

driver=oracle.jdbc.OracleDriver

url=jdbc:oracle:thin:@localhost:1521:dab

user=dab  

password=19901204

log4j.properties

#配置了控制台和文本记录两种方式

log4j.rootLogger=DEBUG,CONSOLE,FILEOUT  

log4j.addivity.org.apache=true 

# CONSOLE  

log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender  

log4j.appender.Threshold=DEBUG  

log4j.appender.CONSOLE.Target=System.out  

log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout  

#log4j.appender.CONSOLE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n  

log4j.appender.CONSOLE.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH\:mm\:ss} \:%m%n 

#  

# FILEOUT  

log4j.appender.FILEOUT=org.apache.log4j.RollingFileAppender  

log4j.appender.FILEOUT.File=${catalina.home}\\file.log  

log4j.appender.fileout.MaxFileSize=100000KB  

# default is true,append to the file; if false, the replace the log file whenever restart system  

log4j.appender.FILEOUT.Append=true  

#RollingFileAppender没有DatePattern这个属性

log4j.appender.FILEOUT.layout=org.apache.log4j.PatternLayout  

#log4j.appender.CONSOLE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n  

log4j.appender.FILEOUT.layout.ConversionPattern=[%-5p]_%d{yyyy-MM-dd HH:mm:ss} :%m%n 

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

    <!-- Configuration of Log4j log4j的配置  start-->  

    <context-param>  

        <param-name>log4jConfigLocation</param-name>  

        <param-value>

        <!-- log4j.properties文件路径 -->  

        classpath:/com/garbin/demo/config/log4j.properties  

        </param-value>  

    </context-param>  

    <context-param>  

        <param-name>log4jRefreshInterval</param-name>  

        <param-value>60000</param-value>  

    </context-param>  

    <listener>  

        <listener-class>  

        org.springframework.web.util.Log4jConfigListener  

        </listener-class>  

    </listener>

    <!-- Configuration of Log4j log4j的配置 end-->

    

    <!-- spring app配置 start -->

    <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>/*</url-pattern>

    </filter-mapping>

    <context-param>

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

        <param-value>classpath:/com/garbin/demo/config/app-*.xml</param-value>

    </context-param>

    <listener>

        <listener-class>

              org.springframework.web.context.ContextLoaderListener

        </listener-class>

    </listener>

    <!-- spring app配置 end --> 

    <!-- struts前端转发器配置 start -->   

    <filter>

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

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

        <!-- 自定义struts.xml配置文件位置,struts-default.xml,struts-plugin.xml为必填项,不能去除 -->

        <init-param>  

            <param-name>config</param-name>  

            <param-value>struts-default.xml,struts-plugin.xml,com/garbin/demo/struts/struts.xml</param-value> 

        </init-param>

    </filter>

    <filter-mapping>

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

        <url-pattern>/*</url-pattern>

    </filter-mapping>

    <!-- struts前端转发器配置 end --> 

</web-app>

 

4、右击项目 Run as - Run on server,用Tomcat运行项目

项目配置成功。

 

5、利用Hibernate Tool自动生成实体类和映射文件

  1. Hibernate Tool的安装
  2. 打开Hibernate视图

你可能感兴趣的:(hibernate3)