On MyBatis-Plus learning plug-in extensions

A, Mybatis plug-in mechanism

mybatis through plug-ins (Interceptor) related to the target object (four objects) Dynamic proxy complete change related data, providing more functionality.

Not here in its internal implementation, only introduce the relevant plug-MP offer.

Two, MP Plugin description

2.1, pagination plug-in

MP seems to have provided the relevant paging method selectPage in BaseMapper in, why use pagination plug it?

This is because selectPage paging through RowBounds ibatis, and that is in memory paging , it is not recommended

The use of paging widget is added Limit keywords related query in the back, in order to achieve physical page

applicationContext.xml

<bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="configLocation" value="classpath:mybatis-config.xml" />
        <property name="typeAliasesPackage" value="cn.hjj.mp.entity"></property>
        <property name="globalConfig" ref="globalConfiguration"></property>
        
        <!-- 注入插件 -->
        <property name="plugins">
            <list>
              <!-- 分页插件 -->
              <bean class="com.baomidou.mybatisplus.plugins.PaginationInterceptor"></bean>            
            </list>
        </property>
    </bean>

Test code as follows

    / ** 
     * tested using plug tab 
     * / 
    @Test 
    public  void testPaginate () { 
        Page <the Employee> = Page new new Page <> (. 1,. 3 ); 
        List <the Employee> = employeeMapper.selectPage the emps (Page, null );   / / the FROM tbl_employee the LIMIT 0,3 
        System.out.println (the emps); 
        
        System.out.println ( "Get paging information ========== ============= " ); 
        System.out.println ( " total number of: "+ page.getTotal ()); 
        System.out.println ( " pages: "+ page.getPages ()); 
        System.out.println ( " current page: "+page.getCurrent ()); 
        System.out.println ( "number of pieces per page:" + page.getSize ()); 
        System.out.println ( "Is there Previous:" + page.hasPrevious ()) ; 
        System.out.println ( "if a next page:" + page.hasNext ());
         // encapsulating this page data into the page object 
        page.setRecords (the emps); 
    }

Page tab parameters for encapsulation

2.2, perform analysis plug-ins

SQL execution analysis plug-in, currently only supports MYSQL5.6.3 above

The role is mainly to analyze whether DELETE and UPDATE statements are full-table operation.

Explain the keyword is essentially a mosaic in front of the SQL statement to analyze the SQL statement to be executed

Extra column determination result to determine whether the entire table

applicationContext.xml

 <bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="configLocation" value="classpath:mybatis-config.xml" />
        <property name="typeAliasesPackage" value="cn.hjj.mp.entity"></property>
        <property name="globalConfig" ref="globalConfiguration"> </ Property > 
        
        <-! Injection plug -> 
        < Property name = "plugins" > 
            < List > 
              <-! Perform analysis plug-in, the production environment is not recommended to use -> 
              < bean class = "com.baomidou. mybatisplus.plugins.SqlExplainInterceptor " > 
                      <-! stopProceed discover whether the implementation of a full table delete update statement to stop the execution -> 
                      < Property name =" stopProceed " value =" to true " > </ Property > 
              </ bean >           
            </ List > 
        </property>
    </bean>

Test code as follows

    / ** 
     * Perform analysis plug-in test use 
     * SQL Explain this nature by SQL keyword analysis performed 
     * / 
    @Test (expected = Exception. Class )
     public  void testSqlExplain () { 
        employeeMapper.delete ( null ); // full table deleted 
    }

2.3 Performance Analysis plug-in

The main output for the SQL statement and its execution time, you can set it exceeds the specified time, stop running

Not recommended for production environments

applicationContext.xml

    <bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="configLocation" value="classpath:mybatis-config.xml" />
        <property name="typeAliasesPackage" value="cn.hjj.mp.entity"></property>
        <property name="globalConfig" ref="globalConfiguration"> </ Property > 
        
        <-! Injection widget -> 
        < Property name = "plugins" > 
            < List > 
              <-! Performance Analysis widget -> 
              < the bean class = "com.baomidou.mybatisplus.plugins.PerformanceInterceptor" > 
                      < Property name = "format" value = "to true" > </ Property > 
                      <-! set a maximum execution time exceeds the error, optimization tips, but as successful implementation of SQL -> 
                      <-! <Property name = "maxTime" value = "100000"> </ Property> ->
              </bean>             
            </list>
        </property>
    </bean>

2.4, optimistic locking plug

Optimistic locking plug-in thing to do is:

1, when removing the recording, it will bring the current version of the version value

version comparison value 2, then when will update their version value with database records

      If they are equal, the update operation is performed, and version + 1; if equal, the update fails

In fact, demand is hoped to achieve when updating records, and I hope this record has not been updated to others

Note: Use optimistic locking plug-in is required @Version annotation entity fields, and the database must also have a corresponding map field

@TableName("tbl_employee")
public class Employee extends Model<Employee> {

    private static final long serialVersionUID = 1L;

    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;
    private String lastName;
    private String email;
    private String gender;
    private Integer age;
    @Version
    private Integer version;

applicationContext.xml

    <bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="configLocation" value="classpath:mybatis-config.xml" />
        <property name="typeAliasesPackage" value="cn.hjj.mp.entity"></property>
        <property name="globalConfig" ref="globalConfiguration"></property>
        
        <!-- 注入插件 -->
        <property name="plugins">
            <list>
              <!-- 乐观锁插件 -->
              <bean class="com.baomidou.mybatisplus.plugins.OptimisticLockerInterceptor"></bean>              
            </list>
        </property>
    </bean>

Guess you like

Origin www.cnblogs.com/jayhou/p/9824571.html