官网:https://spring.io/projects/spring-boot/
springboot学习网站:http://www.springboot.wiki/
注:学习该文章需要掌握maven,并且最好使用过springmvc

官网学习

1、项目—学习—参考文件

image.png

2、进入启动项目参考文件

image.png

3、配置要求

image.png
image.png

4.1、创建pom文件

image.png
注:这里最主要的是parent,如果不按照上面模板的话,就自己手动把parent给添加上,这步是指定父工程

(建议去官网自行复制)

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.example</groupId>
  6. <artifactId>myproject</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <parent>
  9. <groupId>org.springframework.boot</groupId>
  10. <artifactId>spring-boot-starter-parent</artifactId>
  11. <version>2.2.2.RELEASE</version>
  12. </parent>
  13. <description/>
  14. <developers>
  15. <developer/>
  16. </developers>
  17. <licenses>
  18. <license/>
  19. </licenses>
  20. <scm>
  21. <url/>
  22. </scm>
  23. <url/>
  24. <!-- Additional lines to be added here... -->
  25. </project>

4.2、添加依赖

image.png

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-web</artifactId>
  5. </dependency>
  6. </dependencies>

5、编写启动代码

image.png

  1. import org.springframework.boot.SpringApplication;
  2. import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. import org.springframework.web.bind.annotation.RestController;
  5. @RestController
  6. @EnableAutoConfiguration
  7. public class Example {
  8. @RequestMapping("/")
  9. String home() {
  10. return "Hello World!";
  11. }
  12. public static void main(String[] args) {
  13. SpringApplication.run(Example.class, args);
  14. }
  15. }

@RestController:@Controller和@ResponseBody的合体,说明他是一个controller类,并且响应是Json对象
@EnableAutoConfiguration:自动配置注解,该注解集成了各种热门的依赖(核心配置,springboot和springmvc的差距基本就是依靠这个注解拉开的)
@RequestMapping(“/“):该注解说明这个方法是url的映射方法,通过url访问这个方法
SpringApplication.run(Example.class, args);:这句代码的意思是启动该应用的入口

6、启动项目

spring-boot默认内嵌tomcat容器,所以不需要独立安装
image.png

注:默认启动8080端口,如果端口被占用会启动失败
server.port=9099

这个是我写的包,可以作为参考(实际开发不是这样写的)
image.png


第一个hello world

刚刚是从官网找到的资料并启动了第一个springboot项目,但实际还是稍微有点出入,这时候我们需要进行一些修改

pom.xml配置修改

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>eden</groupId>
  7. <artifactId>study-springboot</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. <!-- 指定jdk版本 -->
  10. <properties>
  11. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  12. <maven.compiler.source>1.8</maven.compiler.source>
  13. <maven.compiler.target>1.8</maven.compiler.target>
  14. </properties>
  15. <!-- 指定父工程 -->
  16. <parent>
  17. <groupId>org.springframework.boot</groupId>
  18. <artifactId>spring-boot-starter-parent</artifactId>
  19. <version>2.1.2.RELEASE</version>
  20. </parent>
  21. <!-- 依赖jar -->
  22. <dependencies>
  23. <dependency>
  24. <groupId>org.springframework.boot</groupId>
  25. <artifactId>spring-boot-starter-web</artifactId>
  26. </dependency>
  27. </dependencies>
  28. <!-- 插件 -->
  29. <build>
  30. <plugins>
  31. <plugin>
  32. <groupId>org.springframework.boot</groupId>
  33. <artifactId>spring-boot-maven-plugin</artifactId>
  34. </plugin>
  35. </plugins>
  36. </build>
  37. </project>

Example类修改成AppStart

  1. package cn.eden;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.web.bind.annotation.RestController;
  5. //@EnableAutoConfiguration
  6. @SpringBootApplication
  7. public class AppStart {
  8. public static void main(String[] args) {
  9. SpringApplication.run(AppStart.class, args);
  10. }
  11. }

这里更换了一个注解,@EnableAutoConfiguration注解更换成了@SpringBootApplication,实际上@SpringBootApplication内嵌了@EnableAutoConfiguration注解,并且在此基础上添加了@ComponentScan注解,@ComponentScan注解是用来扫包的,可以将该类包下以及它的子包中的controller都扫描进入IOC容器当中
并且删除了一个方法,url映射全部转移到controller类中

总结:也就是说更换的注解,新注解可以在原有的注解上添加了一个扫包的功能

编写controller类

  1. package cn.eden.controller;
  2. import org.springframework.web.bind.annotation.RequestMapping;
  3. import org.springframework.web.bind.annotation.RestController;
  4. @RestController
  5. public class helloController {
  6. @RequestMapping("/hello")
  7. public Object hello() {
  8. return "Hello World!";
  9. }
  10. }

到这一步的时候就完成了一个最基础的demo,启动应用就能访问了

下面这个是我的项目结构
image.png


配置

配置文件方式

1、application.properties
2、yml配置

端口

固定端口:server.port=9099
随机端口:server.port=${random.int[1024,9999])}

自定义属性配置

@Value(${“keyName”})
可以获取application.properties中的配置keyName映射的属性


集成mybatis

pom依赖

查看依赖的官网:http://mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/index.html#

pom.xml

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

application.yml

# 连接池配置
spring:
  datasource:
      driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/database?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=false
    username: root
    password: root

# mapping扫描路径
mybatis:
  mapper-locations: classpath:mapping/*.xml

springboot-start-logging