Detailed! Mybatis plus common API full set of tutorials, I don’t believe you do not understand after reading!

Time:2020-9-18

preface

Official website: official document of mybatis plus simplify mybatis!

Create database

The database name is mybatis_ plus

Create table

Create user table

DROP TABLE IF EXISTS user;
CREATE TABLE user
(
ID bigint (20) not null comment 'primary key ID',
Name varchar (30) null default null comment 'name',
Age int (11) null default null comment 'age',
Email varchar (50) null default null comment 'mailbox',
PRIMARY KEY (id)
);
INSERT INTO user (id, name, age, email) VALUES
(1, 'Jone', 18, '[email protected]'),
(2, 'Jack', 20, '[email protected]'),
(3, 'Tom', 28, '[email protected]'),
(4, 'Sandy', 21, '[email protected]'),
(5, 'Billie', 24, '[email protected]');

Note: – in real development, there are four fields: version, deleted and GMT_ Create, GMT_ Modified (modification time)

Initialize project

Use springboot to initialize!

Import dependency

<! -- database driven -- >
<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- lombok -->
<dependency>
  <groupId>org.projectlombok</groupId>
  <artifactId>lombok</artifactId>
</dependency>
<!-- mybatis-plus -->
<! -- mybatis plus is self-developed, not official! >
<dependency>
  <groupId>com.baomidou</groupId>
  <artifactId>mybatis-plus-boot-starter</artifactId>
  <version>3.0.5</version>
</dependency>

Note: try not to import mybatis and mybatis plus at the same time! Avoid unpredictable problems caused by version differences.

Connect to database

establish application.yml

spring:
  profiles:
    active: dev
  datasource:
#Driving different MySQL 5 com.mysql.jdbc .Driver
#         mysql 8   com.mysql.cj . jdbc.Driver . you need to add a time zone. Servertimezone = GMT% 2B8
    url: jdbc:mysql://localhost:3306/mybatis_plus?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: root

Business code

Entity class
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
  private Long id;
  private String name;
  private Integer age;
  private String email;
}

Mapper interface

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.kuang.pojo.User;
import org.springframework.stereotype.Repository;
//Inherit the basic class basemapper above the corresponding mapper
@Repository // represents the persistence layer
public interface UserMapper extends BaseMapper<User> {
  //All CRUD operations have been written
}

Note that we need to scan all interfaces under our mapper package on the main startup class
@MapperScan(“com.kwhua.mapper”)

test

@SpringBootTest
class MybatisPlusApplicationTests {
  //All the parent classes of mapper are inherited
  //We can also write our own extension method!
  @Autowired
  private UserMapper userMapper;
  @Test
  void contextLoads() {
    //The parameter is a wrapper and a condition constructor. Here, we set the condition to null and query all.
    List<User> users = userMapper.selectList(null);
    users.forEach(System.out::println);
 }
}

All data output
Detailed! Mybatis plus common API full set of tutorials, I don't believe you do not understand after reading!

Configuration log

All of our SQL is now invisible, we want to know how it is executed, so we need to configure the output of the log
application.yml File add log configuration

#Configuration log
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

View the log information of SQL execution
Detailed! Mybatis plus common API full set of tutorials, I don't believe you do not understand after reading!

3、 Crud of mybatis plus

1. Insert operation

//Test insertion
    @Test
    public void testInsert(){
        User user = new User();
        user.setName("kwhua_mybatis-plus_insertTest");
        user.setAge(15);
        user.setEmail("[email protected]");

        int result =  userMapper.insert (user); // help us generate ID automatically
        System.out.println (result); // number of rows affected
        System.out.println (user); // when you see the ID, it will be filled in automatically. }

Detailed! Mybatis plus common API full set of tutorials, I don't believe you do not understand after reading!

The ID is automatically filled in when you see it. The default value of the ID inserted into the database is the global unique ID

Primary key generation strategy

1) Auto increment of primary key
1. @ tableid (type) on entity class field= IdType.AUTO )
2. Set the database ID field to auto increment!
Detailed! Mybatis plus common API full set of tutorials, I don't believe you do not understand after reading!

3. Test again (you can see that the ID value is 1 larger than the last insert)
Detailed! Mybatis plus common API full set of tutorials, I don't believe you do not understand after reading!

ID generation strategy source code interpretation

public enum IdType {
  Auto (0), // database ID auto increment
  None (1), // no primary key set
  Input (2), // manual input
  ID_ Worker (3), // default mode, globally unique ID
  UUID (4), // globally unique ID UUID
  ID_ WORKER_ STR(5); //ID_ Walker string representation
}

The above will not be tested one by one.

update operation

@Test
    public void testUpdate(){
        User user = new User();
        //Automatic splicing of dynamic SQL by condition
        user.setId(1302223874217295874L);
        user.setName("kwhua_mybatis-plus_updateTest");
        user.setAge(20);
        //Note: updatebyid, but the parameter is an object!
        int i = userMapper.updateById(user);
        System.out.println(i);
    }

Detailed! Mybatis plus common API full set of tutorials, I don't believe you do not understand after reading!

Auto fill

Creation time, modification time! These two field operations are automated, we do not want to update them manually!
Alibaba Development Manual: all database tables should be configured with GMT_ create、gmt_ modified! And it needs automation!
Method 1: database level (generally not used in work)
1. New field GMT in table_ create, gmt_ modified
Detailed! Mybatis plus common API full set of tutorials, I don't believe you do not understand after reading!

2. Synchronize entity classes

private Date gmtCreate;
private Date gmtModified;

3. Check again
Detailed! Mybatis plus common API full set of tutorials, I don't believe you do not understand after reading!

Method 2: code level
1. Delete the default value of database, update operation!
Detailed! Mybatis plus common API full set of tutorials, I don't believe you do not understand after reading!

2. You need to add annotation to the entity class field

//Field add padding
    @TableField(fill = FieldFill.INSERT)
    private Date gmt_create;

    @TableField(fill = FieldFill.INSERT_UPDATE)
    private Date gmt_modified;

3. Write a processor to handle this annotation!

@Slf4j
@Component // don't forget to add the processor to the IOC container!
public class MyMetaObjectHandler implements MetaObjectHandler {
    //Fill strategy on insert
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("start insert fill.....");
        // setFieldValByName(String fieldName, Object fieldVal, MetaObject metaObject
        this.setFieldValByName("gmt_create",new Date(),metaObject);
        this.setFieldValByName("gmt_modified",new Date(),metaObject);
    }

    //Population policy on update
    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("start update fill.....");
        this.setFieldValByName("gmt_modified",new Date(),metaObject);
    }
}

4. Test inserts and updates and check for time changes.

Optimistic lock

Optimistic lock: so called Siyi, very optimistic, it always thinks that there will be no problem, no matter what you do, don’t lock it! If something goes wrong,
Update the value test again
Pessimistic lock: so it’s called Siyi, very pessimistic, it always thinks that there are always problems, no matter what you do, you will lock it! Do it again!

Optimistic lock implementation mode:

When fetching a record, get the current version
When updating, bring this version with you
When the update is performed, set version = newversion where version = OldVersion
If the version is not correct, the update fails

Optimistic lock: 1. Query first and get version number version = 1
-- A
update user set name = "kwhua", version = version + 1
where id = 2 and version = 1
--B thread completes first. At this time, version = 2, which will cause modification failure of a!
update user set name = "kwhua", version = version + 1
where id = 2 and version = 1

Optimistic lock test
1. Add version field to database!
Detailed! Mybatis plus common API full set of tutorials, I don't believe you do not understand after reading!

2. Entity class with corresponding fields

@Version // optimistic lock version annotation
    private Integer version;

3. Register components

//Scan our mapper folder
@MapperScan("com.kwhua.mapper")
@EnableTransactionManagement
@Configuration // configuration class
public class MyBatisPlusConfig {
    //Register optimistic lock plug-in
    @Bean
    public OptimisticLockerInterceptor optimisticLockerInterceptor() {
        return new OptimisticLockerInterceptor();
    }
   }

4. Testing

//Test optimistic lock successfully!
    @Test
    public void testOptimisticLocker(){
        //1. Query user information
        User user = userMapper.selectById(1L);
        //2. Modify user information
        user.setName("kwhua");
        user.setEmail("[email protected]");
        //3. Execute the update operation
        userMapper.updateById(user);
    }

The version field has changed from 1 to 2

//Failed to test optimistic lock! Multithreading
    @Test
    public void testOptimisticLocker2(){

        //Thread 1
        User user = userMapper.selectById(1L);
        user.setName("kwhua111");
        user.setEmail("[email protected]");

        //Simulate another thread to perform queue jumping
        User user2 = userMapper.selectById(1L);
        user2.setName("kwhua222");
        user2.setEmail("[email protected]");
        userMapper.updateById(user2);

        //Spinlock to multiple attempts to submit!
        userMapper.updateById (user); // if there is no optimistic lock, the value of queue skipping thread will be overridden!
    }

Detailed! Mybatis plus common API full set of tutorials, I don't believe you do not understand after reading!

You can see that thread 1 failed to perform the update

Query operation

//Test query
    @Test
    public void testSelectById(){
        User user = userMapper.selectById(1L);
        System.out.println(user);
    }

    //Test batch query!
    @Test
    public void testSelectByBatchId(){
        List<User> users = userMapper.selectBatchIds(Arrays.asList(1, 2, 3));
        users.forEach(System.out::println);
    }

    //One of the conditional queries uses the map operation
    @Test
    public void testSelectByBatchIds(){
        HashMap<String, Object> map = new HashMap<>();
        //Customize to query
        map.put("name","kwhua");
        map.put("age",15);

        List<User> users = userMapper.selectByMap(map);
        users.forEach(System.out::println);
    }

6.1 paging query
1. Configure interceptor components

//Paging plug-in
@Bean
public PaginationInterceptor paginationInterceptor() {
  return  new PaginationInterceptor();
}

2. Use page object directly!

//Test paging query
@Test
public void testPage(){
  //Parameter 1: current page
  //Parameter 2: page size
  Page<User> page = new Page<>(2,5);
  userMapper.selectPage(page,null);
  page.getRecords().forEach(System.out::println);
  System.out.println(page.getTotal());
}

Physical deletion

//Test deletion
    @Test
    public void testDeleteById(){
        userMapper.deleteById(1L);
    }

    //Batch deletion by ID
    @Test
    public void testDeleteBatchId(){
        userMapper.deleteBatchIds(Arrays.asList(2L,3L));
    }

    //Delete by map
    @Test
    public void testDeleteMap(){
        HashMap<String, Object> map = new HashMap<>();
        map.put("name","kwhua");
        userMapper.deleteByMap(map);
    }

Logical deletion

Physical deletion: remove directly from the database
Logical deletion: in the database is not removed, but through a variable to make it invalid! deleted = 0 => deleted = 1
The administrator can view the deleted records! Prevent data loss, similar to recycle bin!

1. Add a deleted field to the data table
Detailed! Mybatis plus common API full set of tutorials, I don't believe you do not understand after reading!

2. Add attribute to entity class

@Tablelogic // logical deletion
 private Integer deleted;

3. Configuration

//Logical delete component!
    @Bean
    public ISqlInjector sqlInjector() {
        return new LogicSqlInjector();
    }

Profile configuration

  global-config:
    db-config:
      logic-delete-value: 1
      logic-not-delete-value: 0

4. Testing
Test deletion
Detailed! Mybatis plus common API full set of tutorials, I don't believe you do not understand after reading!

The field value was also changed from 0 to 1
Detailed! Mybatis plus common API full set of tutorials, I don't believe you do not understand after reading!

Test query
Detailed! Mybatis plus common API full set of tutorials, I don't believe you do not understand after reading!

Performance analysis plug in

Function: performance analysis interceptor, used to output each SQL statement and its execution time
MP also provides a performance analysis plug-in. If it exceeds this time, it will stop running!
1. Import plug-in

/**
     *SQL execution efficiency plug-in
     */
    @Bean
    @Profile ({"dev", "test"}) // set the dev test environment on to ensure our efficiency
    public PerformanceInterceptor performanceInterceptor() {
        PerformanceInterceptor performanceInterceptor = new PerformanceInterceptor();
        PE rformanceInterceptor.setMaxTime (100); // MS sets the maximum execution time of SQL. If it exceeds this time, it will not be executed
        performanceInterceptor.setFormat(true);
        return performanceInterceptor;
    }

Detailed! Mybatis plus common API full set of tutorials, I don't believe you do not understand after reading!

Conditional constructor (wrapper)

.isNotNull .gt

@Test
    void contextLoads() {
        //Query users whose name is not empty and whose mailbox is not empty, and whose age is greater than or equal to 12
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper
                . isnotnull ("name") // not empty
                .isNotNull("email")
                .ge("age",18);
        userMapper.selectList (wrapper).forEach( System.out :: println); // compare with the map we just learned
    }

Detailed! Mybatis plus common API full set of tutorials, I don't believe you do not understand after reading!

.eq

@Test
    void test2(){
        //Query name kwhua
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("name","kwhua");
        User user =  userMapper.selectOne (wrapper); // use selectone to query one data and list or map to query multiple results
        System.out.println(user);
    }

.between

@Test
    void test3(){
        //Query users aged between 20 and 30
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.between ("age", 20,30); // interval
        Integer count =  userMapper.selectCount (wrapper); // number of query results
        System.out.println(count);
    }

.like

//Fuzzy query
    @Test
    void test4(){
        //Query the data without E in the name and the mailbox begins with T
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        //Left likelift% T, right likereight T%
        wrapper
                .notLike("name","e")
                .likeRight("email","t");

        List<Map<String, Object>> maps = userMapper.selectMaps(wrapper);
        maps.forEach(System.out::println);
    }

.insql

//Fuzzy query
    @Test
    void test5(){

        QueryWrapper<User> wrapper = new QueryWrapper<>();
        //ID is found in the subquery
        wrapper.inSql("id","select id from user where id<3");

        List<Object> objects = userMapper.selectObjs(wrapper);
        objects.forEach(System.out::println);
    }

.orderByAsc

//Test six
    @Test
    void test6(){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        //Sort by ID
        wrapper.orderByAsc("id");

        List<User> users = userMapper.selectList(wrapper);
        users.forEach(System.out::println);
    }

Code auto generator

You can create a Java class in the test folder

//Code auto generator
public class generateCode {
   public static void main(String[] args) {
    //You need to build a code auto generator object
    AutoGenerator mpg = new AutoGenerator();
    //Configuration policy
    //1. Global configuration
    GlobalConfig gc = new GlobalConfig();
    String projectPath = System.getProperty("user.dir");
    gc.setOutputDir(projectPath+"/src/main/java");
    gc.setAuthor ("kwhua"); // author name
    gc.setOpen(false);
    gc.setFileOverride (false); // whether to override
    gc.setIdType(IdType.ID_WORKER);
    gc.setDateType(DateType.ONLY_DATE);
    gc.setSwagger2 (true); // entity attribute swagger2 annotation

    //User defined file name. Note that% s will automatically fill in the table entity attribute!
    gc.setServiceName("%sService"); 
    gc.setControllerName("%sController");
    gc.setServiceName("%sService");
    gc.setServiceImplName("%sServiceImpl");
    gc.setMapperName("%sMapper");
    gc.setXmlName("%sMapper");

    mpg.setGlobalConfig(gc);

    //2. Set data source
    DataSourceConfig dsc = new DataSourceConfig();
    dsc.setUrl("jdbc:mysql://localhost:3306/kwhua_test?
useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8");
    dsc.setDriverName("com.mysql.cj.jdbc.Driver");
    // dsc.setDriverName (" com.mysql.jdbc . driver "); // mysql5.6 and below
    dsc.setUsername("root");
    dsc.setPassword("root");
    dsc.setDbType(DbType.MYSQL);
    mpg.setDataSource(dsc);
    //3. Configuration of packages
    PackageConfig pc = new PackageConfig();
    pc.setParent (" com.kwhua "); // package name
    pc.setModuleName ("model"); // module name
    pc.setEntity("entity");
    pc.setMapper("mapper");
    pc.setService("service");
    pc.setController("controller");
    mpg.setPackageInfo(pc);

    //4. Policy configuration
    StrategyConfig strategy = new StrategyConfig();
     strategy.setInclude ("user", "course"); // set the name of the table to be mapped
    strategy.setNaming(NamingStrategy.underline_to_camel);
    strategy.setColumnNaming(NamingStrategy.underline_to_camel);
    strategy.setEntityLombokModel (true); // automatic Lombok;
    strategy.setLogicDeleteFieldName("deleted");
    //Auto fill configuration
    TableFill gmtCreate = new TableFill("gmt_create", FieldFill.INSERT);
    TableFill gmtModified = new TableFill("gmt_modified",FieldFill.INSERT_UPDATE);
    ArrayList<TableFill> tableFills = new ArrayList<>();
    tableFills.add(gmtCreate);
    tableFills.add(gmtModified);
    strategy.setTableFillList(tableFills);
    //Optimistic lock
    strategy.setVersionFieldName("version");
    //Create the corresponding class name according to your table name. If your table name is not underlined, such as test, you can cancel this step
    strategy.setTablePrefix("t_");
    strategy.setRestControllerStyle (true); // rest request
    //Auto underline, for example localhost:8080/hello_ id_2
    strategy.setControllerMappingHyphenStyle(true); 
    mpg.setStrategy(strategy);
    mpg.execute (); // execute
 }
}

The corresponding code can be generated by executing the main method

last

Thank you for seeing here. If you think the article is helpful to you, please give me a like. I will share Java related technical articles or industry information every day. You are welcome to pay attention to and forward the article!

Recommended Today

Solutions to leetcode problems and knowledge points 1. Sum of two numbers

Title Link 1. Two Sum  difficulty: $/ color {00965e} {easy}$ Knowledge points 1. Hash (hash) function The classic hash function times33 is widely used, and the core algorithm is as follows: hash(i) = hash(i-1) * 33 + str[i] Laruence has an article about:Hash algorithm in PHP 2. Hash collision processing method 2.1 chain address method […]