mybatis-plus 2.x Upgrade 3.x Notes

The main upgrades from 2.0 to 3.0 are:

  • Upgrading JDK 8 + Optimizing Performance Wrapper Support lambda Syntax
  • Modular MP allocates each package structure reasonably
  • Reconstructing injection method to support any method to simplify injection mode
  • Global Configuration Underline Conversion Eliminates Injection of AS Statements
  • Modify Wrapper to Query Wrapper Update Wrapper
  • Reconstructing Paging Plug-in to eliminate fixed paging model and support Mapper to return directly to IPage interface
  • Add Rest Api through Controller layer
  • Entity String type fields default LIKE query SelectOne default LIMIT 1
  • New bean map Interchange Tool Class for Support of SelectectMaps
  • Add db2 support starter to Spring boot 2 + support
  • Refactoring Generator Provides Custom DB Multiple Template Engine Support

Configuration item

SqlSession Factory configuration

2.0

    <!--register SqlSessionFactory-->
    <bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean">
        <!-- Configuring data sources -->
        <property name="dataSource" ref="shardingDataSource"/>
        <property name="globalConfig" ref="globalConfig"/>
        <!-- To configure Mybatis Configuration files (none) -->
        <!--<property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/>-->
        <!-- Automatic scanning Xml file location -->
        <!--<property name="mapperLocations" value="classpath:mapper/with-sharding/*Mapper.xml"  />-->
        <!-- Enumeration property configuration scan to support wildcards * perhaps ; Division -->
        <!-- <property name="typeEnumsPackage" value="com.wlqq.insurance.conf.*.enums"/> -->

        <property name="plugins">
            <array>
                <!-- Paging plug-in configuration -->
                <bean id="paginationInterceptor"
                      class="com.baomidou.mybatisplus.plugins.PaginationInterceptor"/>
                <!-- Optimistic Lock Plug-in -->
                <bean id="optimisticLockerInterceptor"
                      class="com.baomidou.mybatisplus.plugins.OptimisticLockerInterceptor">
                </bean>
                <!-- Performance Interceptor and Printing sql,It is not recommended to allocate production environment.-->
                <bean id="performanceInterceptor"
                      class="com.baomidou.mybatisplus.plugins.PerformanceInterceptor"/>
            </array>
        </property>
    </bean>

3.0

 <!--register SqlSessionFactory-->
    <bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
        <!-- Configuring data sources -->
        <property name="dataSource" ref="shardingDataSource"/>
        <property name="globalConfig" ref="globalConfig"/>
        <!-- To configure Mybatis Configuration files (none) -->
        <!--<property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/>-->
        <!-- Automatic scanning Xml file location -->
        <!--<property name="mapperLocations" value="classpath:mapper/with-sharding/*Mapper.xml"  />-->
        <!-- Enumeration property configuration scan to support wildcards * perhaps ; Division -->
        <!-- <property name="typeEnumsPackage" value="com.wlqq.insurance.conf.*.enums"/> -->

        <property name="plugins">
            <array>
                <!-- Paging plug-in configuration -->
                <bean id="paginationInterceptor"
                      class="com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor"/>
                <!-- Optimistic Lock Plug-in -->
                <bean id="optimisticLockerInterceptor"
                      class="com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor">
                </bean>
                <!-- Performance Interceptor and Printing sql,It is not recommended to allocate production environment.-->
                <bean id="performanceInterceptor"
                      class="com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor"/>
            </array>
        </property>
    </bean>

Global Policy Configuration

2.0

    <!-- Definition MP Global strategy -->
    <bean id="globalConfig" class="com.baomidou.mybatisplus.entity.GlobalConfiguration">
        <!-- Primary Key Policy Configuration -->
        <!-- Optional parameters
            AUTO->`0`("data base ID Self increment")
            INPUT->`1`(User input ID")
            ID_WORKER->`2`("Global uniqueness ID")
            UUID->`3`("Global uniqueness ID")
        -->
        <property name="idType" value="0"/>
        <!-- Database Type Configuration -->
        <property name="dbType" value="mysql"/>
        <!-- Global table naming settings for underscores true -->
        <property name="dbColumnUnderline" value="true"/>
    </bean>

3.0

    <!-- Definition MP Global strategy -->
    <bean id="globalConfig" class="com.baomidou.mybatisplus.core.config.GlobalConfig">
        <property name="dbConfig">
            <bean class="com.baomidou.mybatisplus.core.config.GlobalConfig.DbConfig">
                <!-- Primary Key Policy Configuration -->
                <!-- Optional parameters
                    AUTO->`0`("data base ID Self increment")
                    INPUT->`1`(User input ID")
                    ID_WORKER->`2`("Global uniqueness ID")
                    UUID->`3`("Global uniqueness ID")
                -->
                <property name="idType" value="AUTO"/>
                <!-- Database Type Configuration -->
                <property name="dbType" value="MYSQL"/>
                <!-- Global Table Naming Settings for Underlines true -->
                <property name="tableUnderline" value="true"/>

            </bean>
        </property>
    </bean>

Package structure modification

2.0 3.0
com.baomidou.mybatisplus.activerecord.Model com.baomidou.mybatisplus.extension.activerecord.Model;
com.baomidou.mybatisplus.annotations.TableField; com.baomidou.mybatisplus.annotation.TableField;
com.baomidou.mybatisplus.annotations.TableId; com.baomidou.mybatisplus.annotation.TableId;
com.baomidou.mybatisplus.enums.IdType; com.baomidou.mybatisplus.annotation.IdType;
com.baomidou.mybatisplus.service.impl.ServiceImpl; com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
com.baomidou.mybatisplus.mapper.BaseMapper; com.baomidou.mybatisplus.core.mapper.BaseMapper;
com.baomidou.mybatisplus.plugins.Page; com.baomidou.mybatisplus.extension.plugins.pagination.Page;

Wrapper

Entity Wrapper was deleted and Query Wrapper, Update Wrapper, Lambda Query Wrapper and Lambda Update Wrapper were added. EntityWrapper can be replaced by Query Wrapper.

2.0

EntityWrapper query = new EntityWrapper();
        query.setEntity(RiskPrice.builder().code(code).enabled(true).build());
        return dao.selectList(query);

3.0

QueryWrapper<RiskPrice> queryWrapper = new QueryWrapper<>(RiskPrice.builder().code(code).enabled(true).build());
return dao.selectList(queryWrapper);
//perhaps
QueryWrapper<RiskPrice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("code", code).eq("enabled", true);
return dao.selectList(queryWrapper);

Method

BaseMapper

  • The insert AllColumn (T entity) and update AllColumn (T entity) methods are removed.
  • New update (T entity, Wrapper < T > update Wrapper) method

IService

  • insert and insertXXX methods changed to save and saveXXX methods
  • deleteXXX changed to removeXXX method
  • Remove the XXX AllColumn XXX method

Wrapper

2.0 3.0 describe
setSqlSelect(String sqlSelect) select(R... columns); Set the return column of the query

Wrapper < T > Use

Query Wrapper < T > and Update Wrapper < T > shared methods

Method name Explain
allEq map-based content equals=
eq Equal to =
ne Not equal to >
gt Greater than >
ge Greater than or equal to >=
lt Less than <
le Less than or equal to<=
between BETWEEN conditional statement
notBetween NOT BETWEEN conditional statement
like LIKE'% Value%''
notLike NOT LIKE'% Value%'
likeLeft LIKE '% value'
likeRight LIKE '%%'
isNull NULL Value Query
isNotNull NOT NULL Value Query
in IN query
notIn NOT IN Query
inSql IN Query (sql injection)
notInSql NOT IN Query (sql injection)
groupBy Grouping GROUP BY
orderByAsc ASC Sort ORDER BY
orderByDesc DESC sort ORDER BY
orderBy Sort ORDER BY
having HAVING keywords (sql injection)
or or splicing
apply Splicing custom content (sql injection)
last Stitching at the end (sql injection)
exists EXISTS conditional statement (sql injection)
notExists NOT EXISTS conditional statement (sql injection)
and(Function) AND (nested content)
or(Function) OR (nested content)
nested(Function) (Nested content)

Query Wrapper < T > Specific Method

Method name Explain
select SQL query field content, such as id, name, age (repeated settings are final)

Update Wrapper < T > Specific Method

Method name Explain
set SQL SET field (one field used once)

Paging query

IPage<T> selectPage(IPage<T> page, @Param("ew") Wrapper<T> queryWrapper);

Reference resources: https://blog.csdn.net/m0_37840000/article/details/81604613

Tags: Programming SQL Mybatis xml Lambda

Posted on Sat, 21 Sep 2019 03:06:18 -0700 by alconebay