Springboot 插件集成
|Word count:416|Reading time:1min|Post View:
Mybatis-plus
Mybatis-Plus
是在 Mybatis
的基础上进行扩展,只做增强不做改变,可以兼容 Mybatis
原生的特性。同时支持通用CRUD操作、多种主键策略、分页、性能分析、全局拦截等。极大帮助我们简化开发工作。
添加整合依赖
1 2 3 4 5 6
| <!-- mybatis-plus增强CRUD --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.2</version> </dependency>
|
在文件 application.yml中,
修改mybatis配置为mybatis-plus
1 2 3
| mybatis-plus: mapper-locations: classpath*:mapper/**/*Mapper.xml typeAliasesPackage: com.report.**.bean
|
添加 Mybatis Plus
配置 MybatisPlusConfig.java
。 PS:原来的 MyBatisConfig.java
需要删除掉
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| importcom.baomidou.mybatisplus.annotation.DbType; importcom.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; importcom.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor; importcom.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor; importcom.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; importorg.mybatis.spring.annotation.MapperScan; importorg.springframework.context.annotation.Bean; importorg.springframework.context.annotation.Configuration; importorg.springframework.transaction.annotation.EnableTransactionManagement;
/** * Mybatis Plus配置 * *@authorruoyi */ @EnableTransactionManagement(proxyTargetClass =true) @Configuration public classMybatisPlusConfig { @Bean publicMybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor interceptor =newMybatisPlusInterceptor(); //分页插件 interceptor.addInnerInterceptor(paginationInnerInterceptor()); //乐观锁插件 interceptor.addInnerInterceptor(optimisticLockerInnerInterceptor()); //阻断插件 interceptor.addInnerInterceptor(blockAttackInnerInterceptor()); returninterceptor; }
/** *分页插件,自动识别数据库类型https://baomidou.com/guide/interceptor-pagination.html */ publicPaginationInnerInterceptor paginationInnerInterceptor() { PaginationInnerInterceptor paginationInnerInterceptor =newPaginationInnerInterceptor(); //设置数据库类型为mysql paginationInnerInterceptor.setDbType(DbType.MYSQL); //设置最大单页限制数量,默认 500条,-1不受限制 paginationInnerInterceptor.setMaxLimit(-1L); returnpaginationInnerInterceptor; }
/** *乐观锁插件https://baomidou.com/guide/interceptor-optimistic-locker.html */ publicOptimisticLockerInnerInterceptor optimisticLockerInnerInterceptor() { return newOptimisticLockerInnerInterceptor(); }
/** *如果是对全表的删除或更新操作,就会终止该操作https://baomidou.com/guide/interceptor-block-attack.html */ publicBlockAttackInnerInterceptor blockAttackInnerInterceptor() { return newBlockAttackInnerInterceptor(); } }
|
参考:
插件集成 | 若依