SpringBoot——基础配置、整合第三方技术

目录

1. 基础配置

1.1 配置文件格式(3种)

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

1.2 yaml

1.2.1数据格式

1.2.2 读取方式(application.yaml)

1.3 多环境启动

1.3.1多环境开发配置

        yml / yaml

        properties​编辑

1.3.2 多环境启动命令格式

1.3.3 Maven与SpringBoot关联操作(多环境配置)

1.4 配置文件分类

2.整合第三方技术

2.1 整合JUnit

2.2 基于SpringBoot实现SSM整合

1. 基础配置

1.1 配置文件格式(3种)

 

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


1.2 yaml

1.2.1数据格式

 

1.2.2 读取方式(application.yaml)

lesson: SpringBoot
server:
  port: 80
enterprise:
  name: itcast
  age: 20
  tel: 123456789
  subject:
    - java
    - javascript
    - php
    - sql

方式一:

方式二:

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

    @Autowired
    private Environment environment;

    @GetMapping("/{id}")
    public String getById(@PathVariable Integer id) {

        System.out.println("--------- 方式2 ----------");
        System.out.println(environment.getProperty("lesson"));
        System.out.println(environment.getProperty("server.port"));
        System.out.println(environment.getProperty("enterprise.subject[0]"));

        return "Hello SpringBoot";
    }
}

方式三:

@Component
@ConfigurationProperties(prefix = "enterprise")
public class Enterprise {
    private String name;
    private int age;
    private String tel;
    private String[] subject;

    @Override
    public String toString() {
        return "Enterprise{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", tel='" + tel + '\'' +
                ", subject=" + Arrays.toString(subject) +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getTel() {
        return tel;
    }

    public void setTel(String tel) {
        this.tel = tel;
    }

    public String[] getSubject() {
        return subject;
    }

    public void setSubject(String[] subject) {
        this.subject = subject;
    }
}




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

    @Autowired
    private Enterprise enterprise;

    @GetMapping("/{id}")
    public String getById(@PathVariable Integer id) {

        System.out.println("--------- 方式3 ----------");
        System.out.println(enterprise);

        return "Hello SpringBoot";
    }
}

1.3 多环境启动

1.3.1多环境开发配置

 yml / yaml

spring:
  profiles:
    active: pro

lesson: SpringBoot
enterprise:
  name: itcast
  age: 20
  tel: 123456789
  subject:
    - java
    - javascript
    - php
    - sql
---
#生产环境
spring:
  profiles: pro
server:
  port: 80
---
#开发环境
spring:
  profiles: dev
server:
  port: 81
---
#测试环境
spring:
  profiles: test
server:
  port: 82

 

properties

1.3.2 多环境启动命令格式

!!!!!!!!!!package前先clean!!!!!!!!

1.3.3 Maven与SpringBoot关联操作(多环境配置)


1.4 配置文件分类

2.整合第三方技术

2.1 整合JUnit


  

2.2 基于SpringBoot实现SSM整合

 

 

		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
			<version>1.1.10</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 BookDao {

    @Insert("insert into tbl_book values(null,#{type},#{name},#{description})")
    public int add(Book book);//返回行计数

    @Update("update tbl_book set type = #{type}, name = #{name}, description = #{description} where id = #{id}")
    public int update(Book book);

    @Delete("delete from tbl_book where id = #{id}")
    public int delete(Integer id);

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

    @Select("select * from tbl_book")
    public List<Book> getAll();
}



@SpringBootTest
class DemoApplicationTests {

	@Autowired
	private BookDao bookDao;

	@Test
	void testGetById() {
		Book book = bookDao.getById(3);
		System.out.println(book);
	}

}

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值