Skip to content

SpringBoot Knowledge Points Summary (Part 1)

Published: at 06:18 AM
Loading...

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

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;
}

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:

idnameagegenderphone
1Xiao Kun19118800000001
2Xiao Xu18118800000002
3Xiao Cai17118800000003
4Xiao Cai16218800000004
5Xiao Ji15118800000005
6Xiao Mei14118800000006

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);
}

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);
    }

}

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"}

Previous Post
SpringBoot Knowledge Points Summary (Part 2)
Next Post
Testing LaTeX Rendering