首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 綜合教學

Maven + SpringMVC + MyBatis 整合配置大全:pom.xml、applicationContext、spring-mvc、屬性文件與日誌

     編輯:Java 綜合教學

Maven + SpringMVC + MyBatis 整合配置大全:pom.xml、applicationContext、spring-mvc、屬性文件與日誌

模組結構

  • qyxx-web (WAR) 依賴 qyxx-core (JAR)
  • Maven Profile 區分 local/dev/pro 環境,對應 config/${env.type}.properties

核心配置文件

1. pom.xml (web 模組)

關鍵點:

  • 父工程管理版本,<packaging>war</packaging>
  • maven-compiler-plugin 鎖定 JDK 1.7
  • tomcat7-maven-plugin 支援 mvn tomcat7:redeploy 自動部署
  • <filters> + <resources>filtering</resources> 實現多環境屬性替換

2. applicationContext.xml (Spring 容器)

  • 掃描策略:context:component-scan base-package="com.zx" 排除 @Controller(留給 SpringMVC)。
  • 異步/緩存:<task:annotation-driven/> + <cache:annotation-driven/> + SimpleCacheManager。
  • 數據源:DruidDataSource,SpEL 注入 #{common['jdbc.xxx']},完整連接池參數(初始/最小/最大、驗證查詢、廢棄連接回收)。
  • 事務:DataSourceTransactionManager + tx:annotation-driven + tx:advice 定義傳播屬性 + aop:config 切入 com.zx.qyxx.service.*。
  • MyBatis:SqlSessionFactoryBean 引用 dataSource + mapperLocations=classpath:mapper/**/*Mapper.xml + configLocation=mybatis-config.xml;MapperScannerConfigurer 掃描 com.zx.qyxx.dao。

3. spring-mvc.xml (SpringMVC)

  • <mvc:annotation-driven> + MappingJackson2HttpMessageConverter 指定 UTF-8。
  • <mvc:default-servlet-handler/> 靜態資源透傳。
  • 攔截器 SignInterceptor 排除登入/驗證碼/回調等路徑。
  • InternalResourceViewResolver 前綴 /pages/ 後綴 .jsp。
  • <mvc:cors> 全局跨域支援。
  • 定時任務 <task:scheduled-tasks> 每 2 小時執行 listenerService.logout。

4. common.properties / log4j.properties / local.properties

  • common.properties:JDBC、微信/QQ 開放平台、Redis 連接信息。
  • log4j.properties:Console + DailyRollingFile (INFO/ERROR 分離)。
  • config/local.properties:開發環境具體 JDBC URL/帳密、回調域名。

部署建議

  1. mvn clean package -Plocal 產出 WAR。
  2. mvn tomcat7:redeploy -Plocal 一鍵發布。
  3. 生產環境切換 -Ppro,屬性文件自動替換,零代碼修改。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved