JavaWeb项目整合Spring,SpringMVC,Mybatis框架

衔接上篇:
使用IDEA创建基于Gradle构建JavaWeb项目

版本信息
  • spring 4.4.13
  • mybatis 3.4.1

One Step!

根据所需,导入相应jar包,添加依赖。
    //spring 系列包 4.4.13
    // spring mvc
    compile group: 'org.springframework', name: 'spring-webmvc', version: '4.3.13.RELEASE'
    // spring 核心包
    compile group: 'org.springframework', name: 'spring-core', version: '4.3.13.RELEASE'
    // spring beans
    compile group: 'org.springframework', name: 'spring-beans', version: '4.3.13.RELEASE'
    // spring 上下文
    compile group: 'org.springframework', name: 'spring-context', version: '4.3.13.RELEASE'
    // spring web
    compile group: 'org.springframework', name: 'spring-web', version: '4.3.13.RELEASE'
    // spring orm
    compile group: 'org.springframework', name: 'spring-orm', version: '4.3.13.RELEASE'
    // spring测试包
    compile group: 'org.springframework', name: 'spring-test', version: '4.3.13.RELEASE'
    // 日志记录 1.7.25
    compile group: 'org.slf4j', name: 'slf4j-simple', version: '1.7.25'
    // 数据库连接 5.1.38
    compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.38'
    // 数据库连接池 2.7.7
    compile group: 'com.zaxxer', name: 'HikariCP', version: '2.7.7'
    // spring与Mybatis整合 1.3.0
    compile group: 'org.mybatis', name: 'mybatis-spring', version: '1.3.0'
    // mybatis 3.4.1
    compile group: 'org.mybatis', name: 'mybatis', version: '3.4.1'

这里使用的连接池为HikariCP,没了解过的建议百度一下。

Two Step!

进行相关的基础配置

修改web.xml,如下图所示。

 
    
        index.jsp
    
    
    
        SetCharacterEncoding
        org.springframework.web.filter.CharacterEncodingFilter
        
            encoding
            UTF-8
        
        
            forceEncoding
            true
            
        
    
    
        SetCharacterEncoding
        /*
    
    
    
    
        contextConfigLocation
        classpath:/spring/SpringApplicationContext.xml
    
    
    
        org.springframework.web.context.ContextLoaderListener
    
    
    
        springMVC
        org.springframework.web.servlet.DispatcherServlet
        
            
            contextConfigLocation
            classpath:spring/SpringMVC.xml
        
        1
        true
    
    
    
        springMVC
        /
    

在Resources目录下创建SpirngApplicationContext.xml,SpringMVC.xmljdbc.propertieslogback.xml配置文件。

JavaWeb项目整合Spring,SpringMVC,Mybatis框架_第1张图片

SpirngApplicationContext.xml


    
    
    
    
    
    
    
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        

    
    
        
        
        
        
        
        
    
    
    
        
        
    
    
    
        
        
    
    
    

SpringMVC.xml


    
    
    
    
    
    
    
    
        
        
    
    

jdbc.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf-8&useSSL=false
jdbc.username=root
jdbc.password=123456
jdbc.readOnly=false
jdbc.connectionTimeout=30000
jdbc.idleTimeout=600000
jdbc.maxLifetime=1800000
jdbc.maximumPoolSize=60
jdbc.minimumIdle=10
logback.xml


    
        
            %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
        
    
    
        
    

添加完配置文件不要忘了Create new application context。
对于SpringMVC.xml也是如此。


JavaWeb项目整合Spring,SpringMVC,Mybatis框架_第2张图片

到这里我们基本已经配置完成,所以剩下一步,我们只需要测试一下是否成功运行就可以了。

Three Step!

测试是否整合成功

运行Tomcat,没有出现404,说明我们成功了第一步
接下来,我测试一下是否能够根据id查找出数据库中的信息


JavaWeb项目整合Spring,SpringMVC,Mybatis框架_第3张图片

页面并没有输出信息,结果出现了500错误。原因是缺少jar包。所以Gradle要添加以下依赖。

// 页面输出json数据所需jar包
    compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.9.0'
    compile group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.9.0'
    compile group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: '2.9.0'

再次测试,成功运行输出内容。


JavaWeb项目整合Spring,SpringMVC,Mybatis框架_第4张图片

此时我们的SSM框架基本整合成功!!

下一节!SSM项目添加swagger2支持

项目地址:https://github.com/isliqian/ssm
个人博客:www.imqian.top

你可能感兴趣的:(JavaWeb项目整合Spring,SpringMVC,Mybatis框架)