首页 > 其他分享 >springboot_03

springboot_03

时间:2022-12-07 16:23:27浏览次数:42  
标签:username 03 springboot country age password public String

1.yam文件书写格式

 

1.1字面值的表示方式

 

1.2数组的表示方法

 

2.yml文件的读取

 

 

 代码实际

随便写个测试

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

@Value("${country}")
public String country;
@Value("${a.b}")
public String age;
@Value("${shuzu[0].name1}")
public String name;

@GetMapping
public String getByid(){
System.out.println("country------>"+country);
System.out.println("age------>"+age);
System.out.println("shuzu------>"+name);
return "{'module':'user save'}";
};

}

 yml文件写法

server:
port: 81

a:
b: 18

country: chain

shuzu:
-
name1: zhangsan
age: 18
-
name2: lisi
age: 19


主流的写法与读取数据

yam文件中的数据
datasouce:
driver: com.mysal.jdbc.Driver
url: jdbc:mysql://localhost/springboot db
username: root
password: root123


定义一个类存放数据

@Component
//springboot管理
@ConfigurationProperties("datasouce")
//指定加载数据
public class MyDataSource {
private String driver;
private String url;
private String username;
private String password;

@Override
public String toString() {
return "MyDataSource{" +
"driver='" + driver + '\'' +
", url='" + url + '\'' +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}

public String getDriver() {
return driver;
}

public void setDriver(String driver) {
this.driver = driver;
}

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}
}
 
加载运行一下
 

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

@Value("${country}")
public String country;
@Value("${a.b}")
public String age;
@Value("${shuzu[0].name1}")
public String name;

@Autowired
//自动装配
private MyDataSource myDataSource;


@GetMapping
public String getByid(){
System.out.println("country------>"+country);
System.out.println("age------>"+age);
System.out.println("shuzu------>"+name);
System.out.println("myDataSource------>"+myDataSource);

return "{'module':'user save'}";
};

}
成功

 

 




 

标签:username,03,springboot,country,age,password,public,String
From: https://www.cnblogs.com/jinghong-wang/p/16951318.html

相关文章