05 SpringBoot

lix_uan
• 阅读 497

入门案例

  • 开发控制器类

    @RestController
    @RequestMapping("/books")
    public class BookController {
        @GetMapping("/{id}")
        public String getById(@PathVariable Integer id) {
            System.out.println("id ==> " + id);
            return "hello , spring boot! ";
        }
    }
  • 最简SpringBoot程序所包含的基础文件

    <?xml version="1.0" encoding="UTF-8"?>
    <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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.5.0</version>
        </parent>
        <groupId>cn.lixuan</groupId>
        <artifactId>springboot_01</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
        </dependencies>
    </project>
    // 启动的时候不需要连接数据库
    // @SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})
    @SpringBootApplication
    public class Application {
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }
  • SpringBoot项目快速启动

    <!-- jar支持命令行启动需要依赖maven插件支持 -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    java -jar springboot_01.jar    # 项目的名称根据实际情况修改

基础配置

  • SpringBoot配置文件加载顺序

    // application.properties > application.yml > application.yaml

yaml数据读取

lesson: SpringBoot

server:
  port: 80

enterprise:
  name: itcast
  age: 16
  tel: 4006184000
  subject:
    - Java
    - 前端
    - 大数据
@RestController
@RequestMapping("/books")
public class BookController {
    //使用@Value读取单一属性数据
    @Value("${lesson}")
    private String lesson;
    @Value("${server.port}")
    private Integer port;
    @Value("${enterprise.subject[0]}")
    private String subject_00;

    @Autowired
    private Enterprise enterprise;

    @GetMapping("/{id}")
    public String getById(@PathVariable Integer id){
        System.out.println(lesson);
        System.out.println(port);
        System.out.println(subject_00);

        System.out.println("---------------------");
        System.out.println(enterprise);
        return "hello , spring boot!";
    }
}
@Component
@ConfigurationProperties(prefix = "enterprise")
@Data
public class Enterprise {
    private String name;
    private Integer age;
    private String tel;
    private String[] subject;
}

自定义对象封装数据警告解决方案

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>

yml多环境启动

#设置启用的环境
spring:
  profiles:
    active: dev

---
#开发
spring:
  config:
    activate:
      on-profile: dev
server:
  port: 80
---
#生产
spring:
  profiles: pro
server:
  port: 81
---
#测试
spring:
  profiles: test
server:
  port: 82
---

带参数启动SpringBoot

java –jar springboot.jar --spring.profiles.active=test
java –jar springboot.jar --server.port=88
java –jar springboot.jar --server.port=88 --spring.profiles.active=test

Maven与SpringBoot多环境兼容

  • Maven中设置多环境属性

    <profiles>
        <profile>
            <id>dev_env</id>
            <properties>
                <profile.active>dev</profile.active>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>pro_env</id>
            <properties>
                <profile.active>pro</profile.active>
            </properties>
        </profile>
        <profile>
            <id>test_env</id>
            <properties>
                <profile.active>test</profile.active>
            </properties>
        </profile>
    </profiles>
  • 对资源文件开启对默认占位符的解析

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <configuration>
                    <encoding>utf-8</encoding>
                    <useDefaultDelimiters>true</useDefaultDelimiters>
                </configuration>
            </plugin>
        </plugins>
    </build>
  • SpringBoot中引用Maven属性

    #设置启用的环境
    spring:
      profiles:
        active: ${profile.active}
    
    ---
    #开发
    spring:
      profiles: dev
    server:
      port: 80
    ---
    #生产
    spring:
      profiles: pro
    server:
      port: 81
    ---
    #测试
    spring:
      profiles: test
    server:
      port: 82
    ---
    

SpringBoot中4级配置文件

  • 1级: file :config/application.yml 【最高】

  • 2级: file :application.yml

  • 3级:classpath:config/application.yml

  • 4级:classpath:application.yml 【最低】

  • 1级与2级留做系统打包后设置通用属性

  • 3级与4级用于系统开发阶段设置通用属性

整合第三方技术

SpringBoot整合JUnit

  • 添加整合junit起步依赖(可以直接勾选)

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
  • 编写测试类,默认自动生成了一个

    @SpringBootTest
    class Springboot07JunitApplicationTests {
        @Autowired
        private BookService bookService;
    
        @Test
        public void testSave() {
            bookService.save();
        }
    }

SpringBoot整合MyBatis

  • 创建SpringBoot工程,添加druid依赖

    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>1.2.6</version>
    </dependency>
  • 设置数据源参数

    spring:
      datasource:
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://localhost:3306/ssm_db?serverTimezone=UTC
        username: root
        password: root
        type: com.alibaba.druid.pool.DruidDataSource
  • 定义数据层接口与映射配置

    @Mapper
    public interface UserDao {
        @Select("select * from tbl_book where id=#{id}")
        Book getById(Integer id);
    }
  • 测试类中注入dao接口,测试功能

    @SpringBootTest
    class Springboot08MybatisApplicationTests {
        @Autowired
        private BookDao bookDao;
    
        @Test
        public void testGetById() {
            Book book = bookDao.getById(1);
            System.out.println(book);
        }
    }
点赞
收藏
评论区
推荐文章

暂无数据

lix_uan
lix_uan
Lv1
学无止境,即刻前行
文章
7
粉丝
7
获赞
0