Spring Boot笔记

SpringBoot项目快速启动:

        对SpringBoot项目打包(执行Maven构建指令package)

        执行启动指令

java –jar springboot.jar

                注:jar支持命令行启动需要依赖maven插件支持

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

三种配置格式:

        application.properties:

server.port=80

        application.yml:

server:
  port: 80

        application.yaml:

server:
  port: 80

        SpringBoot配置文件加载顺序:

                application.properties  >  application.yml  >  application.yaml

        自动提示功能消失解决方案:

yaml数据格式:

        优点:

                容易阅读

                容易与脚本语言交互

                以数据为核心,重数据轻格式

        YAML文件扩展名:

                .yml(主流)

                .yaml

        语法规则:

                大小写敏感

                属性层级关系使用多行描述,每行结尾使用冒号结束

                使用缩进表示层级关系,同层级左侧对齐,只允许使用空格(不允许使用Tab键)

                属性值前面添加空格(属性名与属性值之间使用冒号+空格作为分隔)

                数组数据在数据书写位置的下方使用减号作为数据开始符号,每行书写一个数据,减号与数据间空格分隔

                # 表示注释

yaml数据读取:

lesson: SpringBoot

server:
  port: 80

enterprise:
  name: zqy
  age: 18
  tel: 4006184000
  subject:
    - aaa
    - bbb
    - ccc

        1. @Value读取单个数据,属性名引用方式:${一级属性名.二级属性名……}:

        2. 封装全部数据到Environment对象:

@RestController
@RequestMapping("/books")
public class BookController {
    @Autowired
    private Environment env;
    @GetMapping("/{id}")
    public String getById(@PathVariable Integer id){
        System.out.println(env.getProperty("lesson"));
        System.out.println(env.getProperty("enterprise.name"));
        System.out.println(env.getProperty("enterprise.subject[0]"));
        return "hello , spring boot!";
    }
}

        3. 自定义对象封装指定数据:

@Component
@ConfigurationProperties(prefix = "enterprise")
public class Enterprise {
    private String name;
    private Integer age;
    private String[] subject;
}
@RestController
@RequestMapping("/books")
public class BookController {
    @Autowired
    private Enterprise enterprise;
}

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

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

多环境启动:

        yaml方式:

spring:
  profiles:            //启动指定环境
    active: pro
---
spring:
  profiles: pro        //设置生产环境

server:
  port: 80             //生产环境具体参数设定
---
spring:
  profiles: dev

server:
  port: 81
---
spring:
  profiles: test
server:
  port: 82

                过时格式:

spring:
  profiles: pro

                 推荐格式:

spring:
  config:
    activate:
      on-profile: pro

         properties文件方式:

                主启动配置文件application.properties:

spring.profiles.active=pro

                环境分类配置文件application-pro.properties:

server.port=80

        多环境启动命令格式:

//用测试环境启动
java –jar springboot.jar –-spring.profiles.active=test
//修改端口为88启动
java –jar springboot.jar –-server.port=88
//用测试环境启动,并且修改端口为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>

        SpringBoot中引用Maven属性:

spring:
  profiles:
    active: ${profile.active}
---
spring:
  profiles: pro
server:
  port: 80
---
spring:
  profiles: dev
server:
  port: 81
---
spring:
  profiles: test
server:
  port: 82

        对资源文件开启对默认占位符的解析:

<plugin>            
    <artifactId>maven-resources-plugin</artifactId>
    <configuration>                
        <encoding>utf-8</encoding>
        <useDefaultDelimiters>true</useDefaultDelimiters>
    </configuration>        
</plugin>

        执行Maven打包指令

配置文件分类:

        SpringBoot中4级配置文件:

                1级: file :config/application.yml   ——最高级

                2级: file :application.yml

                3级:classpath:config/application.yml

                4级:classpath:application.yml   ——最低级

        作用:

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

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

整合JUnit:

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

整合MyBatis:

        选择当前模块需要使用的技术集(MyBatis、MySQL):

        设置数据源参数:

spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/xxx
    username: root
    password: root

         定义数据层接口与映射配置:

@Mapper
public interface UserDao {
    @Select("select * from xxx")
    public Book getById();
}

        测试类中注入dao接口,测试功能:

@SpringBootTest
class Springboot08MybatisApplicationTests {
    @Autowired
    private BookDao bookDao;
    @Test
    public void testGetById() {
        Book book = bookDao.getById(1);
        System.out.println(book);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值