导入spring4.* 相关的jar包和依赖包即可
1、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>ifengUpgrade</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> <welcome-file>login.jsp</welcome-file> </welcome-file-list> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>SpringMVC</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>2</load-on-startup> --> </servlet> <servlet-mapping> <servlet-name>SpringMVC</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <async-supported>true</async-supported> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
2、spring.xm配置
<?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:aop="http://www.springframework.org/schema/aop" 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.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"> <!-- 扫描 注入 --> <!-- <context:component-scan base-package="com.hk"></context:component-scan> --> <context:property-placeholder location="classpath:config.properties" ignore-unresolvable="true"/> <context:property-placeholder location="classpath:db.properties"/> <context:component-scan base-package="com.hk.common.timer" /> <context:component-scan base-package="com.hk.*.controller" /> <context:component-scan base-package="com.hk.*.dao" /> <context:component-scan base-package="com.hk.*.service.impl" /> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${jdbc.driver}"></property> <property name="url" value="${jdbc.url}"></property> <property name="username" value="${jdbc.user}"></property> <property name="password" value="${jdbc.password}"></property> <property name="initialSize" value="${initialSize}"></property> <property name="maxActive" value="${maxActive}"></property> <property name="maxIdle" value="${maxIdle}"></property> <property name="minIdle" value="${minIdle}"></property> <property name="maxWait" value="${maxWait}"></property> </bean> <!-- 配置数据连接 工厂 自动扫描mapping.xml文件 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!-- 自动扫描mapping.xml文件 --> <property name="mapperLocations" value="classpath:com/hk/*/entity/*.xml"></property> </bean> <!-- DAO接口所在包名,Spring会自动查找其下的类 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.hk.*.dao" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean> <!-- 事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <tx:annotation-driven transaction-manager="transactionManager"/> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <!-- 约定优于编码 设置事务详情 --> <tx:method name="get*" read-only="true" propagation="SUPPORTS"/> <tx:method name="find*" read-only="true" propagation="SUPPORTS"/> <tx:method name="select*" read-only="true" propagation="SUPPORTS"/> <tx:method name="add*"/> <tx:method name="save*"/> <tx:method name="insert*"/> <tx:method name="delete*"/> <tx:method name="update*"/> </tx:attributes> </tx:advice> <aop:config> <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.hk.*.service.impl.*.*(..))"/> </aop:config> </beans>
3、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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd"> <!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 --> <context:component-scan base-package="com.hk.*.controller" /> <!--避免IE执行AJAX时,返回JSON出现下载文件 --> <!-- 定义跳转的文件的前后缀 ,视图模式配置--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 --> <!-- <property name="prefix" value="/WEB-INF/jsp/" /> --> <property name="suffix" value=".jsp" /> </bean> <!-- 上传文件配置 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="resolveLazily" value="true"/> <property name="maxUploadSize" value="100000000"/> <property name="defaultEncoding" value="utf-8"/> </bean> <mvc:annotation-driven></mvc:annotation-driven> </beans>
4、db.properties 数据库连接配置
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3619/gps6?useUnicode=true&characterEncoding=utf8 jdbc.user=root jdbc.password=sdasda #----------- --------- 公共 ------------------------------# #定义初始连接数 initialSize=0 #定义最大连接数 maxActive=20 #定义最大空闲 maxIdle=20 #定义最小空闲 minIdle=1 #定义最长等待时间 maxWait=60000
5、log4j.properties 配置
##root logger , local level: debug, output: stdout ##
## root logger: better performance than others ##
log4j.rootLogger=info,stdout,A2
log4j.appender.stdout=org.apache.log4j.DailyRollingFileAppender
log4j.appender.stdout.Threshold=stdout
log4j.appender.stdout.File=/usr/local/tomcat3/upgrade_logs/upgrade.log
log4j.appender.stdout.MaxFileSize=1000KB
log4j.appender.stdout.MaxBackupIndex=100
log4j.appender.stdout.Append=true
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%-5p][%d] -- %m%n
log4j.appender.stdout.Encoding=UTF-8
log4j.appender.A2=org.apache.log4j.ConsoleAppender
log4j.appender.A2.layout=org.apache.log4j.PatternLayout
log4j.appender.A2.layout.ConversionPattern=[%-5p][%d] -- %m%n
log4j.appender.A2.Encoding=UTF-8
#show application DEBUG log
#log4j.logger.sd = A2
#show ibatis DEBUG log
log4j.logger.com.ibatis = stdout
log4j.logger.com.ibatis.common.jdbc.SimpleDataSource = stdout
log4j.logger.com.ibatis.common.jdbc.ScriptRunner = stdout
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate = stdout
log4j.logger.java.sql.Connection = stdout
log4j.logger.java.sql.Statement = stdout
log4j.logger.java.sql.PreparedStatement = stdout
log4j.logger.java.sql.ResultSet = stdout