SSM框架+Maven+SwaggerUi+Mysql整合(JDK1.8)

开发工具:

1,Spring (4.3.3.RELEASE)
2,Mybatis(3.4.1)
3,Mysql
4,JDK1.8
5,Eclipse(Oxygen(4.7))
6,maven(3.6)
7,Swagger2

整合步骤:

一,构建maven工程

JDK就不说怎么弄了,大家都知道
1,下载maven,然后解压(路径自己定义)
2,配置环境变量(鼠标选中计算机图标右击)
SSM框架+Maven+SwaggerUi+Mysql整合(JDK1.8)_第1张图片
SSM框架+Maven+SwaggerUi+Mysql整合(JDK1.8)_第2张图片
3,配置maven本地仓库
在【maven解压路径】+\apache-maven-3.6.0\conf 文件夹下打开.setting.xml文件
SSM框架+Maven+SwaggerUi+Mysql整合(JDK1.8)_第3张图片
4,打开eclipse,添加maven配置文件
SSM框架+Maven+SwaggerUi+Mysql整合(JDK1.8)_第4张图片
5,创建maven工程(请事先配置好eclipse的工作空间编码格式等)
File->New->MavenProject
SSM框架+Maven+SwaggerUi+Mysql整合(JDK1.8)_第5张图片
SSM框架+Maven+SwaggerUi+Mysql整合(JDK1.8)_第6张图片
SSM框架+Maven+SwaggerUi+Mysql整合(JDK1.8)_第7张图片
tip:点击finish完成构建,如果报错(常见的webapp.jar什么的错误)就去找到本地仓库找到这个jar所在的文件夹, 把文件夹删了,再重新新建工程(网络还有说可以新建cat来解决的反正我试了没用
继续,此时工程构建完应该有报错,右击工程名->buildPath
SSM框架+Maven+SwaggerUi+Mysql整合(JDK1.8)_第8张图片
然后点击serverRuntime
SSM框架+Maven+SwaggerUi+Mysql整合(JDK1.8)_第9张图片
SSM框架+Maven+SwaggerUi+Mysql整合(JDK1.8)_第10张图片
tip:前提是要先添加一个服务器
此时工程应该不报错了,接着修改jdk为对应版本
SSM框架+Maven+SwaggerUi+Mysql整合(JDK1.8)_第11张图片
SSM框架+Maven+SwaggerUi+Mysql整合(JDK1.8)_第12张图片
打开Navigator视图,如果没有(window->show view->Navigator),修改配置文件
SSM框架+Maven+SwaggerUi+Mysql整合(JDK1.8)_第13张图片
SSM框架+Maven+SwaggerUi+Mysql整合(JDK1.8)_第14张图片
然后右击工程名->properties
SSM框架+Maven+SwaggerUi+Mysql整合(JDK1.8)_第15张图片
SSM框架+Maven+SwaggerUi+Mysql整合(JDK1.8)_第16张图片
修改web.xml
SSM框架+Maven+SwaggerUi+Mysql整合(JDK1.8)_第17张图片
此时update project应该会报错,接着把pom.xml依赖加进去就好了

二,SSM框架整合

1,添加依赖到pom.xml


  4.0.0
  com.swaggerMaven.ssm
  swaggerMaven
  war
  0.0.1-SNAPSHOT
  swaggerMaven Maven Webapp
  
 
		
			junit
			junit
			3.8.1
			test
		
		
		
		
		
		
			org.springframework
			spring-test
			4.3.3.RELEASE
		
		
			org.springframework
			spring-core
			4.3.3.RELEASE
		
		
		
			org.springframework
			spring-context
			4.3.3.RELEASE
		
		
		
			org.springframework
			spring-context-support
			4.3.3.RELEASE
			jar
			compile
		
		
		
			org.springframework
			spring-beans
			4.3.3.RELEASE
		
		
		
			org.springframework
			spring-expression
			4.3.3.RELEASE
		
		
 
		
		
		
			org.springframework
			spring-aop
			4.3.3.RELEASE
		
		
		
			org.springframework
			spring-aspects
			4.3.3.RELEASE
		
		
		
			org.aspectj
			aspectjrt
			1.8.5
		
		
			org.aspectj
			aspectjweaver
			1.8.5
		
		
 
		
		
		
			org.springframework
			spring-web
			4.3.3.RELEASE
		
		
		
			org.springframework
			spring-webmvc
			4.3.3.RELEASE
		
		
		
			org.springframework
			spring-websocket
			4.3.3.RELEASE
		
		
		
			org.springframework
			spring-webmvc-portlet
			4.3.3.RELEASE
		
 
		
 
		
			org.codehaus.jackson
			jackson-core-lgpl
			1.9.6
		
		
			org.codehaus.jackson
			jackson-core-asl
			1.9.4
		
		
			org.codehaus.jackson
			jackson-mapper-asl
			1.9.5
		
		
			org.codehaus.jackson
			jackson-mapper-lgpl
			1.9.6
		
 
		
 
		
		
		
			org.springframework
			spring-jdbc
			4.3.3.RELEASE
		
		
		
			org.springframework
			spring-tx
			4.3.3.RELEASE
		
		
		
			org.springframework
			spring-orm
			4.3.3.RELEASE
		
		
		
			org.springframework
			spring-jms
			4.3.3.RELEASE
		
		
		
			org.codehaus.jackson
			jackson-mapper-asl
			1.9.13
		
		
 
		
		
			org.mybatis
			mybatis
			3.4.1
		
		
			org.mybatis
			mybatis-spring
			1.3.0
		
		
			mysql
			mysql-connector-java
			5.1.21
		
		
			c3p0
			c3p0
			0.9.1.2
			jar
		
 		
 		
        	com.fasterxml.jackson.core
        	jackson-databind
        	2.9.3
    	
    	
        	com.fasterxml.jackson.core
        	jackson-core
        	2.9.3
    	
    	
       		com.fasterxml.jackson.core
        	jackson-annotations
        	2.9.3
    	
		
		
			org.slf4j
			slf4j-api
			1.7.22
		
		
			org.slf4j
			slf4j-log4j12
			1.7.22
		
		
			log4j
			log4j
			1.2.17
		
		
			jstl
			jstl
			1.2
		
 
		
		
 
		
			javax.servlet
			servlet-api
			2.5
			provided
		
		
			javax.servlet
			jsp-api
			2.0
			provided
		
	
  
    swaggerMaven
  
			
				src/main/resources
				
					**/*.properties
					**/*.xml
					**/*.tld
				
				false
			
			
				src/main/java
				
					**/*.properties
					**/*.xml
					**/*.tld
				
				false
			
		
 
		
			
				org.apache.maven.plugins
				maven-compiler-plugin
				3.1
				
					1.8
					1.8
					UTF-8
					true
					true
					true
				
			
 
			
				org.apache.maven.plugins
				maven-surefire-plugin
				2.4.3
				
					true
				
			
 
		
	


添加好了依赖,右击工程名->maven->update project(等待下载jar包)
此时工程不再报错
2,配置spring文件,在resoures文件夹下创建spring-context.xml



	 
	  
    
      
      
          
    
	
	
	    
	    
	    
	    
	    
	
    
    
	
		
		
		
		
	
    
	
		
		
		
	
    
   	
    
		
		
	
	
	
	
	
    
    



3,配置SpringMVC,在resources文件夹下创建文件spring-mvc.xml




    
        
            
                
                    
                        
                            text/html; charset=UTF-8
                            application/json;charset=UTF-8
                        
                    
                
                
                    
                        
                            text/html; charset=UTF-8
                            application/json;charset=UTF-8
                        
                    
                
            
        
     
        
                          
    
    
      
    
    
	
	
	
	
	
    
    
        
        
        
    



4,最后配置web.xml



  ssm
  
    encodingFilter
    org.springframework.web.filter.CharacterEncodingFilter
    true
    
      encoding
      UTF-8
    
  
  
    encodingFilter
    /*
  
  
    contextConfigLocation
    classpath:spring-context.xml
  
  
    org.springframework.web.context.ContextLoaderListener
  
  
    springDispatcherServlet
    org.springframework.web.servlet.DispatcherServlet
    
      contextConfigLocation
      classpath:spring-mvc.xml
    
    1
  
  
    springDispatcherServlet
    /
  
  
    springDispatcherServlet
    /service/*
  

5,创建配置文件jdbc.properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/ssm
username=root
password=000000

initialSize=0 
maxActive=20 
maxIdle=20
minIdle=1
maxWait=60000

6,创建配置文件log4j.properties

#\u5B9A\u4E49LOG\u8F93\u51FA\u7EA7\u522B 
log4j.rootLogger=INFO,Console,File 
#\u5B9A\u4E49\u65E5\u5FD7\u8F93\u51FA\u76EE\u7684\u5730\u4E3A\u63A7\u5236\u53F0  
log4j.appender.Console=org.apache.log4j.ConsoleAppender 
log4j.appender.Console.Target=System.out
#\u53EF\u4EE5\u7075\u6D3B\u5730\u6307\u5B9A\u65E5\u5FD7\u8F93\u51FA\u683C\u5F0F\uFF0C\u4E0B\u9762\u4E00\u884C\u662F\u6307\u5B9A\u5177\u4F53\u7684\u683C\u5F0F  
log4j.appender.Console.layout = org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n

#\u6587\u4EF6\u5927\u5C0F\u5230\u8FBE\u6307\u5B9A\u5C3A\u5BF8\u7684\u65F6\u5019\u4EA7\u751F\u4E00\u4E2A\u65B0\u7684\u6587\u4EF6
log4j.appender.File = org.apache.log4j.RollingFileAppender
#\u6307\u5B9A\u8F93\u51FA\u76EE\u5F55
log4j.appender.File.File = logs/ssm.log
#\u5B9A\u4E49\u6587\u4EF6\u6700\u5927\u5927\u5C0F
log4j.appender.File.MaxFileSize = 10MB
# \u8F93\u51FA\u6240\u4EE5\u65E5\u5FD7\uFF0C\u5982\u679C\u6362\u6210DEBUG\u8868\u793A\u8F93\u51FADEBUG\u4EE5\u4E0A\u7EA7\u522B\u65E5\u5FD7
log4j.appender.File.Threshold = ALL
log4j.appender.File.layout = org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n

到此SSM已经配置完毕,开始写测试类测试一下
编写controller类

package com.ssm.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ssm.service.UserService;

@Controller
@RequestMapping("userController")
public class UserController {
	
	@Resource
	private UserService uservice;
	
	@RequestMapping(value="login")
	@ResponseBody
	public List> login(@RequestParam Map param){
		List> rstList = uservice.login(param);
		return rstList;
	}
}

编写service

package com.ssm.service;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.ssm.dao.UserDao;

@Service
public class UserService {
	
	@Resource
	private UserDao userDao;
	
	public List> login(Map param){
		return userDao.queryUserInfo(param);
	}
}

编写Dao

package com.ssm.dao;

import java.util.List;
import java.util.Map;

public interface UserDao {
	public List> queryUserInfo(Map param);
}

编写mapper.xml





	
	



前端jsp测试

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




测试页面



登录测试

点击测试

SSM框架+Maven+SwaggerUi+Mysql整合(JDK1.8)_第18张图片
OK,SSM框架已经打好,接下来集成Swagger

三,集成Swagger

1,添加依赖

		
            io.springfox
            springfox-swagger2
            2.5.0
        
        
            io.springfox
            springfox-swagger-ui
            2.5.0
        

2,编写Swagger配置类

package com.ssm.swagger;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@ComponentScan(basePackages="com.controller")
@Configuration
@EnableSwagger2
@EnableWebMvc
public class SwaggerConfig {
	@Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .build()
                .apiInfo(apiInfo());
    }
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Test项目接口文档")
                .description("test项目接口测试")
                .version("1.0.0")
                .termsOfServiceUrl("")
                .license("")
                .licenseUrl("")
                .build();
    }
}

3,spring-mvc.xml配置

	
	
	
	
	
	

4,简添加一下注解进行测试
SSM框架+Maven+SwaggerUi+Mysql整合(JDK1.8)_第19张图片
5,在浏览器中输入localhost:8080/工程名/swagger-ui.html
SSM框架+Maven+SwaggerUi+Mysql整合(JDK1.8)_第20张图片
OK,一切搞定

你可能感兴趣的:(程序员)