其他分享
首页 > 其他分享> > 整合Spring5+Struts2.5+Hibernate5+maven

整合Spring5+Struts2.5+Hibernate5+maven

作者:互联网

1. 使用Eclipse创建Maven项目

2. 配置pom.xml引入需要的依赖包

 1   <dependencies>
 2         <dependency>
 3             <groupId>junit</groupId>
 4             <artifactId>junit</artifactId>
 5             <version>3.8.1</version>
 6             <scope>test</scope>
 7         </dependency>
 8 
 9         <!-- struts2 -->
10         <dependency>
11             <groupId>org.apache.struts</groupId>
12             <artifactId>struts2-core</artifactId>
13             <version>2.5.20</version>
14         </dependency>
15         <dependency>
16             <groupId>org.apache.struts</groupId>

17 <artifactId>struts2-convention-plugin</artifactId> 18 <version>2.5.20</version> 19 </dependency> 20 21 <!-- spring --> 22 <dependency> 23 <groupId>org.springframework</groupId> 24 <artifactId>spring-core</artifactId> 25 <version>5.1.5.RELEASE</version> 26 </dependency> 27 <dependency> 28 <groupId>org.springframework</groupId> 29 <artifactId>spring-web</artifactId> 30 <version>5.1.5.RELEASE</version> 31 </dependency> 32 <dependency> 33 <groupId>org.springframework</groupId> 34 <artifactId>spring-context</artifactId> 35 <version>5.1.5.RELEASE</version> 36 </dependency> 37 <dependency> 38 <groupId>org.springframework</groupId> 39 <artifactId>spring-beans</artifactId> 40 <version>5.1.5.RELEASE</version> 41 </dependency> 42 <dependency> 43 <groupId>org.springframework</groupId> 44 <artifactId>spring-aspects</artifactId> 45 <version>5.1.5.RELEASE</version> 46 </dependency> 47 48 <!-- hibernate --> 49 <dependency> 50 <groupId>org.hibernate</groupId> 51 <artifactId>hibernate-core</artifactId> 52 <version>5.4.2.Final</version> 53 </dependency> 54 55 <!-- spring + hibernate5 --> 56 <dependency> 57 <groupId>org.springframework</groupId> 58 <artifactId>spring-orm</artifactId> 59 <version>5.1.5.RELEASE</version> 60 </dependency> 61 <!-- struts2 + spring --> 62 <dependency> 63 <groupId>org.apache.struts</groupId> 64 <artifactId>struts2-spring-plugin</artifactId> 65 <version>2.5.20</version> 66 </dependency> 67 68 69 <!-- 数据库 --> 70 <dependency> 71 <groupId>commons-dbcp</groupId> 72 <artifactId>commons-dbcp</artifactId> 73 <version>1.4</version> 74 </dependency> 75 76 <dependency> 77 <groupId>sqlserver</groupId> 78 <artifactId>sqljdbc4</artifactId> 79 <version>4.0</version> 80 </dependency> 81 <!-- log4j-core --> 82 <dependency> 83 <groupId>org.apache.logging.log4j</groupId> 84 <artifactId>log4j-core</artifactId> 85 <version>2.11.2</version> 86 </dependency> 87 </dependencies>

3. 在源码路径下配置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>
        <property name="hibernate.c3p0.max_size">10</property>
        <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
        <property name="hibernate.show_sql">true</property>
    </session-factory>
</hibernate-configuration>

数据源依赖的数据可以放入spring配置中

4. 在源码路径下配置Spring配置文件 我这里为Spring.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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    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.xsd 
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd 
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd ">
  
   <context:component-scan base-package="com.hugh.ssh.dao" />
  
   <!-- 配置数据源 -->
   <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
        <property name="url" value="jdbc:sqlserver://IP地址:1433;DatabaseName=pz_test;" />
        <property name="username" value="sa"/>
        <property name="password" value="密码"/>
    </bean>  
    
    <!-- 配置session工厂  -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" lazy-init="false">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
        <!-- //加载实体类的映射文件位置及名称 -->
        <property name="mappingLocations" value="classpath:com/hugh/ssh/domain/*.hbm.xml"></property>
    </bean>  
    
    <!-- 配置Spring声明式事务  -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean> 
 
    <!-- 配置事务事务属性 -->
     <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="get*" read-only="true"/>
            <tx:method name="*"/>
        </tx:attributes>
    </tx:advice>
 
    <!-- 配置事务切点,并把切点和事务属性关联起来 -->
    <aop:config>
        <aop:pointcut expression="execution(* com.hugh.ssh.dao.impl.*.*(..))" id="txPointcut"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
    </aop:config>


</beans>

5. 在源码路径下配置struts.xml

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

    <!-- 指定由spring负责action对象的创建 -->
    <constant name="struts.objectFactory" value="spring" />

    <!-- 所有匹配*.action的请求都由struts2处理 -->
    <constant name="struts.action.extension" value="action," />
    <!-- 是否启用开发模式 -->
    <constant name="struts.devMode" value="true" />
    <!-- struts配置文件改动后,是否重新加载 -->
    <constant name="struts.configuration.xml.reload" value="true" />
    <!-- 设置浏览器是否缓存静态内容 -->
    <constant name="struts.serve.static.browserCache" value="false" />
    <!-- 请求参数的编码方式 -->
    <constant name="struts.i18n.encoding" value="UTF-8" />
    <!-- 每次HTTP请求系统都重新加载资源文件,有助于开发 -->
    <constant name="struts.i18n.reload" value="true" />
    <!-- 文件上传最大值 -->
    <constant name="struts.multipart.maxSize" value="104857600" />
    <!-- 让struts2支持动态方法调用 -->
    <constant name="struts.enable.DynamicMethodInvocation" value="true" />
    <!-- Action名称中是否还是用斜线 -->
    <constant name="struts.enable.SlashesInActionNames" value="false" />
    <!-- 允许标签中使用表达式语法 -->
    <constant name="struts.tag.altSyntax" value="true" />
    <!-- 对于WebLogic,Orion,OC4J此属性应该设置成true -->
    <constant name="struts.dispatcher.parametersWorkaround" value="false" />


    <package name="basicstruts2" extends="struts-default">
       <action name="index">
            <result>/index.jsp</result>
        </action>
    </package>

</struts>

6. 配置项目的web.xml

<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    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" metadata-complete="true">
    
    <display-name>ssh</display-name>

     <!-- 整合Spring -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:Spring.xml</param-value>
    </context-param>

    <!-- 整合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>

7. 源码路径下配置log4j.properties

### set log levels 日志的优先级###
log4j.rootLogger=INFO , console , debug , error

### console ###
log4j.appender.console = org.apache.log4j.ConsoleAppender 
log4j.appender.console.Target = System.out
log4j.appender.console.layout = org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern = %-d{yyyy-MM-dd HH\:mm\:ss} [%p]-[%c] %m%n

### log file 保存日志文件###
log4j.appender.debug = org.apache.log4j.DailyRollingFileAppender
log4j.appender.debug.File = logs/ghub_debug.log
log4j.appender.debug.Append = true 
log4j.appender.debug.Threshold = DEBUG
log4j.appender.debug.layout = org.apache.log4j.PatternLayout
log4j.appender.debug.layout.ConversionPattern = %-d{yyyy-MM-dd HH\:mm\:ss} [%p]-[%c] %m%n

### exception 保存异常文件###
log4j.appender.error = org.apache.log4j.DailyRollingFileAppender
log4j.appender.error.File = logs/ghub_error.log
log4j.appender.error.Append = true
log4j.appender.error.Threshold = ERROR
log4j.appender.error.layout = org.apache.log4j.PatternLayout
log4j.appender.error.layout.ConversionPattern = %-d{yyyy-MM-dd HH\:mm\:ss} [%p]-[%c] %m%n

#日志文件最大值
log4j.appender.File.MaxFileSize = 1000MB

 

标签:Hibernate5,spring,log4j,maven,debug,apache,org,Struts2.5,appender
来源: https://www.cnblogs.com/WongHugh/p/10609605.html