springBoot(四)增强插件-mybatis Plsh

增强插件流行的两种

  1. tk.mybatis
  2. mybatis plus

注意:侵入性强的代码,劲量少用,不然后期维护会很麻烦(一旦增强插件不维护了)

Mybatis-Plsh

官网: https://mp.baomidou.com/guide/#%E7%89%B9%E6%80%A7

优点

  1. 侵入性小,损耗小
  2. 通过CRUD操作,支持分页
  3. 预防SQL注入,支持多种数据区
  4. 支持ActiveRecord支持代码生产等等

导入包

 <dependencies>
 
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    
    <!--主要包mybatis-plus-->
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.2.0</version>
    </dependency>
    
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>runtime</scope>
    </dependency>
</dependencies>

配置:

yml 配置

# DataSource Config
spring:
 datasource:
   driver-class-name: org.h2.Driver
   schema: classpath:db/schema-h2.sql
   data: classpath:db/data-h2.sql
   url: jdbc:h2:mem:test
   username: root
   password: test

在 Spring Boot 启动类中添加 @MapperScan 注解,扫描 Mapper 文件夹:

@SpringBootApplication
@MapperScan("com.baomidou.mybatisplus.samples.quickstart.mapper")
public class Application {

   public static void main(String[] args) {
       SpringApplication.run(QuickStartApplication.class, args);
   }

}

编写Mapper类 UserMapper.java继承BaseMapper<对应的泛型>

public interface UserMapper extends BaseMapper<User> {

}

Mybatis-Plsh 分页查询

https://mybatis.plus/guide/page.html

Mybatis-Plsh 乐观锁

https://mybatis.plus/guide/optimistic-locker-plugin.html#%E4%B8%BB%E8%A6%81%E9%80%82%E7%94%A8%E5%9C%BA%E6%99%AF

Mybatis-Plsh Mapper层接口

https://mp.baomidou.com/guide/crud-interface.html#mapper-crud-%E6%8E%A5%E5%8F%A3

Mybatis-Plsh 条件构造器

https://mp.baomidou.com/guide/wrapper.html#abstractwrapper

上一篇:P2698 [USACO12MAR]花盆Flowerpot——单调队列


下一篇:ABP(ASP.NET Boilerplate Project)学习总结