Online Education Framework (2)--Mybatis-plus

  • MyBatis-Plus (MP for short) is an enhancement tool for MyBatis (opens new window). On the basis of MyBatis, it only enhances and does not change. It is born to simplify development and improve efficiency. (Simply speaking, ordinary CRUD can be done by mybatis-plus for you, but for complex CRUD-design involving multiple tables is better than custom sql)
  • mybatis-plus official website

Features of Mybtis-plus

No intrusion: only enhance and do not change, the introduction of it will not affect the existing project, it is as smooth as silk

Low loss: basic CURD will be automatically injected at startup, performance is basically no loss, direct object-oriented operation, powerful CRUD operation: built-in general-purpose Mapper, general-purpose

Service, only a small amount of configuration can achieve most of the CRUD operations of a single table, and a more powerful conditional builder to meet various usage requirements. Support Lambda

Formal call: Through Lambda expressions, you can easily write various query conditions, no need to worry about writing wrong fields. Support automatic generation of primary keys: support up to 4 primary key strategies (including distributed unique ID generator-Sequence), which can be freely configured , Perfectly solve the primary key problem and support ActiveRecord mode:

Support ActiveRecord form call, entity classes only need to inherit Model class to perform powerful CRUD operations,

Support custom global general operations:

Support global universal method injection (Write once, use anywhere) Built-in code generator: Use code or Maven plug-in to quickly generate Mapper, Model, Service, Controller layer code, support template engine, and more custom configurations, waiting for you to use Built-in paging plugin:

Based on MyBatis physical paging, developers do not need to care about specific operations. After the plug-in is configured, writing paging is equivalent to ordinary List query paging plug-in supports multiple databases:

Support MySQL, MariaDB, Oracle, DB2, H2, HSQL, SQLite, Postgre, SQLServer and other databases

Built-in performance analysis plug-in: It can output Sql statements and their execution time. It is recommended to enable this function during development and testing to quickly detect slow queries. Built-in global interception plug-in: Provides full table delete and update operation intelligent analysis block, and can also customize interception Rules to prevent misoperation

Detailed explanation of the logic deletion of the major functions of mybatis-plus through the code

Tombstone: Tombstone is only a deletion in the sense. Tombstone does not actually delete data from the database. It only changes the tombstone field in the database to 1, and the default non-deleted field is 0. When querying, the data that has been logically deleted will not be queried. The advantage of logical deletion is that the data can be restored by mistakenly deleting data.
Physical deletion: Physical deletion is a real deletion. Data is completely deleted from the database. It is very difficult to restore the data. Therefore, the general table design is more logical deletion.

In fact, it is not difficult to implement logical deletion. You only need to add the @TableLogic annotation to the logical deletion attribute of the entity class.

Insert picture description here

Automatic filling of mybatis-plus

Auto-fill is to specify which fields are automatically added when creating a piece of data. For example, creation time, modification time, etc. When creating a piece of data, the creation time is added to the field by default, and there is no need to set the value of the creation time field.

It is not difficult to realize auto-filling. You need to add a @TableFiled annotation to the attribute that needs to be auto-filled in the entity class, and then customize an auto-filling class to indicate what content is automatically filled.
fill = FieldFill.INSERT This means that it is added by default when it is created.
fill = FieldFill.INSERT_UPDATE This represents the default addition of modified data

Step 1: Add annotations to the entity class

Insert picture description here

Step 2: Customize an auto-filled class

Insert picture description here

code generator for mybatis-plus

The so-called code generator is the code to help you generate the architecture, no need to write Controller, service, mapper, entity, etc., these are automatically generated by the code generator, you only need to add things to it.

The code of the code generator is fixed, and only a few parts need to be changed.

public class CodeGenerator {
    public void main1() {

        // 1、创建代码生成器
        AutoGenerator mpg = new AutoGenerator();

        // 2、全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        gc.setOutputDir("D:\\chengxuruanjian\\guli\\service\\service_user" + "/src/main/java");
        gc.setOpen(false); //生成后是否打开资源管理器
        gc.setFileOverride(false); //重新生成时文件是否覆盖
         * mp生成service层代码,默认接口名称第一个字母有 I
         * UcenterService
         * */
        gc.setServiceName("%sService");	//去掉Service接口的首字母I
        gc.setIdType(IdType.ID_WORKER_STR); //主键策略


        // 3、数据源配置
        DataSourceConfig dsc = new DataSourceConfig();

        // 4、包配置
        PackageConfig pc = new PackageConfig();
        pc.setModuleName("user"); //模块名

        // 5、策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setInclude("ucenter_member");    //这个是表名,如果有多张表就填多少个 strategy.setInclude("edu_teacher","","");
        strategy.setTablePrefix(pc.getModuleName() + "_"); //生成实体时去掉表前缀

        strategy.setEntityLombokModel(true); // lombok 模型 @Accessors(chain = true) setter链式操作

        strategy.setRestControllerStyle(true); //restful api风格控制器
        strategy.setControllerMappingHyphenStyle(true); //url中驼峰转连字符


        // 6、执行

General structure:

Insert picture description here

What needs to be changed:

Insert picture description here

Insert picture description here

Insert picture description here

Insert picture description here

At this point, the explanation of mysql-plus is basically over. In fact, mysql-plus has other functions, here is just the functions used in the project.