[MyatisPlus] MyatisPlus code generator and advanced query LambdaQueryWrapper&QueryWrapper

Advantages of MyatisPlus

1. No intrusion: only enhancement and no change, the introduction of it will not affect the existing project, as smooth as silk

2. Small loss: Basic CURD will be injected automatically upon startup, with basically no loss in performance, direct object-oriented operation, BaseMapper

3. Powerful CRUD operations: built-in general Mapper and general Service, most of the CRUD operations on a single table can be realized with only a small amount of configuration, and there is a powerful condition constructor to meet various usage needs. In the future, simple CRUD operations do not need to be performed by yourself written!

4. Support Lambda form call: through Lambda expressions, it is convenient to write various query conditions, no need to worry about field errors

5. Support automatic primary key generation: support up to 4 primary key strategies (including a distributed unique ID generator - Sequence), which can be freely configured to perfectly solve the primary key problem

6. Support ActiveRecord mode: support ActiveRecord form call, the entity class only needs to inherit the Model class to perform powerful CRUD operations

7. Support custom global general operations: support global general method injection ( Write once, use anywhere )

8. 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 are waiting for you to use (automatically help you generate code)

9. Built-in paging plug-in: Based on MyBatis physical paging, developers don't need to care about specific operations. After configuring the plug-in, writing paging is equivalent to ordinary List query

10. The paging plug-in supports multiple databases: supports MySQL, MariaDB, Oracle, DB2, H2, HSQL, SQLite, Postgre, SQLServer and other databases

11. 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, which can quickly find out slow queries

12. Built-in global blocking plug-in: provide intelligent analysis and blocking of delete and update operations of the whole table, and also customize blocking rules to prevent misoperation
insert image description here

MyatisPlus code generator code and dependencies

  <!--        mybatis-plus-generator-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>3.4.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity-engine-core</artifactId>
            <version>2.1</version>
        </dependency>
@SpringBootTest
public class MyatisPlusAutoGenerator {
    
    

    @Test
    public void run() {
    
    

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

        // 2、全局配置
        GlobalConfig gc = new GlobalConfig();
//        String projectPath = System.getProperty("user.dir");
        gc.setOutputDir("E:\\Java-Project\\manage" + "/src/main/java");
        gc.setAuthor("帅哥");
        gc.setOpen(false); //生成后是否打开资源管理器
        gc.setFileOverride(false); //重新生成时文件是否覆盖
        gc.setServiceName("%sService");    //去掉Service接口的首字母I
        gc.setIdType(IdType.ID_WORKER); //主键策略
        gc.setDateType(DateType.ONLY_DATE);//定义生成的实体类中日期类型
        gc.setSwagger2(true);//开启Swagger2模式
        mpg.setGlobalConfig(gc);

        // 3、数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://localhost:3306/manage?serverTimezone=GMT%2B8");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("123456");
        dsc.setDbType(DbType.MYSQL);
        mpg.setDataSource(dsc);

        // 4、包配置
        PackageConfig pc = new PackageConfig();
        pc.setModuleName("manage"); //模块名
        pc.setParent("com.hp");
        pc.setController("controller");
        pc.setEntity("entity");
        pc.setService("service");
        pc.setMapper("mapper");
        mpg.setPackageInfo(pc);

        // 5、策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setTablePrefix("t_","sys_");
//        strategy.setInclude("article");
//        strategy.setInclude("building");
//        strategy.setInclude("course");
//        strategy.setInclude("student_course");
//        strategy.setInclude("sys_dict");
//        strategy.setInclude("sys_file");
//        strategy.setInclude("sys_menu");
//        strategy.setInclude("sys_role");
//        strategy.setInclude("sys_role_menu");
        strategy.setInclude("sys_user");
//        strategy.setInclude("t_comment");  //去掉表中前缀
        strategy.setNaming(NamingStrategy.underline_to_camel);//数据库表映射到实体的命名策略
        strategy.setTablePrefix(pc.getModuleName() + "_"); //生成实体时去掉表前缀

        strategy.setColumnNaming(NamingStrategy.underline_to_camel);//数据库表字段映射到实体的命名策略
        strategy.setEntityLombokModel(true); // lombok 模型 @Accessors(chain = true) setter链式操作

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

        mpg.setStrategy(strategy);


        // 6、执行
        mpg.execute();
    }

Wrapper

MyBatis-Plus allows users to freely build queries through EntityWrapper (referred to as EW, a query condition constructor encapsulated by MP) or Condition (similar to EW)

Conditions, simple and convenient, without additional burden, can effectively improve development efficiency, it is mainly used to deal with sql splicing, sorting, entity parameter query, etc.

insert image description here

configuration file

spring:
  datasource:
    username: root
    password: hp
    url: jdbc:mysql://localhost:3306/hm?useSSL=false&useUnicode=true&characterEncoding=utf-8
    driver-class-name: com.mysql.cj.jdbc.Driver

mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    <dependencies>
        <!-- mybatis-plus-boot-starter -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.3</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <artifactId>junit</artifactId>
            <groupId>junit</groupId>
            <scope>test</scope>
        </dependency>
    </dependencies>

Guess you like

Origin blog.csdn.net/weixin_42694422/article/details/129282458