1.引入第三方Jar包或者用Maven的pom添加依赖
<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.0modelVersion>
<groupId>com.candlegroupId>
<artifactId>home-finance-sampleartifactId>
<packaging>warpackaging>
<version>0.0.1-SNAPSHOTversion>
<name>home-finance-sample Maven Webappname>
<url>http://maven.apache.orgurl>
<build>
<finalName>home-finance-samplefinalName>
build>
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.11version>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-coreartifactId>
<version>3.2.6.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>3.2.6.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-context-supportartifactId>
<version>3.2.6.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-txartifactId>
<version>3.2.6.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-aopartifactId>
<version>3.2.6.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-beansartifactId>
<version>3.2.6.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>3.2.6.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webartifactId>
<version>3.2.6.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>3.2.6.RELEASEversion>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.2.4version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-springartifactId>
<version>1.2.2version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.28version>
dependency>
<dependency>
<groupId>commons-dbcpgroupId>
<artifactId>commons-dbcpartifactId>
<version>1.4version>
dependency>
<dependency>
<groupId>commons-poolgroupId>
<artifactId>commons-poolartifactId>
<version>1.6version>
dependency>
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
<version>1.2.17version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-apiartifactId>
<version>1.7.5version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-log4j12artifactId>
<version>1.7.5version>
dependency>
<dependency>
<groupId>org.freemarkergroupId>
<artifactId>freemarkerartifactId>
<version>2.3.20version>
dependency>
<dependency>
<groupId>org.apache.tomcatgroupId>
<artifactId>servlet-apiartifactId>
<version>6.0.37version>
dependency>
dependencies>
project>
2.配置web.xml文件
<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">
<session-config>
<session-timeout>-1session-timeout>
session-config>
<filter>
<filter-name>encodingFilterfilter-name>
<filter-class>
org.springframework.web.filter.CharacterEncodingFilter
filter-class>
<init-param>
<param-name>encodingparam-name>
<param-value>UTF-8param-value>
init-param>
<init-param>
<param-name>forceEncodingparam-name>
<param-value>trueparam-value>
init-param>
filter>
<filter-mapping>
<filter-name>encodingFilterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>
classpath:/config/spring/spring-common.xml
param-value>
context-param>
<listener>
<listener-class> org.springframework.web.context.ContextLoaderListener
listener-class>
listener>
<listener>
<listener-class>
com.sz.mt.listener.LoginListenner
listener-class>
listener>
<servlet>
<servlet-name>SpringMVCservlet-name>
<servlet-class> org.springframework.web.servlet.DispatcherServlet
servlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>
classpath:/config/spring/springmvc-servlet.xml
param-value>
init-param>
<load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>SpringMVCservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
<listener>
<listener-class> org.springframework.web.context.request.RequestContextListener
listener-class>
listener>
<listener>
<listener-class>
org.springframework.web.util.IntrospectorCleanupListener
listener-class>
listener>
<listener-class>com.sz.mt.listener.ServersInitlistener-class>
listener>
<error-page>
<error-code>500error-code>
<location>/WEB-INF/views/common/timeout.jsplocation>
error-page>
<error-page>
<error-code>404error-code>
<location>/WEB-INF/views/common/404.jsplocation>
error-page>
<error-page>
<error-code>403error-code>
<location>/WEB-INF/views/common/timeout.jsplocation>
error-page>
<welcome-file-list>
<welcome-file>index.jspwelcome-file>
welcome-file-list>
web-app>
3.spring-common.xml(Spring配置),此处省略config.properties配置的数据库连接的内容
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-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"
>
<import resource="classpath:/config/ehcache/applicationContext-ehcache.xml"/>
<mvc:annotation-driven />
<task:annotation-driven/>
<aop:aspectj-autoproxy/>
<context:annotation-config />
<mvc:annotation-driven />
<context:component-scan base-package="com.sz.mt" annotation-config="true">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
context:component-scan>
<context:property-placeholder location="classpath:/config/ibatis/jdbc.properties" />
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="1048576000" />
bean>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="filters" value="${jdbc.filters}" />
<property name="initialSize" value="8" />
<property name="minIdle" value="8" />
<property name="maxActive" value="100" />
<property name="maxWait" value="60000" />
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<property name="minEvictableIdleTimeMillis" value="300000" />
<property name="validationQuery" value="SELECT 'x'" />
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<property name="poolPreparedStatements" value="true" />
<property name="maxPoolPreparedStatementPerConnectionSize" value="20" />
bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:/config/ibatis/mybatis-config.xml" />
<property name="mapperLocations" value="classpath*:com/sz/mt/dao/*Mapper.xml" />
bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<property name="basePackage" value="com.sz.mt.dao" />
bean>
<bean class="com.sz.mt.common.springmvc.SpringContextHolder" lazy-init="false" />
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
bean>
<tx:annotation-driven transaction-manager="transactionManager" />
beans>
4.spring-servlet.xml文件配置(SpringMVC)
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
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-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-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">
<aop:aspectj-autoproxy proxy-target-class="true" />
<context:annotation-config />
<context:component-scan base-package="com.sz.mt"
use-default-filters="false">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
context:component-scan>
<bean id="applicationProperties"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="ignoreResourceNotFound" value="true" />
<property name="locations">
<list>
<value>classpath:/config/ibatis/jdbc.propertiesvalue>
list>
property>
bean>
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="systemPropertiesModeName"
value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<property name="properties" ref="applicationProperties" />
bean>
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:resources mapping="/upload/**" location="/upload/" />
<mvc:resources mapping="/pdf/**" location="/pdf/" />
<mvc:annotation-driven />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
bean>
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/main*" />
<mvc:mapping path="/main/**" />
<mvc:mapping path="/user*" />
<mvc:mapping path="/user/**" />
<bean
class="com.sz.mt.common.interseptor.LoginInterceptor">
bean>
mvc:interceptor>
mvc:interceptors>
<bean
class="com.sz.mt.common.interseptor.ControllerJsonExceptionHandler">
bean>
<bean id="freemarkerResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="order" value="1" />
<property name="suffix" value=".ftl" />
<property name="contentType" value="text/html;charset=utf-8" />
<property name="viewClass">
<value>org.springframework.web.servlet.view.freemarker.FreeMarkerViewvalue>
property>
bean>
<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath">
<value>/views/value>
property>
<property name="freemarkerSettings">
<props>
<prop key="template_update_delay">5prop>
<prop key="default_encoding">UTF-8prop>
<prop key="locale">UTF-8prop>
<prop key="datetime_format">yyyy-MM-dd HH:mm:ssprop>
<prop key="time_format">HH:mm:ssprop>
<prop key="number_format">0.####prop>
<prop key="boolean_format">true,falseprop>
<prop key="whitespace_stripping">trueprop>
<prop key="tag_syntax">auto_detectprop>
<prop key="url_escaping_charset">UTF-8prop>
props>
property>
bean>
beans>
5.mybatis-config.xml文件(Mybatis配置)
<configuration>
<mappers>
<mapper resource="config/ibatis/common_SqlMap.xml" />
mappers>
configuration>
6.common_SqlMap.xml文件配置分页
<mapper namespace="common" >
<sql id="Oracle_Pagination_Head" >
<if test="oracleStart != null and oracleEnd != null" >
if>
sql>
<sql id="Oracle_Pagination_Tail" >
<if test="oracleStart != null and oracleEnd != null" >
#{oracleStart} ]]>
if>
sql>
mapper>
7.jdbc.properties文件配置
#druid Connection
jdbc.url=jdbc:oracle:thin:@127.0.0.1:1521:ORCL
jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
jdbc.username=user
jdbc.password=password
jdbc.filters=stat
jdbc.maxActive=20
jdbc.initialSize=1
jdbc.maxWait=60000
jdbc.minIdle=10
jdbc.maxIdle=15
jdbc.timeBetweenEvictionRunsMillis=60000
jdbc.minEvictableIdleTimeMillis=300000
jdbc.validationQuery=SELECT 'x'
jdbc.testWhileIdle=true
jdbc.testOnBorrow=false
jdbc.testOnReturn=false
jdbc.maxOpenPreparedStatements=20
jdbc.removeAbandoned=true
jdbc.removeAbandonedTimeout=1800
jdbc.logAbandoned=true
8.连接池的配置spring-pool.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context" 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-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-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 id="c3p0" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${oracle.jdbc.driverClassName}" />
<property name="jdbcUrl" value="${oracle.jdbc.url}" />
<property name="user" value="${oracle.jdbc.username}" />
<property name="password" value="${oracle.jdbc.password}" />
<property name="acquireIncrement" value="5" />
<property name="acquireRetryAttempts" value="30" />
<property name="acquireRetryDelay" value="1000" />
<property name="autoCommitOnClose" value="false" />
<property name="checkoutTimeout" value="10000" />
<property name="idleConnectionTestPeriod" value="60" />
<property name="initialPoolSize" value="20" />
<property name="minPoolSize" value="20" />
<property name="maxPoolSize" value="100" />
<property name="maxIdleTime" value="60" />
<property name="automaticTestTable" value="c3p0_TestTable" />
<property name="breakAfterAcquireFailure" value="false" />
bean>
<bean id="bonecp" class="com.jolbox.bonecp.BoneCPDataSource" destroy-method="close">
<property name="driverClass" value="${oracle.jdbc.driverClassName}" />
<property name="jdbcUrl" value="${oracle.jdbc.url}" />
<property name="username" value="${oracle.jdbc.username}" />
<property name="password" value="${oracle.jdbc.password}" />
<property name="idleConnectionTestPeriodInMinutes" value="60" />
<property name="idleMaxAgeInMinutes" value="30" />
<property name="maxConnectionsPerPartition" value="30" />
<property name="minConnectionsPerPartition" value="10" />
<property name="partitionCount" value="3" />
<property name="acquireIncrement" value="5" />
<property name="statementsCacheSize" value="100" />
<property name="releaseHelperThreads" value="3" />
bean>
<bean id="proxool" class="org.logicalcobwebs.proxool.ProxoolDataSource" >
<property name="driver" value="${oracle.jdbc.driverClassName}" />
<property name="driverUrl" value="${oracle.jdbc.url}" />
<property name="user" value="${oracle.jdbc.username}" />
<property name="password" value="${oracle.jdbc.password}" />
<property name="alias" value="proxool_alias" />
<property name="prototypeCount" value="4" />
<property name="minimumConnectionCount" value="1" />
<property name="maximumConnectionCount" value="30" />
<property name="houseKeepingTestSql" value="select sysdate from dual" />
bean>
<bean id="dbcp" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${oracle.jdbc.driverClassName}" />
<property name="url" value="${oracle.jdbc.url}" />
<property name="username" value="${oracle.jdbc.username}" />
<property name="password" value="${oracle.jdbc.password}" />
<property name="initialSize" value="1" />
<property name="maxActive" value="30" />
<property name="maxIdle" value="2" />
<property name="minIdle" value="1" />
<property name="timeBetweenEvictionRunsMillis" value="3600000" />
<property name="minEvictableIdleTimeMillis" value="3600000" />
bean>
beans>