YAML Configuration and Retrieval in SpringBoot
In the application.yml
file, custom configurations can be written as follows:
student:
name: Xiao Kun
age: 18
hobbies:
- Singing
- Dancing
- Rap
- Basketball
- Note: Indentation between levels must be aligned, and there must be a space after the colon before writing the value. If it’s an array, each line after a line break is a value, preceded by
-
, and there must also be a space after-
.
Method 1 to retrieve configuration: Use @Value("${property}")
, applied in the pojo
entity class as follows:
@Data
@NoArgsConstructor
@Component
public class Student {
@Value("${student.name}")
private String name;
@Value("${student.age}")
private Integer age;
}
Method 2 to retrieve configuration: Use @ConfigurationProperties(prefix = "prefix")
, applied in the pojo
entity class as follows:
@Data
@NoArgsConstructor
@Component
@ConfigurationProperties(prefix = "student")
public class Student {
private String name;
private Integer age;
private List<String> hobbies;
}
- Note: If using Method 2, the property names in the
pojo
entity class must be exactly the same as the names in theyml
configuration file. - Note:
@Value
only supports simple string parsing and cannot parse complex data structures likeList
.
SpringBoot Integration with MyBatis
The MyBatis starter dependency is as follows, add it to pom.xml
:
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
Also introduce the MySQL driver dependency as follows:
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
</dependency>
Write the data source configuration in the application.yml
configuration file as follows:
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/{database_name}
username: {username}
password: {password}
Suppose we now have a database named mybatis
with a table user
as follows:
id | name | age | gender | phone |
---|---|---|---|---|
1 | Xiao Kun | 19 | 1 | 18800000001 |
2 | Xiao Xu | 18 | 1 | 18800000002 |
3 | Xiao Cai | 17 | 1 | 18800000003 |
4 | Xiao Cai | 16 | 2 | 18800000004 |
5 | Xiao Ji | 15 | 1 | 18800000005 |
6 | Xiao Mei | 14 | 1 | 18800000006 |
Next, we need to create a pojo
entity class User
with the following code:
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private Integer id;
private String name;
private Short age;
private Integer gender;
private String phone;
}
Then create an interface UserMapper
under the mapper
package as follows:
@Mapper
public interface UserMapper {
@Select("select * from user where id = #{id}")
public User selectUserById(Integer id);
}
- Note: Don’t forget to add
@Mapper
.
Then, create an interface UserService
under the service
package as follows:
public interface UserService {
public User selectUserById(Integer id);
}
Next, under the impl
package in the service
package, create an implementation class UserServiceImpl
that implements the UserService
interface and add the @Service
annotation to hand it over to the IOC
container:
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public User selectUserById(Integer id) {
return userMapper.selectUserById(id);
}
}
Finally, create UserController
under the controller
package with the following code:
@RestController
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/selectUserById")
public User selectUserById(Integer id) {
return userService.selectUserById(id);
}
}
- Note: The above operations belong to the MVC architecture.
Then, run the project and enter http://localhost:8080/selectUserById?id=1
in the browser to see the effect. The browser will output:
{"id":1,"name":"Xiao Kun","age":19,"gender":1,"phone":"18800000001"}