MybatisPlusConfig.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package com.zhongzheng.common.config;
  2. import com.baomidou.mybatisplus.annotation.DbType;
  3. import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
  4. import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
  5. import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
  6. import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
  7. import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
  8. import com.zhongzheng.common.croe.CreateAndUpdateMetaObjectHandler;
  9. import com.zhongzheng.common.croe.CustomTenantLineHandler;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.context.annotation.Bean;
  12. import org.springframework.context.annotation.Configuration;
  13. import org.springframework.transaction.annotation.EnableTransactionManagement;
  14. @EnableTransactionManagement(proxyTargetClass = true)
  15. @Configuration
  16. public class MybatisPlusConfig {
  17. @Autowired
  18. private CustomTenantLineHandler customTenantLineHandler;
  19. @Bean
  20. public MybatisPlusInterceptor mybatisPlusInterceptor() {
  21. MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
  22. // 多租户插件(注意:这个一定要放在最上面)
  23. interceptor.addInnerInterceptor(new TenantLineInnerInterceptor(customTenantLineHandler));
  24. // 分页插件
  25. interceptor.addInnerInterceptor(paginationInnerInterceptor());
  26. // 乐观锁插件
  27. interceptor.addInnerInterceptor(optimisticLockerInnerInterceptor());
  28. // 阻断插件
  29. // interceptor.addInnerInterceptor(blockAttackInnerInterceptor());
  30. return interceptor;
  31. }
  32. /**
  33. * 分页插件,自动识别数据库类型
  34. * https://baomidou.com/guide/interceptor-pagination.html
  35. */
  36. public PaginationInnerInterceptor paginationInnerInterceptor() {
  37. PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
  38. // 设置数据库类型为mysql
  39. paginationInnerInterceptor.setDbType(DbType.MYSQL);
  40. // 设置最大单页限制数量,默认 500 条,-1 不受限制
  41. paginationInnerInterceptor.setMaxLimit(-1L);
  42. return paginationInnerInterceptor;
  43. }
  44. /**
  45. * 乐观锁插件
  46. * https://baomidou.com/guide/interceptor-optimistic-locker.html
  47. */
  48. public OptimisticLockerInnerInterceptor optimisticLockerInnerInterceptor() {
  49. return new OptimisticLockerInnerInterceptor();
  50. }
  51. /**
  52. * 如果是对全表的删除或更新操作,就会终止该操作
  53. * https://baomidou.com/guide/interceptor-block-attack.html
  54. */
  55. // public BlockAttackInnerInterceptor blockAttackInnerInterceptor() {
  56. // return new BlockAttackInnerInterceptor();
  57. // }
  58. /**
  59. * sql性能规范插件(垃圾SQL拦截)
  60. * 如有需要可以启用
  61. */
  62. // public IllegalSQLInnerInterceptor illegalSQLInnerInterceptor() {
  63. // return new IllegalSQLInnerInterceptor();
  64. // }
  65. /**
  66. * 自定义主键策略
  67. * https://baomidou.com/guide/id-generator.html
  68. */
  69. // @Bean
  70. // public IdentifierGenerator idGenerator() {
  71. // return new CustomIdGenerator();
  72. // }
  73. /**
  74. * 元对象字段填充控制器
  75. * https://baomidou.com/guide/auto-fill-metainfo.html
  76. */
  77. @Bean
  78. public MetaObjectHandler metaObjectHandler() {
  79. return new CreateAndUpdateMetaObjectHandler();
  80. }
  81. /**
  82. * sql注入器配置
  83. * https://baomidou.com/guide/sql-injector.html
  84. */
  85. // @Bean
  86. // public ISqlInjector sqlInjector() {
  87. // return new DefaultSqlInjector();
  88. // }
  89. /**
  90. * TenantLineInnerInterceptor 多租户插件
  91. * https://baomidou.com/guide/interceptor-tenant-line.html
  92. * DynamicTableNameInnerInterceptor 动态表名插件
  93. * https://baomidou.com/guide/interceptor-dynamic-table-name.html
  94. */
  95. }