1. 程式人生 > >SpringBoot整合SpringMvc+Mybatis+Swagger

SpringBoot整合SpringMvc+Mybatis+Swagger

SpringBoot+SpringMvc+Mybatis整合

SpringBoot+SpringMvc+Mybatis+Swagger簡單的整合步驟 。使用Eclipse工具1、新建一個maven專案

1、建立一個Maven工程

2、pom.xm設定

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>cn.itcast.demo</groupId>
  <artifactId>springBoot-springMvc-hibernate</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <properties>
  	<java.version>1.7</java.version>  
  </properties>
  
  <!-- 引入springBoot -->
  <parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>1.4.0.RELEASE</version>
  </parent>
  
  <!-- 配置中央倉庫 -->
  <repositories>
     <repository>
         <id>alimaven</id>
         <name>aliyun maven</name>
         <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
     </repository>
  </repositories>
  
  <dependencies>
  
        <!-- 整合 springMvc -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		
		<!-- 整合 Hibernate -->
		<dependency>
	        <groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>
		
		<!-- 模板 thymeleaf Demo -->
		<dependency>
	      <groupId>org.springframework.boot</groupId>
	      <artifactId>spring-boot-starter-thymeleaf</artifactId>
	    </dependency>

		<!-- swagger 介面文件 -->
		<dependency>
	        <groupId>io.springfox</groupId>
	        <artifactId>springfox-swagger2</artifactId>
	        <version>2.2.2</version>
	    </dependency>	
	    <dependency>
	        <groupId>io.springfox</groupId>
	        <artifactId>springfox-swagger-ui</artifactId>
	        <version>2.2.2</version>
	    </dependency>
		
		<!-- 熱部署 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<optional>true</optional>
		</dependency>
		
		<!-- 單元測試 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		
		<!-- 資料庫連線池 -->
        <!-- <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency> -->
		
		<!-- mysql -->
		<dependency>
	        <groupId>mysql</groupId>
	        <artifactId>mysql-connector-java</artifactId>
    	</dependency>   
    	
    	<!-- oracle -->
    	<dependency>
			<groupId>com.oracle</groupId>
			<artifactId>ojdbc6</artifactId>
			<version>11.2.0.1.0</version>
		</dependency>
  </dependencies>
</project>

3、配置application.properties

#mysql資料庫配置
spring.datasource.url=jdbc:mysql://xxx.xxx.xx.xx:3306/springmvc?characterEncoding=utf-8
spring.datasource.username=username
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
​
#oracle\u6570\u636E\u5E93\u914D\u7F6E
#spring.datasource.url=jdbc:oracle:thin:@xxx.xxx.xx.xx:1521:orcl
#spring.datasource.username=username
#spring.datasource.password=password
#spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
​
mybatis.type-aliases-package=com.springboot.pojo
server.port=8080
​
#Swagger Default Access Address : http://localhost:8080/swagger-ui.html

3、mapper層內容省略 包名為:com.springboot.mapper

4、pojo層

package com.springboot.pojo;
​
import java.util.Date;
​
public class User {
    private Integer id;
    private String username;
    private Date birthday;
    private String sex;
    private String address;
    省略set/get方法
}

5、service層介面類

public interface UserService {
    public List<User> getAll();
    public User selectUserById(int id);
}

6、service 層實現類

package com.springboot.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.springboot.mapper.UserMapper;
import com.springboot.pojo.User;
import com.springboot.pojo.UserExample;
import com.springboot.service.UserService;
​
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public List<User> getAll() {
        UserExample example = new UserExample();
        example.createCriteria().andIdBetween(1, 50);
        List<User> userList = userMapper.selectByExample(example);
        return userList;
    }
​
    @Override
    public User selectUserById(int id) {
        return userMapper.selectByPrimaryKey(id);
    }
}

7、cotroller控制層

package com.springboot.web;
​
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.springboot.pojo.User;
import com.springboot.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
​
@Api(value = "查詢使用者資訊")
@RestController
public class UserController {
    
    @Autowired
    private UserService userService;
 
    @ApiOperation(value="根據使用者 ID 獲取使用者詳細資訊")
    @GetMapping(value = "getUserById/{id}")
    public User getUserById(@RequestParam(name = "id", required = true) Integer id){
        User user = userService.selectUserById(id);
        return user;
    }
    
    @ApiOperation(value="根據使用者 ID 獲取使用者詳細資訊   全部列表")
    @GetMapping(value = "getAllUsers")
    public List<User> getAllUser(){
        List<User> userList = userService.getAll();
        return userList;
    }
}
​

8、Application啟動類

package com.springboot.application;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import springfox.documentation.swagger2.annotations.EnableSwagger2;

@SpringBootApplication(scanBasePackages = {"com.springboot"}) // 掃描該路徑下的所有spring元件
@MapperScan(basePackages = {"com.springboot.mapper"}) // 掃描Mapper實體類
@EnableSwagger2 // Swagger
public class Application {

	public static void main(String[] args) {		
		SpringApplication.run(Application.class, args);
	}

}

9、配置Swagger 配置類,放在啟動類掃描的包下,建議和啟動類放在同一目錄下;

package com.springboot.application;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

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

@Configuration
public class SwaggerConfig {

	@Bean
	public Docket createRestApi() {
		return new Docket(DocumentationType.SWAGGER_2)
				.apiInfo(apiInfo())
				.select()
				.apis(RequestHandlerSelectors.basePackage("com.springboot.web"))
				.paths(PathSelectors.any())
				.build();
	}
	
	private ApiInfo apiInfo() {
		return new ApiInfoBuilder()
				.title("這是我的springBoot API 介面文件")
				.description("簡單優雅的restfun風格,Swagger介面測試用")
				.termsOfServiceUrl("http://blog.csdn.net/saytime")
				.version("2.0")
				.build();
	}
}

Run as --- java Application就可以啟動專案

注意:

maven除了本地化之外,還需在 .../conf/setting.xml檔案中,增加以下配置,主要和工程中配置中央倉庫對應,本地倉庫沒有的依賴jar包,可以從中央倉庫中下載。

    <mirrors>
        <mirror>  
          <id>alimaven</id>  
          <name>aliyun maven</name>  
          <url>http://maven.aliyun.com/nexus/content/groups/public/</url>  
          <mirrorOf>central</mirrorOf>          
        </mirror>
      </mirrors>

 

SpringBoot整合SpringMvc+Mybatis