Some functional defects and solutions of mybatisplus

Add, delete, modify, and check based on multiple fields combined primary key . Native mybatisplus only supports one primary key. Mpp supports multiple field combined primary key addition, deletion, modification, and query. The mapper needs to inherit MppBaseMapper<br>The
combined primary key field in the entity class needs to be modified with @MppMultiId annotations <br>
If you need to use multiple primary key related operations in the service, you can directly inherit the IMppService interface<br>

Optimize the paging plug-in to realize the sorting operation when not paging.
Native mybatisplus paging and sorting are bound. Mpp optimizes the paging plug-in. Use the MppPaginationInterceptor plug-in. <br>
Supports sorting operations without paging. <br> The
page parameter size is set to -1 can achieve full data access without paging, and set OrderItem to achieve sorting<br>

Automatic filling optimization function & automatic scanning Entity class to build ResultMap function
Native mybatisplus can only do two fillings of %s+1 and now, and mybatisplus-plus performs custom complex SQL filling for specified fields when inserting or updating. <br>You
need to use native annotation @TableField to set fill=FieldFill.INSERT fill=FieldFill.UPDATE or fill=FieldFill.INSERT_UPDATE on entity class fields, otherwise it will not trigger custom fill<br>
br/>mybatisplus-plus uses @InsertFill When the annotation triggers the insertion, execute the custom SQL in the annotation to fill the entity class field<br>
mybatisplus-plus uses the @UpdateFill annotation to trigger the update, execute the custom SQL in the annotation to fill the entity class field<br>
br/>It can also be automatically fill the primary key field, does not solve the problem of native mybatisplus multiple primary key support <br>
use ColNameUtil.pn static method to get the entity class reading method corresponding column name <br>
<br>
write resultmap in xml is a headache In particular, the objects returned when the table is connected are diverse. If you do not return according to the map, the workload of creating a resultmap will be doubled. <br>
Using @AutoMap to annotate the entity class, you can parse the field annotated with @TableField when the application starts, and automatically generate a resultMap with scan.mybatis-plus_xxxx as the id<br>
You can directly configure and use this resultMap instance in the xml< br>

The returned entity objects formed by the connection of various tables can be generated through inheritance. After scanning, various resultmaps are automatically constructed and referenced in xml. <br>
<br>
do not even table query, the input parameters are often not a single entity class, instead of using the more flexible the Map object, <br>
but the definition of key parameters of map name is too casual, you can use the interface to define constants. However, native mybatis needs to fill in the complete package name when calling static class methods and variables in xml, which is not conducive to mass adoption.<br>Is
it possible to use lambda expressions to translate the column names in the entity like in mybatisplus? <br> Mpp
does package support The default package name is introduced in ognl of xml, and lambda is supported to define column names.
For example, xml uses the following statement to introduce the
native method of create_time in the map parameter. <br>

#{create_time}

The default package name of mpp refers to the interface constant mode<br>
mpp.utilBasePath in the configuration file can set the default package name of ognl<br>

#{${@ColInfo@createTime}}

mpp's lambda way<br>

#{${@MPP@col("TestEntity::getCreateTime")}}

Import jar from the central library

    <dependency>
        <groupId>com.github.jeffreyning</groupId>
        <artifactId>mybatisplus-plus</artifactId>
        <version>1.3.1-RELEASE</version>
    </dependency>

Set @InsertFill on the entity class field, and automatically fill in the complex calculation value of the seqno field when inserting.
Query the current largest seqno value and add 3 to convert it into a 10-digit string. If there are not enough digits, fill it with 0

    @TableField(value="seqno",fill=FieldFill.INSERT )
    @InsertFill("select lpad(max(seqno)+3,10,'0') from test")
    private String seqno;

Set @KeySequence on the entity class, and automatically fill in complex calculated values ​​for the id field when inserting

@KeySequence("select lpad(max(seqno)+3,10,'0') from test")
@TableName(value = "test")
public class TestEntity {
    @TableId(value = "id", type=IdType.INPUT)
    private Integer id;

Set @InsertFill @UpdateFill on the entity class field, use the current time to fill when inserting and updating

    @InsertFill("select now()")
    @UpdateFill("select now()")
    @TableField(value="update_time",fill=FieldFill.INSERT_UPDATE)
    private Date updateTime;

Use @EnableMPP in the startup class to start the extended custom filling function and automatically create the resultmap function .**Use @EnableKeyGen in the startup class to start the primary key custom primary key filling function.**
Note that if you implement IKeyGenerator yourself, it will conflict with @EnableKeyGen

@SpringBootApplication
@EnableMPP
@EnableKeyGen
public class PlusDemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(PlusDemoApplication.class, args);
    }
}

Use @AutoMap annotation on the entity class br/>JoinEntity is a subclass of
TestEntity @TableName(autoResultMap=true)
autoResultMap must be set to true
br/> The parent class can not add @AutoMap, and mybatisplus is responsible for generating when the parent class sets autoResultMap=true resultmap
but the id of resultmap generated by native mybatisplus is mybatis-plus_xxxx without scan.

@AutoMap
@TableName(autoResultMap=true)
public class JoinEntity extends TestEntity{
    @TableField("some2")
    private String some2;

    public String getSome2() {
        return some2;
    }

    public void setSome2(String some2) {
        this.some2 = some2;
    }

    @Override
    public String toString() {
        return "JoinEntity{" +
                "some2='" + some2 + '\'' +
                '}';
    }
}

Add the scan entity path to the configuration file, multiple paths are separated by commas

mpp:
  entityBasePath: com.github.jeffreyning.mybatisplus.demo.entity

Add the default path of class loading for ognl to execute java static methods in the configuration file, multiple paths are separated by commas

mpp:
  utilBasePath: com.github.jeffreyning.mybatisplus.demo.common

The automatically generated resultMap is introduced in the xml file & the static method is called in xml with the omitted package name & @MPP@col translates the column name through the entity's lambda expression

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.github.jeffreyning.mybatisplus.demo.mapper.TestMapper">
    <select id="queryUseRM" resultMap="scan.mybatis-plus_JoinEntity">
        select * from test inner join test2 on test.id=test2.refid
    </select>
    <select id="queryUse" resultMap="scan.mybatis-plus_JoinEntity">
        select * from test inner join test2 on test.id=test2.refid
        where test.create_time <![CDATA[ <= ]]> #{${@MPP@col("TestEntity::getCreateTime")}}
        and test.id=#{${@MPP@col("TestEntity::getId")}}
        and update_time <![CDATA[ <= ]]> #{${@ColInfo@updateTime}}
    </select>
</mapper>

The interface directly returns the instance class

@Mapper
public interface TestMapper extends BaseMapper<TestEntity> {
    public List<JoinEntity> queryUseRM();
    public List<JoinEntity> queryUse(Map param);
}

Use the ColNameUtil.pn static method to obtain the column name corresponding to the read method in the entity class

       System.out.println(ColNameUtil.pn(TestEntity::getCreateTime));
       System.out.println(ColNameUtil.pn(TestEntity::getId));
       System.out.println(ColNameUtil.pn(JoinEntity::getSome2));
       System.out.println(ColNameUtil.pn(JoinEntity::getUpdateTime));

Add, delete, modify and check the combined primary key of multiple fields
. Use @MppMultiId on the instance class member variable to indicate the combined primary key

@TableName("test07")
public class Test07Entity {
    @MppMultiId
    @TableField(value = "k1")
    private Integer k1;

    @MppMultiId
    @TableField(value = "k2")
    private String k2;

    @TableField(value = "col1")
    private String col1;
    @TableField(value = "col2")
    private String col2;    

Mapper needs to inherit MppBaseMapper

@Mapper
public interface Test07Mapper extends MppBaseMapper<Test07Entity> {
}

Add, delete, modify and check based on multiple primary keys

    public void testMultiId(){
        //id
        Test07Entity idEntity=new Test07Entity();
        idEntity.setK1(1);
        idEntity.setK2("111");
        //del
        test07Mapper.deleteByMultiId(idEntity);
        //add
        test07Mapper.insert(idEntity);
        //query
        Test07Entity retEntity=test07Mapper.selectByMultiId(idEntity);
        retEntity.setCol1("xxxx");
        //update
        test07Mapper.updateByMultiId(retEntity);
    }

The service layer inherits IMppService

public interface Test07Service extends IMppService<Test07Entity> {
}
@Service
public class Test07ServiceImpl extends ServiceImpl<Test07Mapper, Test07Entity> implements Test07Service {
}

Invoke multiple primary key operations at the service layer

    public void testMultiIdService(){
        //id
        Test07Entity idEntity=new Test07Entity();
        idEntity.setK1(1);
        idEntity.setK2("111");
        //del

        test07Service.deleteByMultiId(idEntity);
        //add
        test07Service.save(idEntity);
        //query
        Test07Entity retEntity=test07Service.selectByMultiId(idEntity);
        retEntity.setCol1("xxxx");
        //update
        test07Mapper.updateByMultiId(retEntity);
    }

Optimize the paging plug-in to implement sorting operations when not paging.
Use the MppPaginationInterceptor plug-in

    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new MppPaginationInterceptor();
    }

The mapper is defined in accordance with the general paging interface, and supports the return value of list or page object writing

@Mapper
public interface TestMapper extends BaseMapper<TestEntity> {
    public List<JoinEntity> queryUseRM(Page page);
}

Page parameter setting size=-1 is full query, when size>0, paging is normal, setting OrderItem to perform sorting regardless of whether paging

    public void testOrder(){
        Page page=new Page();

        page.setSize(-1);
        page.addOrder(new OrderItem().setColumn("test.id").setAsc(true));
        page.addOrder(new OrderItem().setColumn("test2.some2").setAsc(true));

        List rp=testMapper.queryUseRM(page);
    }

Demo download
mybatisplus-plus 1.3.0 sample project download
link:
https://pan.baidu.com/s/1wyMBHS4ke_oLEEYOld6-jQ

Scan the subscription official account, reply "plus" to get the download password
Image text

Guess you like

Origin blog.51cto.com/13442277/2581450