This is the seventh day of my participation in the August More text Challenge. For details, see:August is more challenging

⭐ August more text challenge day 7 ⭐, learn MybatisPlus, welcome friends to learn 😁

Code Mantis shrimp is a sand sculpture and interesting young boy, like most friends like listening to music, games, of course, in addition to the interest of writing, Emm… There is still a long time to go. Let’s work hard together 🌈

Welcome friends to pay attention to my public number: JavaCodes, although the name with Java, but the scope of more than Java field oh 😁, look forward to your attention ❤

The original link⭐MybatisPlus learning notes ⭐ (7) To achieve automatic filling function

Before the order

MybatisPlus column

⭐MybatisPlus learning notes ⭐ (1) Environment setup and entry HelloWorld

⭐MybatisPlus study Notes ⭐ (2) CRUD full set of detailed explanation

⭐MybatisPlus learning notes ⭐ (3) To achieve logical deletion, paging

⭐MybatisPlus study Notes ⭐ (4) Conditional constructor Wrapper method detailed explanation

⭐MybatisPlus study notes ⭐ (five) to achieve optimistic locking mechanism

⭐MybatisPlus study Notes ⭐ (6) Code generator detailed explanation

Look at the MybatisPlus module for this link



1, automatic filling function introduction

Some properties in common services need to be configured with some default values. MyBatis-Plus provides a plug-in to do this, namely autofill.

Notes for automatic filling function:

  • The filling principle is to directly set the value of the entity properties!!
  • The annotation specifies that the property must have a value in the corresponding case, and that if there is no value, the entry will be null
  • The default methods provided by MetaObjectHandler have a policy of not overwriting properties if they have values, and not filling properties if the fill value is null
  • The field must declare the TableField annotation, and the attribute fill selects the corresponding policy. This declaration tells MyBtis-plus that the SQL field needs to be reserved for injection
  • The population handler MyMetaObjectHandler needs to declare @Component or @Bean injection in Spring Boot
  • The strictInsertFill or strictUpdateFill methods of the parent class must be used to distinguish the fieldfill. XXX annotation from the field name and field type
  • There is no need to distinguish by anything to use the fillStrategy method of the superclass

2. Realize automatic filling function

1. Annotate @tableField == for the properties that need to be autopopulated

@TableField(fill = FieldFill.INSERT)  // Fill on insert
private String email;

@TableField(fill = FieldFill.INSERT_UPDATE)   // Fill both insert and modify
private String lastName;
Copy the code

2. Implement field filling controller and write custom filling rule ==

Realize the meta object processor interface: com. Baomidou. Mybatisplus. Core. Handlers. MetaObjectHandler

@Component
public class MyHandler implements MetaObjectHandler {

    //insert
    @Override
    public void insertFill(MetaObject metaObject) {
        this.setFieldValByName("lastName"."LastName autofill insert",metaObject);
        this.setFieldValByName("email"."Email autofill insert",metaObject);
    }

    // Update the field to be populated
    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("email"."Email autofill and modify",metaObject); }}Copy the code

=, insert test

@Autowired
EmployeeMapper employeeMapper;


@Test
void contextLoads(a) {
    Employee employee = new Employee();
    employee.setAge(19);
    employee.setGender(0);

    int insert = employeeMapper.insert(employee);
    System.out.println(insert);
}
Copy the code

3. Update tests

@Autowired
EmployeeMapper employeeMapper;


@Test
void contextLoads(a) {
    Employee employee = new Employee();
    employee.setAge(19);

    int update = employeeMapper.update(employee,new QueryWrapper<Employee>().eq("gender".1));
    System.out.println(update);
}
Copy the code





The last

I am aCode pipi shrimpI am a mantis shrimp lover who loves to share knowledge. I will keep updating my blog in the future. I look forward to your attention!!

Creation is not easy, if this blog is helpful to you, I hope you can == a key three! ==, thanks for your support, see you next time ~~~