使用intellij idea搭建MAVEN+springmvc+mybatis框架


1、首先使用idea创建一个maven项目

2、接着配置pom.xml,以下为我的配置


  4.0.0
  com.liuwenjian
  angular-demo
  war
  1.0-SNAPSHOT
  angular-demo Maven Webapp
  http://maven.apache.org
  
    UTF-8
    4.2.5.RELEASE
  
    
      
        junit
        junit
        4.12
        test
      
      
      
        org.springframework
        spring-aop
        ${spring.version}
        jar
        compile
      
      
        org.springframework
        spring-aspects
        ${spring.version}
        jar
        compile
      
      
        org.springframework
        spring-beans
        ${spring.version}
        jar
        compile
      
      
        org.springframework
        spring-context
        ${spring.version}
        jar
        compile
      
      
        org.springframework
        spring-context-support
        ${spring.version}
        jar
        compile
      
      
        org.springframework
        spring-core
        ${spring.version}
        
          
            commons-logging
            commons-logging
          
        
        jar
        compile
      
      
        org.springframework
        spring-expression
        ${spring.version}
        jar
        compile
      
      
        org.springframework
        spring-jdbc
        ${spring.version}
        jar
        compile
      
      
        org.springframework
        spring-jms
        ${spring.version}
        jar
        compile
      
      
        org.springframework
        spring-orm
        ${spring.version}
        jar
        compile
      
      
        org.springframework
        spring-oxm
        ${spring.version}
        jar
        compile
      
      
        org.springframework
        spring-tx
        ${spring.version}
        jar
        compile
      
      
        org.springframework
        spring-web
        ${spring.version}
        jar
        compile
      
      
        org.springframework
        spring-webmvc
        ${spring.version}
        jar
        compile
      
      
        org.springframework
        spring-test
        ${spring.version}
        jar
        compile
      

      
        javax.servlet
        jstl
        1.2
        jar
        compile
      

      
        commons-logging
        commons-logging
        1.1.1
      

      
        commons-collections
        commons-collections
        3.2.1
      

      
        commons-dbcp
        commons-dbcp
        1.4
      

      
        commons-pool
        commons-pool
        1.5.4
      
      
      
        org.mybatis
        mybatis
        3.3.1
      
      
      
        org.mybatis
        mybatis-spring
        1.2.4
      
      
      
        mysql
        mysql-connector-java
        5.1.26
      
      
      
        c3p0
        c3p0
        0.9.1.2
      
      
      
        log4j
        log4j
        1.2.14
      
      
      
        javax.servlet
        servlet-api
        2.5
      
      
        javax.servlet
        jstl
        1.2
      
      
      
        org.freemarker
        freemarker
        2.3.23
      
      
      
        commons-io
        commons-io
        2.4
      

      
        commons-fileupload
        commons-fileupload
        1.3
      

      
      
        com.google.code.gson
        gson
        2.2.2
      

      
      
        org.tuckey
        urlrewritefilter
        4.0.3
      
    
  
    angular-demo
    
      
        org.eclipse.jetty
        jetty-maven-plugin
        9.3.7.v20160115
        
          
          10
          automatic
          9966
          foo
          10
          
            
            /
          
          
            
            9080
            60000
          
          
          
            
            target/yyyy_mm_dd.request.log
            
            
            90
            
            true
            false
            
            GMT+8:00
          
        
      
    
  

  2、配置web.xml


  Archetype Created Web Application
    springMVC

    
    
        contextConfigLocation
        classpath:/config/applicationContext.xml
    
    
    
        encodingFilter
        org.springframework.web.filter.CharacterEncodingFilter
        true
        
            encoding
            UTF-8
        
    
    
        encodingFilter
        /*
    

    
    
        UrlRewriteFilter
        org.tuckey.web.filters.urlrewrite.UrlRewriteFilter
    
    
        UrlRewriteFilter
        /*
        REQUEST
        FORWARD
    

    
    
        
        log4jConfigLocation
        classpath:config/log4j.properties
    
    
        
        log4jRefreshInterval
        6000
    
    
        org.springframework.web.util.Log4jConfigListener
    

    
    
        org.springframework.web.context.ContextLoaderListener
    

    
    
        springMVC
        org.springframework.web.servlet.DispatcherServlet
        
        
            contextConfigLocation
            classpath:config/spring-mvc.xml
        
        1
    

    
    
        springMVC
        /
    

    
        404
        /error/404
    

    
        500
        /error/500
    

    
        /index.jsp
        /index.htm
        /index.html
    

  3、在resources/config目录下新建applicationContext.xml文件

<?xml version="1.0" encoding="UTF-8"?>


    
    
    

    
    
    
        
    

    
    
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
    

    
    
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
    

    
    
        
        
            
                
                
            
        
    

    
    
        
        
        
        
        
    

    
    
        
        
    

    
    
        
    

    
    
        
        
            
            
            
            
            
            
            
            
            
            
            
            
            
            
        
    

    
    
        
        
    

  在文件中引入的jdbc.properties文件

#============================================================================
# MySQL
#============================================================================
jdbc.mysql.driver=com.mysql.jdbc.Driver
jdbc.mysql.url=jdbc:mysql://localhost:9081/angular-demo?useUnicode=true&characterEncoding=utf8
jdbc.mysql.username=root
jdbc.mysql.password=root

#============================================================================
# MS SQL Server
#============================================================================
jdbc.sqlserver.driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
jdbc.sqlserver.url=jdbc:sqlserver://127.0.0.1:1433;database=angular-demo;
jdbc.sqlserver.username=root
jdbc.sqlserver.password=root

#============================================================================
# ORACLE
#============================================================================
jdbc.orcl.driver=oracle.jdbc.OracleDriver
jdbc.orcl.url=jdbc:oracle:thin:@localhost:1526:angular-demo
jdbc.orcl.username=root
jdbc.orcl.password=root

#============================================================================
# common settings
#============================================================================
jdbc.initialSize=5
jdbc.minIdle=5
jdbc.maxIdle=20
jdbc.maxActive=100
jdbc.maxWait=100000
jdbc.defaultAutoCommit=false
jdbc.removeAbandoned=true
jdbc.removeAbandonedTimeout=600
jdbc.testWhileIdle=true
jdbc.timeBetweenEvictionRunsMillis=60000
jdbc.numTestsPerEvictionRun=20
jdbc.minEvictableIdleTimeMillis=300000

  mybatis-config.xml配置

<?xml version="1.0" encoding="UTF-8"?>


    
        
        
        
    
    
        
    

  spring-mvc.xml配置

前端模板解析最常用的是内置jsp,velocity和freemarker,这边选择的是freemarker

<?xml version="1.0" encoding="UTF-8"?>


    
    

    
    

    
    
    
    
    

    
    
        
    

    
    
        
            
                
                /error/upload_error
            
        
    

    
    
        
    

    
    
        
            /WEB-INF/views/
        
        
            
                
            
        
        
    

    

    
    
        
        
        
        
            org.springframework.web.servlet.view.freemarker.FreeMarkerView
            
        
        
        
        
        
        
        
        
        
    
  

  freemarker.properties配置

tag_syntax=auto_detect
template_update_delay=60//模板更新时间
default_encoding=UTF-8//默认编码
output_encoding=UTF-8//输出编码
locale=zh_CN//使用语言
date_format=yyyy-MM-dd
time_format=HH:mm:ss
datetime_format=yyyy-MM-dd HH:mm:ss
number_format=0.##########
classic_compatible=true
template_exception_handler=ignore//模板类型处理

  log4j.properties配置

##define output style
ConversionPattern=%d %-5p [%t] %c - %m%n

log4j.rootLogger=DEBUG,Console
log4j.logger.com.cnblogs.lzrabbit=DEBUG
log4j.logger.org.springframework=ERROR
log4j.logger.org.mybatis=ERROR
log4j.logger.org.apache.ibatis=ERROR
log4j.logger.org.quartz=ERROR
log4j.logger.org.apache.axis2=ERROR
log4j.logger.org.apache.axiom=ERROR
log4j.logger.org.apache=ERROR
log4j.logger.httpclient=ERROR
#log4j.additivity.org.springframework=false
#Console 
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Threshold=DEBUG  
log4j.appender.Console.Target=System.out  
log4j.appender.Console.layout=org.apache.log4j.PatternLayout  
log4j.appender.Console.layout.ConversionPattern=${ConversionPattern}
#log4j.appender.Console.encoding=UTF-8

#org.apache.log4j.DailyRollingFileAppender
log4j.appender.DailyFile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.DailyFile.DatePattern='.'yyyy-MM-dd'.log'
log4j.appender.DailyFile.File=${myApp.root}/logs/daily.log
log4j.appender.DailyFile.Append=true
log4j.appender.DailyFile.Threshold=DEBUG
log4j.appender.DailyFile.layout=org.apache.log4j.PatternLayout
log4j.appender.DailyFile.layout.ConversionPattern=${ConversionPattern}
log4j.appender.DailyFile.encoding=UTF-8

#    %c 输出日志信息所属的类的全名
#    %d 输出日志时间点的日期或时间,默认格式为ISO8601,也可以在其后指定格式,比如:%d{yyy-MM-dd HH:mm:ss},
#      输出类似:2016-03-10 22:10:11
#    %f 输出日志信息所属的类的类名
#    %l 输出日志事件的发生位置,即输出日志信息的的语句处于它所在的类的第几行
#    %m 输出代码中指定的信息,如log(message)中的message
#    %n 输出一个回车换行符,Windows平台为"rn",Unix平台为"n"
#    %p 输出优先级,即DEBUG.INFO,WARN,ERROR,FATAL.如果是调用debug()输出的,则为DEBUG,以此类推
#    %r 输出自应用启动到输出该日志信息所耗费的毫秒数
#    %t 输出产生该日志的线程名

在pom文件内部,已添加内嵌jetty服务器,可以直接使用。

配置完成,就可以使用内嵌的服务器启动了