一、 基本环境

1. JDK1.8 

2. Maven 3.6.0 国内镜像地址 https://maven.aliyun.com/repository/central

二、 准备阶段

  1. 项目名称 springBoot-root

  2. repository 配置


<repository>
   <id>aliyun</id>
   <url>https://maven.aliyun.com/repository/central</url>
   <releases>
       <enabled>true</enabled>
       <updatePolicy>always</updatePolicy>
   </releases>
   <snapshots>
       <enabled>false</enabled>
   </snapshots>
</repository>
  1. pluginRepository配置


<pluginRepository>
   <id>aliyun</id>
   <url>https://maven.aliyun.com/repository/central</url>
   <releases>
       <enabled>true</enabled>
       <updatePolicy>always</updatePolicy>
   </releases>
</pluginRepository>
  1. 或settings.xml


<mirror>
   <id>aliyun</id>
   <mirrorOf>aliyun</mirrorOf>
   <name>aliyun</name>
   <url>https://maven.aliyun.com/repository/public</url>
</mirror>

三、 搭建阶段-父POM

  1. spring boot 父依赖


<parent>
   <artifactId>spring-boot-starter-parent</artifactId>
   <groupId>org.springframework.boot</groupId>
   <version>2.3.7.RELEASE</version>
</parent>
  1. spring cloud dependencies


<dependencies>
   <dependency>
       <groupId>org.springframework.cloud</groupId>
       <artifactId>spring-cloud-dependencies</artifactId>
       <version>Hoxton.SR9</version>
       <type>pom</type>
       <scope>import</scope>
   </dependency>
</dependencies>
  1. spring cloud dependencies 版本对照表,新版本查询地址:https://start.spring.io/actuator/info

{
 "Finchley.M2": "Spring Boot >=2.0.0.M3 and <2.0.0.M5",
 "Finchley.M3": "Spring Boot >=2.0.0.M5 and <=2.0.0.M5",
 "Finchley.M4": "Spring Boot >=2.0.0.M6 and <=2.0.0.M6",
 "Finchley.M5": "Spring Boot >=2.0.0.M7 and <=2.0.0.M7",
 "Finchley.M6": "Spring Boot >=2.0.0.RC1 and <=2.0.0.RC1",
 "Finchley.M7": "Spring Boot >=2.0.0.RC2 and <=2.0.0.RC2",
 "Finchley.M9": "Spring Boot >=2.0.0.RELEASE and <=2.0.0.RELEASE",
 "Finchley.RC1": "Spring Boot >=2.0.1.RELEASE and <2.0.2.RELEASE",
 "Finchley.RC2": "Spring Boot >=2.0.2.RELEASE and <2.0.3.RELEASE",
 "Finchley.SR4": "Spring Boot >=2.0.3.RELEASE and <2.0.999.BUILD-SNAPSHOT",
 "Finchley.BUILD-SNAPSHOT": "Spring Boot >=2.0.999.BUILD-SNAPSHOT and <2.1.0.M3",
 "Greenwich.M1": "Spring Boot >=2.1.0.M3 and <2.1.0.RELEASE",
 "Greenwich.SR5": "Spring Boot >=2.1.0.RELEASE and <2.1.15.BUILD-SNAPSHOT",
 "Greenwich.BUILD-SNAPSHOT": "Spring Boot >=2.1.15.BUILD-SNAPSHOT and <2.2.0.M4",
 "Hoxton.SR4": "Spring Boot >=2.2.0.M4 and <2.3.0.BUILD-SNAPSHOT",
 "Hoxton.BUILD-SNAPSHOT": "Spring Boot >=2.3.0.BUILD-SNAPSHOT"
}
  1. Maven 插件 a. resources插件负责处理项目资源文件并拷贝到输出目录 b. compiler插件负责版本编译 c. 其他组件版本定义


<properties>
   <maven.compiler.source>1.8</maven.compiler.source>
   <maven.compiler.target>1.8</maven.compiler.target>
   <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
   <swagger.version>2.9.2</swagger.version>
   <mybatis-starter.version>2.1.4</mybatis-starter.version>
</properties>

<dependency>
   <groupId>io.springfox</groupId>
   <artifactId>springfox-swagger2</artifactId>
   <version>${swagger.version}</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>${swagger.version}</version>
</dependency>
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>${mybatis-starter.version}</version>
</dependency>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <configuration>
        <encoding>${project.build.sourceEncoding}</encoding>
    </configuration>
</plugin>

<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-compiler-plugin</artifactId>
   <configuration>
       <source>${maven.compiler.source}</source>
       <target>${maven.compiler.target}</target>
   </configuration>
</plugin>

四、 搭建阶段-Eureka

  1. 新建子模块:myEureka

  2. 引入Server依赖 和 添加springBoot maven plugin


<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

<plugin>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
  1. 新建包名:com.elead.it

  2. 添加启动类 EurekaStarter.java


@EnableEurekaServer
@SpringBootApplication
public class EurekaStarter {
   public static void main(String[] args) {
       SpringApplication.run(EurekaStarter.class);
  }
}
  1. 添加配置文件 application.yml

server:
  port: 8761
spring:
  application:
    name: eureka-server
eureka:
  instance:
    hostname: localhost
    lease-renewal-interval-in-seconds: 30
    lease-expiration-duration-in-seconds: 90
  server:
    eviction-interval-timer-in-ms: 10000
  client:
    register-with-eureka: true
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

五、 搭建阶段-Zuul

  1. 新建子模块:myZuul

  2. 引入Server依赖 和 添加springBoot maven plugin


<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>

<plugin>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
  1. 新建包名:com.elead.it

  2. 添加启动类 ZuulStarter.java


@SpringBootApplication
@EnableZuulProxy
public class ZuulStarter {
   public static void main(String[] args) {
       SpringApplication.run(ZuulStarter.class, args);
  }
}
  1. 添加配置文件 application.yml

server:
  port: 8060
  servlet:
    context-path: /zuul
spring:
  application:
    name: zuul
eureka:
  client:
    enabled: true
    serviceUrl:
      defaultZone: http://127.0.0.1:8761/eureka/
zuul:
  routes:
    springboot:
      path: api/**
      serviceId: mySpringBoot

六、 搭建阶段-mySpringBoot

  1. 新建子模块:mySpringBoot

  2. 引入spring web依赖 和 添加springBoot maven plugin


<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

<dependency>
   <groupId>io.springfox</groupId>
   <artifactId>springfox-swagger2</artifactId>
</dependency>

<dependency>
   <groupId>io.springfox</groupId>
   <artifactId>springfox-swagger-ui</artifactId>
</dependency>

<plugin>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
  1. 新建包名:com.elead.it

  2. 添加启动类 MySpringBootStarter.java

/**
* 内置tomcat启动类
*/
@SpringBootApplication
public class MySpringBootStarter {
   public static void main(String[] args) {
       SpringApplication.run(MySpringBootStarter.class);
  }
}
  1. 新建包名:com.elead.it.po

  2. 添加po类UserInfo.java

package com.elead.it.po;

import java.io.Serializable;
import java.util.Date;

/**
* 想省去set get 的同学请用lombok
*/
public class UserInfo implements Serializable {
   private String uid;
   private String name;
   private String email;
   private int age;
   private Date createDate;
   private Date modifyDate;

   public String getUid() {
       return uid;
  }

   public void setUid(String uid) {
       this.uid = uid;
  }

   public String getName() {
       return name;
  }

   public void setName(String name) {
       this.name = name;
  }

   public String getEmail() {
       return email;
  }

   public void setEmail(String email) {
       this.email = email;
  }

   public int getAge() {
       return age;
  }

   public void setAge(int age) {
       this.age = age;
  }

   public Date getCreateDate() {
       return createDate;
  }

   public void setCreateDate(Date createDate) {
       this.createDate = createDate;
  }

   public Date getModifyDate() {
       return modifyDate;
  }

   public void setModifyDate(Date modifyDate) {
       this.modifyDate = modifyDate;
  }
}
  1. 新建包名:com.elead.it.controller

  2. 添加类:UserInfoController.java


@RestController
@RequestMapping("user")
public class UserInfoController {

   @Value("${instance.id}")
   private String instanceId;

   private static UserInfo userInfo;

   static {
       Date now = new Date();
       userInfo = new UserInfo();
       userInfo.setAge(18);
       userInfo.setEmail("zhangSan@e-lead.com");
       userInfo.setUid("001");
       userInfo.setName("张三");
       userInfo.setCreateDate(now);
       userInfo.setModifyDate(now);
  }

   @RequestMapping(method = RequestMethod.GET)
   public Map<String, Object> getUser() {
       Map<String, Object> result = new HashMap<String, Object>();
       result.put("instanceId", instanceId);
       result.put("data", userInfo);
       return result;
  }
}
  1. 新建包名:com.elead.it.config

  2. 添加类:MySpringBootConfig.java


@Configuration
@EnableSwagger2
public class MySpringBootConfig {
   @Bean
   public Docket restfulApi() {
       return new Docket(DocumentationType.SWAGGER_2)
              .apiInfo(apiInfo())
              .select()
              .apis(RequestHandlerSelectors.any())
              .paths(PathSelectors.any())
              .build();
  }

   private ApiInfo apiInfo() {
       return new ApiInfoBuilder()
              .title("MySpringBoot")
              .description("MySpringBoot")
              .version("1.0.0")
              .build();
  }
}
  1. 添加配置文件 application.yml

server:
port: 8881
spring:
application:
  name: mySpringBoot
eureka:
client:
  enabled: true
  service-url:
    defaultZone: http://localhost:8761/eureka/
instance:
id: 2

七、附录一 swagger注解

@Api: 修饰整个类,用于controller类上

@ApiOperation: 描述一个接口,用户controller方法上

@ApiParam: 单个参数描述

@ApiModel: 用来对象接收参数,即返回对象

@ApiModelProperty: 对象接收参数时,描述对象的字段

@ApiResponse: Http响应其中的描述,在ApiResonse中

@ApiResponses: Http响应所有的描述,用在

@ApiIgnore: 忽略这个API

@ApiError: 发生错误的返回信息

@ApiImplicitParam: 一个请求参数

@ApiImplicitParam: 多个请求参数

八、附录二 PO,VO,DAO,BO,POJO之间的区别

VO
value object:值对象
通常用于业务层之间的数据传递,由new创建,由GC回收。

PO
persistant object:持久层对象
对应数据库中表的字段。
VO和PO,都是属性加上属性的get和set方法;表面看没什么不同,但代表的含义是完全不同的。

DTO
data transfer object:数据传输对象。
表里面有十几个字段:id,name,gender(M/F),age,conmpanyId(如001)...
页面需要展示四个字段:name,gender(男/女),age,conmpanyName(如今日头条股份有限公司)。
DTO由此产生,一是能提高数据传输的速度(减少了传输字段),二能隐藏后端表结构。

BO
business object:业务对象
BO把业务逻辑封装为一个对象。
PO的组合

POJO
plain ordinary java object:简单无规则java对象
纯的传统意义的java对象,最基本的Java Bean只有属性加上属性的get和set方法。
可以转化为PO、DTO、VO;比如POJO在传输过程中就是DTO。

DAO
data access object:数据访问对象
主要用来封装对数据的访问,注意,是对数据的访问,不是对数据库的访问。

九、喜闻乐见环节

  1. 要求连接数MySQL据库,通过Mybatis 查询UserInfo对象

  2. DDL、DML语句存储在mySpringBoot.sql文件中


<dependency>
   <groupId>org.mybatis.spring.boot</groupId>
   <artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>

<dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
</dependency>

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
posted on 2021-01-18 10:56  白糖℃  阅读(167)  评论(0编辑  收藏  举报