目录
1.3.3 Maven与SpringBoot关联操作(多环境配置)
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);
}
}