Mybaits-plus實戰(二)


1. Mybaits-plus實戰(二)

1.1. mybatis-plus插件

1.1.1. 用法

  1. 先舉個例子介紹用法,如下:直接作為Bean注入,一般來講插件太多印象性能,所以大部分插件都只在測試,開發環境使用,一般不上生產環境,下面我介紹的插件都以xml格式配置為例,因為那樣參數展示會多一些,使用時直接pom使用或作為如下Bean注入
    @Bean
    public PerformanceInterceptor performanceInterceptor() {
        PerformanceInterceptor performanceInterceptor = new PerformanceInterceptor();
        /*<!-- SQL 執行性能分析,開發環境使用,線上不推薦。 maxTime 指的是 sql 最大執行時長 -->*/
        performanceInterceptor.setMaxTime(1000);
        /*<!--SQL是否格式化 默認false-->*/
        performanceInterceptor.setFormat(false);
        return performanceInterceptor;
    }

1.1.2. 分頁插件

<plugins>
    <!--
     | 分頁插件配置
     | 插件提供二種方言選擇:1、默認方言 2、自定義方言實現類,兩者均未配置則拋出異常!
     | overflowCurrent 溢出總頁數,設置第一頁 默認false
     | optimizeType Count優化方式 ( 版本 2.0.9 改為使用 jsqlparser 不需要配置 )
     | -->
    <!-- 注意!! 如果要支持二級緩存分頁使用類 CachePaginationInterceptor 默認、建議如下!! -->
    <plugin interceptor="com.baomidou.mybatisplus.plugins.PaginationInterceptor">
        <property name="sqlParser" ref="自定義解析類、可以沒有" />
        <property name="localPage" value="默認 false 改為 true 開啟了 pageHeper 支持、可以沒有" />
        <property name="dialectClazz" value="自定義方言類、可以沒有" />
    </plugin>
</plugins>

1.1.2.1. json序列化移除transient

  1. 看到有個jackson的json序列化溢出transient功能,即controller層請求返回對象中屬性若有transient修飾,則去掉該屬性不傳回去
  2. 但我不習慣用jackson,所以改了fastjson的方式
@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
        FastJsonConfig fj = new FastJsonConfig();
        fj.setSerializerFeatures(SerializerFeature.SkipTransientField);
        fastJsonHttpMessageConverter.setFastJsonConfig(fj);
        converters.add(fastJsonHttpMessageConverter);
    }
}

1.1.3. 執行分析插件

  1. 用作攔截全表的delete和update操作,防止瞎寫的sql語句
<plugins>
    <!-- SQL 執行分析攔截器 stopProceed 發現全表執行 delete update 是否停止運行 -->
    <plugin interceptor="com.baomidou.mybatisplus.plugins.SqlExplainInterceptor">
        <property name="stopProceed" value="false" />
    </plugin>
</plugins>

1.1.4. 性能分析插件

  1. 用於輸出每條 SQL 語句及其執行時間
<plugins>
    ....

    <!-- SQL 執行性能分析,開發環境使用,線上不推薦。 maxTime 指的是 sql 最大執行時長 -->
    <plugin interceptor="com.baomidou.mybatisplus.plugins.PerformanceInterceptor">
        <property name="maxTime" value="100" />
        <!--SQL是否格式化 默認false-->
        <property name="format" value="true" />
    </plugin>
</plugins>

1.1.5. 樂觀鎖插件

  1. 實體字段需要加上@version注解
<bean class="com.baomidou.mybatisplus.plugins.OptimisticLockerInterceptor"/>
public class User {

    @Version
    private Integer version;

    ...
}

1.1.6. 注入自定義SQL

  1. 自定義注入全表刪除方法 deteleAll

1.1.6.1. java

  1. 配置注入方法
public class MySqlInjector extends AutoSqlInjector {

    @Override
    public void inject(Configuration configuration, MapperBuilderAssistant builderAssistant, Class<?> mapperClass,
            Class<?> modelClass, TableInfo table) {
        /* 添加一個自定義方法 */
        deleteAllUser(mapperClass, modelClass, table);
    }

    public void deleteAllUser(Class<?> mapperClass, Class<?> modelClass, TableInfo table) {

        /* 執行 SQL ,動態 SQL 參考類 SqlMethod */
        String sql = "delete from " + table.getTableName();

        /* mapper 接口方法名一致 */
        String method = "deleteAll";
        SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
        this.addMappedStatement(mapperClass, method, sqlSource, SqlCommandType.DELETE, Integer.class);
    }

}

1.1.6.2. Mapper

  1. 聲明方法
public interface UserMapper extends BaseMapper<User> {

    /**
     * 自定義注入方法
     */
    int deleteAll();

}

1.1.6.3. xml

  1. 啟用
<!-- 定義 MP 全局策略,安裝集成文檔部分結合 -->
<bean id="globalConfig" class="com.baomidou.mybatisplus.entity.GlobalConfiguration">
    .....

  <!-- 自定義注入 deleteAll 方法  -->
  <property name="sqlInjector" ref="mySqlInjector" />
</bean>

<!-- 自定義注入器 -->
<bean id="mySqlInjector" class="com.baomidou.test.MySqlInjector" />

1.1.7. 公共字段自動填充

MyMetaObjectHandler的用法,前一篇文章有用到

1.1.8. 邏輯刪除

LogicSqlInjector的用法,前一篇有用到

1.1.9. 讀寫分離與多數據源

  1. 集成pom
<dependency>
  <groupId>com.baomidou</groupId>
  <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
  <version>2.x.x</version>
</dependency>
  1. 配置主從數據源
spring:
  datasource:
    dynamic:
      primary: master #設置默認的數據源或者數據源組,默認值即為master,如果你主從默認下主庫的名稱就是master可不定義此項。
      datasource:
        master:
          username: root
          password: 123456
          driver-class-name: com.mysql.jdbc.Driver
          url: jdbc:mysql://47.100.20.186:3306/dynamic?characterEncoding=utf8&useSSL=false
        slave_1:
          username: root
          password: 123456
          driver-class-name: com.mysql.jdbc.Driver
          url: jdbc:mysql://47.100.20.186:3307/dynamic?characterEncoding=utf8&useSSL=false
        slave_2:
          username: root
          password: 123456
          driver-class-name: com.mysql.jdbc.Driver
          url: jdbc:mysql://47.100.20.186:3308/dynamic?characterEncoding=utf8&useSSL=false
       #......省略
       #以上會配置一個默認庫master,一個組slave下有兩個子庫slave_1,slave_2
  1. 切換數據源
    使用 @DS 注解切換數據源。

1.1.9.1. 讀寫分離

1.1.9.1.1. 配置
sharding:
  jdbc:
    datasource:
      names: ds_master,ds_slave_0,ds_slave_1
      ds_master:
        ...
      ds_slave_0:
        ...
      ds_slave_1:
        ...
    config:
      masterslave:
        load-balance-algorithm-type: round_robin
        name: ds_ms
        master-data-source-name: ds_master
        slave-data-source-names: ds_slave_0,ds_slave_1

1.1.10. Sequence主鍵

  1. 它提供了id_worker,id_worker_str,uuid三種自定義主鍵生成器,對oracle可以用OracleKeyGenerator配置主鍵自增
  2. id_worker就是雪花算法

1.1.11. 其他功能

  1. 多租戶 SQL 解析器
  2. 通用枚舉掃描並自動關聯注入

1.2. 條件構造器

  1. 開始接觸mybatis-plus肯定會常常看到AbstractWrapper這個條件包裝類,通常使用兩個實現QueryWrapperUpdateWrapper
  2. 我列一些覺得常用的
    1. allEq,常用allEq(Map<R, V> params)allEq(Map<R, V> params, boolean null2IsNull),分別表示key對應的值為null,需不需要判斷isnull;allEq(BiPredicate<R, V> filter, Map<R, V> params) 應該比較少用

      • 例子:值的范圍大於2000的條件才被入選
      Map<String, Object> map = new HashMap<>();
      map.put("daily_price_id", 1117L);
      QueryWrapper<DailyPrice> queryWrapper = new QueryWrapper<DailyPrice>().allEq((k, v) -> ((Long) v) > 2000L, map);
      List<DailyPrice> dailyPrices = dailyPriceService.getBaseMapper().selectList(queryWrapper);
      
    2. eq,很明顯,=

    3. ne不等於

    4. 大於小於gt,lt,范圍betweenlike,常用的關鍵字都有

    5. last直接拼接sql到最后,如:last("limit 1")

  3. 至於聯合查詢什么的,或者條件比較復雜的and,or啥的,我覺的還是直接在mapper.xml里直接寫sql比較好,那樣更簡潔明了,它的語法查些簡單的語句還是不錯的

1.3. 附加知識點

  1. 在逐步研究mybatis-plus的過程中,遇到的一些實用知識點整理下

1.3.1. 跳過transient屬性的值

  1. 我們從數據庫所取的字段很多時候是有冗余的,加了transient字段,在controller層返回的時候,json會過濾掉它
  2. 還需要配置內容如下,主要是SerializerFeature.SkipTransientField
@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
        FastJsonConfig fj = new FastJsonConfig();
        // 跳過transient屬性的值,瀏覽器兼容,日期自動格式化
        fj.setSerializerFeatures(SerializerFeature.SkipTransientField,SerializerFeature.BrowserCompatible,SerializerFeature.WriteDateUseDateFormat);
        fastJsonHttpMessageConverter.setFastJsonConfig(fj);
        converters.add(fastJsonHttpMessageConverter);
    }
   }

1.3.2. 日期自動格式化

  1. 對日期字段,返回前端需要的是格式化后的,所以也如上配置SerializerFeature.WriteDateUseDateFormat即可

官網gitee:https://baomidou.gitee.io/mybatis-plus-doc/#/page-plugin
官網:https://mp.baomidou.com/guide/wrapper.html#abstractwrapper


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM