SpringMVC + Spring + Hibernate实战(通用配置)详解编程语言

配置文件

pom.xml

<?xml version="1.0" encoding="UTF-8"?> 
<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/xsd/maven-4.0.0.xsd"> 
<modelVersion>4.0.0</modelVersion> 
<groupId>com.aac</groupId> 
<artifactId>rfc-rabbit-mvc</artifactId> 
<version>1.0-SNAPSHOT</version> 
<packaging>war</packaging> 
<name>rfc-rabbit-mvc Maven Webapp</name> 
<!-- FIXME change it to the project's website --> 
<url>http://www.example.com</url> 
<properties> 
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
<maven.compiler.source>1.7</maven.compiler.source> 
<maven.compiler.target>1.7</maven.compiler.target> 
<spring.version>4.3.7.RELEASE</spring.version> 
<slf.version>1.7.2</slf.version> 
<hibernate.version>4.3.11.Final</hibernate.version> 
<jackson.version>2.8.8</jackson.version> 
</properties> 
<dependencies> 
<dependency> 
<groupId>junit</groupId> 
<artifactId>junit</artifactId> 
<version>4.12</version> 
<scope>test</scope> 
</dependency> 
<dependency> 
<groupId>org.projectlombok</groupId> 
<artifactId>lombok</artifactId> 
<version>1.18.4</version> 
</dependency> 
<dependency> 
<groupId>org.json</groupId> 
<artifactId>json</artifactId> 
<version>20160810</version> 
</dependency> 
<dependency> 
<groupId>com.oracle</groupId> 
<artifactId>ojdbc6</artifactId> 
<version>11.2.0</version> 
</dependency> 
<dependency> 
<groupId>org.apache.commons</groupId> 
<artifactId>commons-lang3</artifactId> 
<version>3.6</version> 
</dependency> 
<dependency> 
<groupId>cn.easyproject</groupId> 
<artifactId>xdb6</artifactId> 
<version>12.1.0.2.0</version> 
</dependency> 
<dependency> 
<groupId>com.mchange</groupId> 
<artifactId>c3p0</artifactId> 
<version>0.9.5.2</version> 
</dependency> 
<dependency> 
<groupId>org.springframework.amqp</groupId> 
<artifactId>spring-rabbit</artifactId> 
<version>2.1.2.RELEASE</version> 
</dependency> 
<dependency> 
<groupId>com.rabbitmq</groupId> 
<artifactId>amqp-client</artifactId> 
<version>5.4.3</version> 
</dependency> 
<dependency> 
<groupId>com.alibaba</groupId> 
<artifactId>fastjson</artifactId> 
<version>1.2.54</version> 
</dependency> 
<!-- jackson --> 
<dependency> 
<groupId>com.fasterxml.jackson.core</groupId> 
<artifactId>jackson-core</artifactId> 
<version>${jackson.version}</version> 
</dependency> 
<dependency> 
<groupId>com.fasterxml.jackson.core</groupId> 
<artifactId>jackson-databind</artifactId> 
<version>${jackson.version}</version> 
</dependency> 
<!-- slf --> 
<dependency> 
<groupId>org.slf4j</groupId> 
<artifactId>slf4j-log4j12</artifactId> 
<version>${slf.version}</version> 
</dependency> 
<dependency> 
<groupId>org.slf4j</groupId> 
<artifactId>slf4j-api</artifactId> 
<version>${slf.version}</version> 
</dependency> 
<!-- system --> 
<dependency> 
<groupId>com.sap</groupId> 
<artifactId>sapjco3</artifactId> 
<version>3.0.12</version> 
<scope>system</scope> 
<systemPath>${project.basedir}/src/main/webapp/WEB-INF/lib/sapjco3.jar</systemPath> 
</dependency> 
<dependency> 
<groupId>com.gzsolartech</groupId> 
<artifactId>smartforms_toolkit</artifactId> 
<version>1.0</version> 
<scope>system</scope> 
<systemPath>${project.basedir}/src/main/webapp/WEB-INF/lib/smartforms_toolkit.jar</systemPath> 
</dependency> 
<dependency> 
<groupId>com.oracle</groupId> 
<artifactId>ojdbc6</artifactId> 
<version>6.0</version> 
<scope>system</scope> 
<systemPath>${project.basedir}/src/main/webapp/WEB-INF/lib/ojdbc6.jar</systemPath> 
</dependency> 
<dependency> 
<groupId>com.oracle</groupId> 
<artifactId>xmlparserv2</artifactId> 
<version>2.0</version> 
<scope>system</scope> 
<systemPath>${project.basedir}/src/main/webapp/WEB-INF/lib/xmlparserv2.jar</systemPath> 
</dependency> 
<!-- spring --> 
<dependency> 
<groupId>org.springframework</groupId> 
<artifactId>spring-core</artifactId> 
<version>${spring.version}</version> 
</dependency> 
<dependency> 
<groupId>org.springframework</groupId> 
<artifactId>spring-context</artifactId> 
<version>${spring.version}</version> 
</dependency> 
<dependency> 
<groupId>org.springframework</groupId> 
<artifactId>spring-context-support</artifactId> 
<version>${spring.version}</version> 
</dependency> 
<dependency> 
<groupId>org.springframework</groupId> 
<artifactId>spring-tx</artifactId> 
<version>${spring.version}</version> 
</dependency> 
<dependency> 
<groupId>org.springframework</groupId> 
<artifactId>spring-web</artifactId> 
<version>${spring.version}</version> 
</dependency> 
<dependency> 
<groupId>org.springframework</groupId> 
<artifactId>spring-webmvc</artifactId> 
<version>${spring.version}</version> 
</dependency> 
<dependency> 
<groupId>org.springframework</groupId> 
<artifactId>spring-jdbc</artifactId> 
<version>${spring.version}</version> 
</dependency> 
<dependency> 
<groupId>org.springframework</groupId> 
<artifactId>spring-web</artifactId> 
<version>${spring.version}</version> 
</dependency> 
<dependency> 
<groupId>org.springframework</groupId> 
<artifactId>spring-tx</artifactId> 
<version>${spring.version}</version> 
</dependency> 
<dependency> 
<groupId>org.springframework</groupId> 
<artifactId>spring-test</artifactId> 
<version>${spring.version}</version> 
</dependency> 
<dependency> 
<groupId>org.springframework</groupId> 
<artifactId>spring-orm</artifactId> 
<version>${spring.version}</version> 
</dependency> 
<!-- hibernate --> 
<dependency> 
<groupId>org.hibernate</groupId> 
<artifactId>hibernate-core</artifactId> 
<version>${hibernate.version}</version> 
</dependency> 
<dependency> 
<groupId>org.hibernate</groupId> 
<artifactId>hibernate-ehcache</artifactId> 
<version>${hibernate.version}</version> 
</dependency> 
</dependencies> 
<build> 
<finalName>rfc-rabbit-mvc</finalName> 
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> 
<plugins> 
<plugin> 
<artifactId>maven-clean-plugin</artifactId> 
<version>3.0.0</version> 
</plugin> 
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --> 
<plugin> 
<artifactId>maven-resources-plugin</artifactId> 
<version>3.0.2</version> 
</plugin> 
<plugin> 
<artifactId>maven-compiler-plugin</artifactId> 
<version>3.7.0</version> 
</plugin> 
<plugin> 
<artifactId>maven-surefire-plugin</artifactId> 
<version>2.20.1</version> 
</plugin> 
<plugin> 
<artifactId>maven-war-plugin</artifactId> 
<version>3.2.0</version> 
</plugin> 
<plugin> 
<artifactId>maven-install-plugin</artifactId> 
<version>2.5.2</version> 
</plugin> 
<plugin> 
<artifactId>maven-deploy-plugin</artifactId> 
<version>2.8.2</version> 
</plugin> 
</plugins> 
</pluginManagement> 
</build> 
</project> 

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" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
id="WebApp_ID" version="3.0"> 
<display-name>rfc-rabbit-mvc</display-name> 
<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> 
<filter> 
<filter-name>openSessionInViewFilter</filter-name> 
<filter-class> 
org.springframework.orm.hibernate4.support.OpenSessionInViewFilter 
</filter-class> 
<init-param> 
<param-name>sessionFactoryBeanName</param-name> 
<param-value>sessionFactory</param-value> 
</init-param> 
</filter> 
<filter-mapping> 
<filter-name>openSessionInViewFilter</filter-name> 
<url-pattern>/*</url-pattern> 
</filter-mapping> 
<servlet> 
<servlet-name>spring-mvc</servlet-name> 
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
<init-param> 
<param-name>contextConfigLocation</param-name> 
<param-value>classpath:spring-mvc.xml</param-value> 
</init-param> 
<load-on-startup>1</load-on-startup> 
</servlet> 
<filter> 
<filter-name>encodingFilter</filter-name> 
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 
<init-param> 
<param-name>encoding</param-name> 
<param-value>UTF-8</param-value> 
</init-param> 
<init-param> 
<param-name>forceEncoding</param-name> 
<param-value>true</param-value> 
</init-param> 
</filter> 
<filter-mapping> 
<filter-name>encodingFilter</filter-name> 
<url-pattern>/*</url-pattern> 
</filter-mapping> 
<servlet-mapping> 
<servlet-name>spring-mvc</servlet-name> 
<url-pattern>*.action</url-pattern> 
</servlet-mapping> 
</web-app> 

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: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-4.1.xsd 
http://www.springframework.org/schema/context  
http://www.springframework.org/schema/context/spring-context-4.1.xsd 
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd"> 
<context:component-scan base-package="com.aac"/> 
<context:annotation-config/> 
<context:property-placeholder location="classpath:hibernate.properties"/> 
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" 
destroy-method="close"> 
<property name="driverClass" value="${database.driverClassName}"/> 
<property name="jdbcUrl" value="${database.url}"/> 
<property name="user" value="${database.username}"/> 
<property name="password" value="${database.password}"/> 
<property name="acquireIncrement" value="10"/> 
<property name="initialPoolSize" value="5"/> 
<property name="maxIdleTime" value="200"/> 
<property name="maxPoolSize" value="600"/> 
<property name="minPoolSize" value="5"/> 
<property name="acquireRetryDelay" value="30"/> 
<property name="acquireRetryAttempts" value="60"/> 
<property name="breakAfterAcquireFailure" value="false"/> 
<property name="autoCommitOnClose" value="true"/> 
</bean> 
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
<property name="dataSource" ref="dataSource"/> 
<property name="hibernateProperties"> 
<!-- 从配置文件中得到各种属性的值,更改的时候只需要改变配置文件从的值即可 --> 
<props> 
<prop key="hibernate.dialect">${hibernate.dialect}</prop> 
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop> 
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop> 
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop> 
<prop key="hibernate.connection.SetBigStringTryClob">${hibernate.connection.SetBigStringTryClob}</prop> 
<prop key="hibernate.temp.use_jdbc_metadata_defaults">false</prop> 
<!-- <prop key="hibernate.current_session_context_class">thread</prop> --> 
<!-- 开启查询缓存 --> 
<prop key="hibernate.cache.use_query_cache">true</prop> 
<!-- 开启二级缓存 --> 
<prop key="hibernate.cache.use_second_level_cache">true</prop> 
<!-- 高速缓存提供程序 --> 
<!-- 由于spring也使用了Ehcache, 保证双方都使用同一个缓存管理器 --> 
<prop key="hibernate.cache.region.factory_class"> 
org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory 
</prop> 
<!-- 用于oracle指定用户名(主要用于更新某个用户的表) --> 
<prop key="hibernate.default_schema">${database.username}</prop> 
</props> 
</property> 
<property name="packagesToScan"> 
<list> 
<value>com.aac.rfcrabbitmvc.pojo</value> 
</list> 
</property> 
</bean> 
<!-- 定义事务管理器 --> 
<bean id="txManager" 
class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
<property name="sessionFactory" ref="sessionFactory"/> 
</bean> 
<!-- 申明annotation 加载事务驱动 --> 
<tx:annotation-driven transaction-manager="txManager" 
proxy-target-class="true"/> 
<!-- hibernate4必须配置为开启事务 否则getCurrentSession()获取不到 --> 
<tx:advice id="txAdvice" transaction-manager="txManager"> 
<tx:attributes> 
<tx:method name="save*"/> 
<tx:method name="import*"/> 
<tx:method name="add*"/> 
<tx:method name="create*"/> 
<tx:method name="insert*"/> 
<tx:method name="edit*"/> 
<tx:method name="update*"/> 
<tx:method name="parse*"/> 
<tx:method name="merge*"/> 
<tx:method name="del*"/> 
<tx:method name="remove*"/> 
<tx:method name="put*"/> 
<tx:method name="use*"/> 
<tx:method name="start*"/> 
<tx:method name="stop*"/> 
<tx:method name="set*"/> 
<tx:method name="execute*"/> 
<tx:method name="get*" read-only="true"/> 
<tx:method name="count*" read-only="true"/> 
<tx:method name="find*" read-only="true"/> 
<tx:method name="list*" read-only="true"/> 
<tx:method name="*" read-only="true"/> 
</tx:attributes> 
</tx:advice> 
</beans> 

hibernate.properties

# 数据库配置 
database.driverClassName=oracle.jdbc.driver.OracleDriver 
# test env 
database.url=jdbc:oracle:thin:@192.168.0.1:1521/orcl 
database.username=test 
database.password=test 
# dev env 
#database.url=jdbc:oracle:thin:@192.168.0.2:1521/orcl 
#database.username=dev 
#database.password=dev 
# prod env 
#database.url=jdbc:oracle:thin:@//192.168.0.3:1521/orcl 
#database.username=prod 
#database.password=prod 
# Hibernate配置 
hibernate.dialect=org.hibernate.dialect.Oracle10gDialect 
hibernate.connection.SetBigStringTryClob=true 
hibernate.show_sql=false 
hibernate.format_sql=false 
hibernate.hbm2ddl.auto=none 

spring-mvc.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" 
xsi:schemaLocation=" 
http://www.springframework.org/schema/beans  
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd 
http://www.springframework.org/schema/context  
http://www.springframework.org/schema/context/spring-context-4.1.xsd 
http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"> 
<context:component-scan base-package="com.aac.rfcrabbitmvc.controller"/> 
<context:annotation-config/> 
<mvc:annotation-driven> 
<mvc:message-converters> 
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> 
<property name="objectMapper"> 
<bean class="com.fasterxml.jackson.databind.ObjectMapper"> 
<property name="dateFormat"> 
<bean class="java.text.SimpleDateFormat"> 
<constructor-arg type="java.lang.String" value="yyyy-MM-dd HH:mm:ss"/> 
</bean> 
</property> 
</bean> 
</property> 
</bean> 
</mvc:message-converters> 
</mvc:annotation-driven> 
</beans> 

log4j.properties

#输出ERROR级别 
#log4j.rootLogger=[level],appenderName1,appenderName2,... 
log4j.rootLogger=ERROR,stdout,E 
#控制台打印 
log4j.appender.stdout=org.apache.log4j.ConsoleAppender 
log4j.appender.stdout.Target=System.out 
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n 
#输出至info日志文件 
log4j.appender.I=org.apache.log4j.DailyRollingFileAppender 
log4j.appender.I.File=D:/wengzhengkai/logs/info/log.log 
log4j.appender.I.Append=true 
log4j.appender.I.Threshold=INFO 
log4j.appender.I.layout=org.apache.log4j.PatternLayout 
log4j.appender.I.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} [%t] [%c] [%p] - %m%n 
#输出至debug日志文件 
log4j.appender.D=org.apache.log4j.DailyRollingFileAppender 
log4j.appender.D.File=D:/wengzhengkai/logs/debug/log.log 
log4j.appender.D.Append=true 
log4j.appender.D.Threshold=DEBUG 
log4j.appender.D.layout=org.apache.log4j.PatternLayout 
log4j.appender.D.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} [%t] [%c] [%p] - %m%n 
#输出至error日志文件 
log4j.appender.E=org.apache.log4j.DailyRollingFileAppender 
log4j.appender.E.File=D:/wengzhengkai/logs/error/error.log 
log4j.appender.E.Append=true 
log4j.appender.E.Threshold=ERROR 
log4j.appender.E.layout=org.apache.log4j.PatternLayout 
log4j.appender.E.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} [%t] [%c] [%p] - %m%n 

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/19600.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论