Maven+SSM:Spring、SpringMVC、Mybatis项目整合

Maven+IntelliJ IDEA、SSM:Spring、SpringMVC、Mybatis项目整合

在这里小小推荐下我的个人博客

csdn:雷园的csdn博客

个人博客:雷园的个人博客

:雷园的

本文将介绍如何搭建ssm项目框架

  • 加入依赖
  • 编写配置
  • 链接数据库
  • 编写测试类
  • 项目结构图

加入依赖pom.xml


    

        
        
            com.fasterxml.jackson.core
            jackson-core
            2.8.8
        
        
            com.fasterxml.jackson.core
            jackson-annotations
            2.8.8
        
        
            com.fasterxml.jackson.core
            jackson-databind
            2.8.8
        

        
        
            org.json
            json
            20180130
        

        
        
            commons-beanutils
            commons-beanutils
            20030211.134440
        
        
            commons-lang
            commons-lang
            2.6
        
        
            commons-lang
            commons-lang
            2.6
        
        
            asm
            asm-commons
            3.3
        
        
            commons-logging
            commons-logging
            1.2
        
        
            commons-collections
            commons-collections
            3.2
        
        
            commons-fileupload
            commons-fileupload
            1.3.1
        
        
            commons-io
            commons-io
            2.2
        
        
            org.apache.commons
            commons-lang3
            3.2
        

        
        
            net.sf.ezmorph
            ezmorph
            1.0.6
        
        
            org.eclipse.jetty
            jetty-util
            9.3.7.v20160115
        
        
            org.apache.httpcomponents
            httpclient
            4.4
        
        
            org.apache.httpcomponents
            httpcore
            4.4
        
        
            org.eclipse.jetty
            jetty-util
            9.3.7.v20160115
        

        
        
            com.alibaba
            fastjson
            1.2.15
        
        
            com.aliyun
            aliyun-java-sdk-core
            3.7.1
        
        
            com.aliyun
            aliyun-java-sdk-dysmsapi
            1.1.0
        
        
            com.alibaba
            druid
            1.1.9
        

        
        
            junit
            junit
            4.12
        
        
            antlr
            antlr
            2.7.7
        
        
            asm
            asm
            3.3
        
        
            asm
            asm-tree
            3.3
        
        
            aopalliance
            aopalliance
            1.0
        
        
            log4j
            log4j
            1.2.17
        
        
            org.aspectj
            aspectjweaver
            1.8.13
        
        
            dom4j
            dom4j
            1.6.1
        
        
            org.freemarker
            freemarker
            2.3.22
        
        
            javassist
            javassist
            3.11.0.GA
        
        
            javax.annotation
            javax.annotation-api
            1.3.2
        
        
            javax.transaction
            jta
            1.1
        

        
        
            org.mybatis
            mybatis
            3.4.6
        
        
            org.mybatis
            mybatis-spring
            1.3.2
        
        
            mysql
            mysql-connector-java
            5.1.25
        
        
            ognl
            ognl
            3.0.6
        
        
            javax.servlet
            servlet-api
            2.5
        
        
            org.slf4j
            slf4j-api
            1.7.25
        
        
        
            org.springframework
            spring-web
            4.3.3.RELEASE
        
        
            org.springframework
            spring-expression
            4.3.3.RELEASE
        
        
            org.springframework
            spring-jdbc
            4.3.3.RELEASE
        
        
            org.springframework
            spring-webmvc
            4.3.3.RELEASE
        
        
            org.springframework
            spring-tx
            4.3.3.RELEASE
        
        
            org.springframework
            spring-test
            4.3.3.RELEASE
        
        
            org.springframework
            spring-orm
            4.3.3.RELEASE
        
        
        
            jstl
            jstl
            1.2
        
        
        
            org.mybatis.generator
            mybatis-generator
            1.3.6
        
        
            org.mybatis.generator
            mybatis-generator-core
            1.3.6
        
        
            org.mybatis.generator
            mybatis-generator-maven-plugin
            1.3.2
        
        
            javax.servlet
            jsp-api
            2.0
        
        
        
            org.apache.poi
            poi-ooxml
            3.14-beta1
        
        
            org.apache.poi
            poi-ooxml-schemas
            3.14-beta1
        
        
            org.apache.poi
            poi
            3.14-beta1
        
        
            org.apache.httpcomponents
            httpclient
            4.5.2
        

    

使用阿里巴巴数据源配置数据库jdbc.properties

url:jdbc:mysql://172.7.52.231:3306/first_ssm?useUnicode=true&characterEncoding=utf8
driverClassName:com.mysql.jdbc.Driver
username:root
password:123456
#------------------------------------------------------------------------------------------
#配置扩展插件 监控统计用filters:stat 日志用filters:log4j 防御sql注入用filters:wall
filters:stat
#最大连接池数量  初始化建立物理连接的个数  获取连接时最长的等待时间  最小连接池数量  maxIdle已经弃用
maxActive:20
initialSize:1
maxWait:60000
minIdle:10
maxIdle:15
#有两个含义 1.Destroy 线程会检测连接的时间 2.testWhileIdle的判断依据
timeBetweenEvictionRunsMillis:60000
#Destory线程中如果检测到当前连接的最后活跃时间和当前时间的差值大于minEvictableIdleTimeMillis,则关闭当前连接
minEvictableIdleTimeMillis:300000
#用来检测连接是否的sql,要求是一个查询语句。在mysql中通常设置为SELECT 'X'
validationQuery:SELECT 'x'
#申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery连接是否有效
testWhileIdle:true
#申请连接时执行validationQuery检测连接是否有效 这个配置会降低性能
testOnBorrow:false
#归还连接时执行validationQuery检测连接是否有效 这个配置会降低性能
testOnReturn:false
#要启用PSCache,必须配置大于0,当大于0时,poolPreparedStatements自动触发修改为true
maxOpenPreparedStatements:20
#对于建立连接超过removeAbandonedTimeout的连接强制关闭
removeAbandoned:true
#指定连接建立多长就被强制关闭
removeAbandonedTimeout:1800
#指定发生removeabandoned时,是否记录当前线程的堆栈信息到日志中
logAbandoned:true

编写配置文件

spring-mybatis.xml




    
    
    
    
    
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
    
    
    
        
        
        
        
        
        
        
    
    
    
        
        
    

spring-web.xml



    


    
    
    
    
    
    
    
        
        
        
    
    
    
    
    
        
    

spring-service.xml



       
    
    
    
        
    
    
    

Configuration.xml






   
    
    
    
    
    
    
  

编写junit单元测试类

import com.aliyuncs.exceptions.ClientException;
import com.ambow.first.dao.TypeMapper;
import com.ambow.first.util.HttpUtils;
import com.ambow.first.util.Send;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.HashMap;
import java.util.Map;

// SpringJUnit
@RunWith(SpringJUnit4ClassRunner.class)
// 加入配置
@ContextConfiguration({"/spring-mybatis.xml", "/spring-service.xml"})
public class Test {
    @Autowired
    private TypeMapper typeMapper;

    @org.junit.Test
    public void testType() {
        System.out.println(typeMapper.queryAll().toString());
    }
}

junit运行结果如下

log4j:WARN No appenders could be found for logger (org.springframework.test.context.junit4.SpringJUnit4ClassRunner).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
[Type{id='0e76f3b8-f576-4b58-9fc3-02ecacff0a5b', name='计算机', place='F1A13', bookNum=3}, Type{id='25b6c373-2d79-4ea2-8fa9-e976e74a05c7', name='小学题库', place='F2 A1', bookNum=0}, Type{id='494f33b3-6edd-4bdc-b4a0-a4c14de6c0e6', name='恐怖', place='F1 A3', bookNum=0}, Type{id='560aecb1-4f25-4738-843d-56c4a1b52957', name='数学', place='F2A13', bookNum=1}, Type{id='573651a8-9fc3-4a76-806b-3e63860af279', name='宗教', place='F1  B12', bookNum=0}, Type{id='67ab626a-c54d-4166-a6b3-6ead775bbca5', name='天文学', place='F1B13', bookNum=1}, Type{id='7de851a4-4b22-46e8-9b74-797d95c4033e', name='机械自动化', place='F2A22', bookNum=2}, Type{id='b0339bdb-d3a5-4a8e-8c8c-e16b5a978aea', name='英语', place='F2A1', bookNum=2}, Type{id='e051ac2e-87c9-4b30-8f31-72e432c67c13', name='自然科学', place='F1A13', bookNum=2}, Type{id='e9b20bbf-02e3-4855-9a2e-0ba01dfca81a', name='菜谱', place='F2A2', bookNum=2}]

项目结构图如下

Maven+SSM:Spring、SpringMVC、Mybatis项目整合_第1张图片
image

你可能感兴趣的:(Maven+SSM:Spring、SpringMVC、Mybatis项目整合)