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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值