SpringBoot使用

1. 简介

SpringBoot设计的目的是简化Spring应用的初始搭建以及开发过程。

2. 入门案例

2.1 引入依赖

<?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>com.lan</groupId>
    <artifactId>springboot_01_quickstart</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <!-- 打包插件,打包成fatjar -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

2.2 启动类

package com.lan;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication // 添加springboot注解
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

3. 配置文件

同目录下的配置文件优先级如下:

application.properties > application.yml > application.yaml

不同目录下的配置文件优先级如下:

classpath:application.yml < classpath:config/application.yml < file:application.yml < file:config/application.yml

3.1 读取配置数据

1. 使用@Value注解即可读取到配置文件中的数据

2. 使用Environment对象

@Autowired
private Environment env;

public void readProperty() {
    env.getProperty("server.port");
}

3. 使用@ConfigurationProperties注解读取指定前缀的属性到当前JavaBean中,配合@PropertySource注解,还可以读取指定的配置文件。

@Component
@ConfigurationProperties(prefix="spring.datasource")
@Data
public class DatasourceBean {
    private String jdbcUrl;
    private String driver;
    private String username;
    private String password;
}

@Autowried
private DatasourceBean datasource;
public void readProperty() {
    datasource.getUsername();
}

注意:@ConfigurationProperties是springboot的注解,@PropertySource是spring的注解

3.2 多环境配置

1. yaml或者yml的实例

# 表示激活dev,激活多个时,用逗号隔开
spring:
  profiles:
    active: dev
---
#开发
spring:
  config:
    activate:
      on-profile: dev
server:
  port: 80
---
#生产
#spring.profiles是过时的配置
spring:
  profiles: pro
server:
  port: 81

2. yaml、yml或properties

application.properties

#设置启用的环境,不设置时,默认只加载application.properties
#表示加载application-pro.properties
spring.profiles.active=pro
# 其它公共配置...
...

application-dev.properties

server.port=8080

application-pro.properties

server.port=80

3. 通过jvm参数指定-Dspring.profiles.active=xxx,表示激活profile=xxx的配置

4. SpringBoot整合Junit

案例

package com.lan;

import com.lan.service.BookService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

// SpringBoot测试注解,不指定时引导类时,测试类所在包必须是测试类所在包及其子包
@SpringBootTest(classes = Application.class)
class BookServiceTest {
    @Autowired
    private BookService bookService;
    @Test
    public void save() {
        bookService.save();
    }
}

5. 整合MyBatis

5.1 引入依赖

添加mybatis的starter依赖

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

5.2 编写DAO

package com.lan.dao;

import com.lan.domain.Book;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

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

mybatis-starter会自动扫描带@Mapper注解的DAO接口,并且将自动将接口的返回结果类型都扫描到别名中。

假如DAO接口没加@Mapper注解,则可以在配置类或者启动类中添加@MapperScan注解指定扫描的DAO的package。

假如有mapper文件需要扫描,可以在application.yml添加以下配置扫描对应的mapper文件。

mybatis:
  mapper-locations: classpath:mapper/*.xml

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值