SpringBoot integration Mybatis-plus configuration

1.Mybatis-plus Introduction

Plus-MyBatis (abbreviated MP) is a  MyBatis  enhancement tools, enhanced not only change on the basis of MyBatis, to simplify development, increase efficiency and health.

MyBatis Plus core functions: support for common CRUD, code generators and conditions of the constructor.

  * General the CRUD: the definition of a good Mapper interfaces, need only to extend BaseMapper <T> interfaces to a common, CRUD function, without writing any interface method with the profile
  * constructor conditions: by EntityWrapper <T> (Entity packaging ), can be used for stitching SQL statements, and supports sorting, grouping, query and other complex SQL
  * code generator: supports a range of policy configuration and global configuration, better than the code generated by MyBatis

2. environment to build

1) configuration dependent Maven

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope > the Test </ scope > 
        </ dependency > 
        <-! mybatisPlus core library -> 
        < dependency > 
            < groupId > com.baomidou </ groupId > 
            < artifactId > the mybatis-PLUS-the Boot-Starter </ artifactId > 
            < Version > 3.1.0 </ Version > 
        </ dependency > 
        <-! introduction Ali database connection pool -> 
        < dependency > 
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.6</version>
        </dependency>
</dependencies>

 

2) configuration file yml

# Configure 
Server: 
  Port: 8081 
the Spring: 
  # configure the data source 
  the DataSource: 
    Driver-class-name: com.mysql.cj.jdbc.Driver 
    url: jdbc: MySQL: // localhost:? 3306 / wwp_user to true useUnicode = & characterEncoding = . 8-UTF 
    username: the root 
    password: the root 
    type: com.alibaba.druid.pool.DruidDataSource 
# PLUS-Related MyBatis 
MyBatis-PLUS: 
  # XML scanning a plurality of catalogs commas or semicolons separated (corresponding to tell Mapper XML file location) 
  Mapper-locations: CLASSPATH: Mapper / * XML. 
  # the following configuration has a default value, may not be provided 
  Global-config: 
    DB-config: 
      # primary key type AUTO: "database ID self-energizing" iNPUT: "a user input ID ", ID_WORKER:" global unique ID (unique numeric ID) ", UUID:" globally unique ID UUID "; 
      ID-type:auto
      # Field strategy IGNORED: "Ignore judgment" NOT_NULL: "Non-NULL judgment") NOT_EMPTY: "non-empty judgment" 
      Field,-at Strategy: not_empty 
      # database type 
      db-of the type: MYSQL 
  the Configuration: 
    # whether to open automatically hump naming map: from the database column name mapping Java properties similar to hump named 
    the map-Underscore-to-CAMEL-Case: to true 
    # If the query results column contains a null value, then MyBatis when mapping does not map this field 
    call-setters-on- NULLS: to true 
    SQL # this configuration will be executed to print out, when developing or testing can 
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

 

3) Configure pagination plug-in

/ ** 
 * @Description MybatisPlus configuration class 
 * @author Sans 
 * @CreateTime 2019/5/26 17:20 
 * / 
@Configuration 
public  class MybatisPlusConfig {
     / ** 
     * PLUS MyBatis-plug efficiency of the SQL execution environment may produce [Close] 
     * / 
    @Bean 
    public PerformanceInterceptor performanceInterceptor () {
         return  new new PerformanceInterceptor (); 
    } 
    / ** 
     * tab widget 
     * / 
    @Bean 
    public PaginationInterceptor paginationInterceptor () {
         return  new new PaginationInterceptor (); 
    } 
}

 

3.mybatis-plus field strategy

When using the method updateById mybatis-plus package to update the data, trying to set a field to a null value, but found no original value is null or updated data, since the time mybatis-plus update determination made null the default is not updated to pass null parameters. (Source code can be seen)

/ ** 
* <P>
* enumeration class policy field
* </ P>
*
* @author Hubin
* @Since 2016-09-09
* /
public enum FieldStrategy {
/ **
* Ignore Analyzing
* /
IGNORED,

/ **
Analyzing non-NULL *
* /
NOT_NULL,

/ **
* determined non-empty
* /
not_empty
}

 

2) Case

Annotated @TableField (strategy = FieldStrategy.IGNORED), ignores null value is determined based on the corresponding fields in the entity, e.g.

@TableField(strategy = FieldStrategy.IGNORED)
private String userName;

 

Guess you like

Origin www.cnblogs.com/blogslee/p/11570841.html