Java-SpringBoot

SpringBoot

start

https://start.aliyun.com

配置文件

配置文件的优先级:application.properties > application.yml > application.yaml

yaml配置文件数据读取

lesson: SpringBoot

server:
  port: 80

enterprise:
  name: it
  age: 16
  tel: 4006184000
  subject:
    - Java
    - 前端
    - 大数据
  1. 使用@Value注解
@RestController
@RequestMapping("/books")
public class tt {          
    @Value("${lesson}")
    private String lesson;
    @Value("${server.port}")
    private Integer port;
    @Value("${enterprise.subject[0]}")
    private String subject_00;
    
    @GetMapping("/{id}")
    public String getById(@PathVariable Integer id) {
        System.out.println(lesson);
        System.out.println(port);
        System.out.println(subject_00);
        return "hello , spring boot!";
    }
}
  1. Environment对象
import org.springframework.core.env.Environment;

@RestController
@RequestMapping("/books")
public class tt {

    @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!";
    }
}
  1. 自定义对象
@Component
@ConfigurationProperties(prefix = "enterprise")
public class Enterprise {
    private String name;
    private Integer age;
    private String tel;
    private String[] subject;

    // ...
}
@RestController
@RequestMapping("/books")
public class BookController {

    @Autowired
    private Enterprise enterprise;

    @GetMapping("/{id}")
    public String getById(@PathVariable Integer id) {
        System.out.println(enterprise.getName());
        System.out.println(enterprise.getAge());
        System.out.println(enterprise.getSubject());
        System.out.println(enterprise.getTel());
        System.out.println(enterprise.getSubject()[0]);
        return "hello , spring boot!";
    }
}

需要添加依赖:

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

多环境配置

使用yml配置文件

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

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

使用properties配置文件

先设置不同环境下的配置文件

application-dev.properties

server.port=80

application-pro.properties

server.port=81

application-test.properties

server.port=82

在默认的配置文件application.properties

spring.profiles.active=pro

命令行启动

java -jar xxx.jar

通过参数来修改配置:

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

配置文件分类

1级:classpath:application.yml

2级:classpath:config/application.yml

3级:file :application.yml

4级:file :config/application.yml(优先级最高)

jar文件所在目录config文件夹下的配置文件优先于类路径下的配置文件

类路径下的 config 下的配置文件优先于类路径下的配置文件

SpringBoot整合junit

@SpringBootTest
public class BookServiceTest {

    @Autowired
    private BookService bookService;

    @Test
    public void save() {
        bookService.save();
    }
}

引导类所在包必须是测试类所在包及其子包。

例如:
引导类所在包是 fun.it
测试类所在包是 fun.it

如果不满足这个要求的话,就需要在使用 @SpringBootTest 注解时,使用 classes 属性指定引导类的字节码对象。如
@SpringBootTest(classes = SpringbootTestApplication.class)

SpringBoot整合MyBatis

创建模块时添加依赖

image-20220719231251360

<dependency>
     <groupId>com.alibaba</groupId>
     <artifactId>druid</artifactId>
     <version>1.1.16</version>
</dependency>

在配置文件中配置数据源:

spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3307/ssm_db?serverTimezone=UTC
    username: root
    password: root

在需要代理生成Dao对象的接口上使用@Mapper注解

@Mapper
public interface BookDao {
    @Select("select * from tbl_book where id = #{id}")
    public Book getById(Integer id);
}

SpringBoot 版本低于2.4.3(不含),Mysql驱动版本大于8.0时,需要在url连接串中配置时区
jdbc:mysql://localhost:3306/ssm_db?serverTimezone=UTC ,或在MySQL数据库端配置时区解决此问题

posted @ 2022-07-24 16:32  ChingFun  阅读(110)  评论(0编辑  收藏  举报