“This is the seventh day of my participation in the First Challenge 2022.

Record a batch addition problem encountered during a development for the generic Mapper. In a demand, encountered batch new, because do not want to write complex XML statements, remember to have seen the general Mapper provides the relevant interface, so directly use, and then there is a problem, so there is this article

1 introduction of problems

1 Problem Description

Requirement: Service data is added in batches, and the primary key is generated using the UUID.

For new batch, general mapper provides two interfaces. Tk. Mybatis mapper. Additional. Insert. InsertListMapper and tk.mybatis.mapper.common.special.InsertListMapper. The import InsertMapper class, didn’t notice the difference, lead to project was introduced in the latter, i.e. tk.mybatis.mapper.com mon. Special. InsertListMapper class, in the subsequent batch operation, the new database error, the parameter id does not exist.

Try one:

After removing the database primary key constraint, the data is successfully stored, but the primary key ID column data is empty.

Try 2:

When you add a database using a for loop tag statement, the data is fine.

2 Problem Analysis

According to the previous operations, the problem occurs when the interfaces are added in batches and the SQL statement related to the batch is checked, the ID is not inserted. It is found that the two interfaces provided by general Mapper have different specific uses.

1 tk.mybatis.mapper.additional.insert.InsertListMapper

This type does not support the primary key policy. That is, you need to manually add the primary key value before batch insertion.

/** * Universal Mapper interface, special method, batch insert, support batch insert database can be used, such as mysql, H2, etc. **@param<T> cannot be empty *@author liuzh
 * @since3.5.0 * /
@RegisterMapper
public interface InsertListMapper<T> {

    * <p> * Does not support primary key policy. Set primary key value * <p> * before insert@KeySqlAnnotated genId mode * *@param recordList
     * @return* /
    @InsertProvider(type = InsertListProvider.class, method = "dynamicSQL")
    int insertList(List<? extends T> recordList);
}
Copy the code

2 tk.mybatis.mapper.common.special.InsertListMapper

This class supports a primary key policy that requires that the entity class must contain an ID attribute and must have an auto-increment primary key.

/** * Universal Mapper interface, special method, batch insert, support batch insert database can be used, such as mysql, H2, etc. **@param<T> cannot be empty *@author liuzh
 */
@tk.mybatis.mapper.annotation.RegisterMapper
public interface InsertListMapper<T> {

    /** * Bulk insert, support bulk insert database can be used, such as MySQL,H2, etc., in addition, this interface limits entities to contain 'id' attribute and must be increment column **@param recordList
     * @return* /
    @Options(useGeneratedKeys = true)
    @InsertProvider(type = SpecialProvider.class, method = "dynamicSQL")
    int insertList(List<? extends T> recordList);

}
Copy the code

2 Problem Solving

Switch import InsertListMapper class, using the tk. Mybatis mapper. Additional. Insert. InsertListMapper after class, data warehousing success. For the above problems, when using open source tools, you still need to know more about their usage, but also need more practice to verify.

3 General Mapper related notes

1 @Table

The @table annotation is a mapping between an entity class and a database Table. The hump naming function (tb_user => tbUser) is enabled by default. You can also manually map the hump by name.

@Table(name = "t_user")
public class user{}Copy the code

2 @Column

The @column annotation represents a mapping between an entity-class attribute and a database table Column name. Hump naming (is_use => isUse) is enabled by default and can be manually mapped

3 @Id

The @ID annotation represents the mapping between this attribute of the entity class and the database primary key

4 @GeneratedValue

The @GeneratedValue annotation is used in conjunction with the @ID annotation to write the database-automatically generated primary key value back to the entity-class object after performing an insert.

5 @KeySql

The @keysQL annotation is a primary key policy annotation that configures how a primary key is generated.

6 @Transient

@TRANSIENT annotation, Transient, non-persistent. That is, ignore entity class and database mapping.