Springboot – Integration with MySQL+ Druid + Mybatis – Plus summary

directory

Springboot – Integration with MySQL+ Druid + Mybatis – Plus summary

I. Abstract:

The purpose of this article

Base JAR package dependencies introduced

Configuration summary

Second, the body

1. Purpose:

2. Introduced the jar package

3. Basic configuration

4. Integrate paging plug-ins



I. Abstract:

  1. The purpose of this article

  2. Base JAR package dependencies introduced

  3. Configuration summary

Second, the body

1. Purpose:

Lead a new team to do a project from 0-1, build the architecture with Springboot as the core. In the process of formulating the specification, I found that everyone was in a mess in the process of using it, so I took this opportunity to write a summary of the specification.

2. Introduced the jar package

<! - spring - the boot - web depend on -- -- >
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<! Mysql driver dependencies -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>
<! - mybatis - plus depend on -- -- >
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.4.1 track</version>
</dependency>
<! --druid database connection pool dependency -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <version>1.2.3</version>
</dependency>
Copy the code

Conclusion:

  • Since druid-spring-boot-starter is introduced, druid dependencies are automatically added, so there is no need to introduce additional druid dependencies separately.
  • This is not the only way to simplify POM files, to do the most with the smallest configuration, and to unify the style of the following configuration files, in short, to make a team specification.
  • Can also give code friends a reference, not in the clouds.

3. Basic configuration

Server.port =8888. # basic configuration spring JDBC datasource. Type = com. Alibaba. Druid. Pool. DruidDataSource spring. The datasource. Url = JDBC: mysql://localhost:3306/amsds_fifa? &serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=root123
spring.datasource.driver-class-name= com. Mysql. Cj, JDBC Driver added # connection pool configuration spring. The datasource. The druid. Initial - size =5
spring.datasource.druid.max-active=10
Copy the code

Conclusion:

  • Driver-class uses cj.jdbc. driver. The url must be configured with the time zone parameter ****serverTimezone . Cj is the MySQL driver after mysql6.
  • The introduction of dependencies in the configuration file can be unified in the spring.datasource. Druid.* style, so that the transition to yML format is at the same level.
  • In addition, when integrating other technologies, starter** matching with Springboot is recommended, so that the code style and technology can be unified, not recommended to introduce alone. 支那

4. Integrate paging plug-ins

@Configuration
public class MybatisPlusConfiguration {

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(a) {
        // Declare plug-in interceptors
        MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
        // Declare a paging plug-in
        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
        // Add to plug-in interceptor
        mybatisPlusInterceptor.addInnerInterceptor(paginationInnerInterceptor);
        returnmybatisPlusInterceptor; }}Copy the code

Conclusion:

  • Mybatis – Plus has no Javadoc in the code, and the documentation is not clear about the changes between the new and old versions, which will cause developers to fall into the hole.
  • The old deprecated PaginationInterceptor method can be used directly by documentation, but older versions are deprecated.
  • The principle of pagination still depends on limit, such pagination is still a problem, easy to lose data