- import org.apache.ibatis.executor.Executor;
- import org.apache.ibatis.mapping.MappedStatement;
- import org.apache.ibatis.plugin.*;
-
- import java.util.Properties;
-
- @Intercepts({
- @Signature(
- type = Executor.class,//表示拦截执行器的方法
- method = "update",//表示拦截执行器中的update方法
- args = {MappedStatement.class,Object.class}
- )
- })
- public class MyIntercepter implements Interceptor {
- @Override
- public Object intercept(Invocation invocation) throws Throwable {
- //拦截方法,具体业务逻辑编写的位置
- return invocation.proceed();
- }
-
- @Override
- public Object plugin(Object target) {
- //创建target对象的代理对象,目的是将当前的拦截器加入到该对象中,这个方法会被执行4次:因为有4中拦截的情况
- return Plugin.wrap(target,this);
- }
-
- @Override
- public void setProperties(Properties properties) {
- //属性设置
- }
- }
- @Configuration
- @MapperScan("com.cd.mp.mapper")
- public class MybatisPlusConfig {
-
- @Bean//设置分页插件
- public PaginationInterceptor paginationInterceptor(){
- return new PaginationInterceptor();
- }
-
- @Bean
- public MyIntercepter myIntercepter(){
- return new MyIntercepter();
- }
- }
2.插件配置
3.实体类中配置
4.数据库表也要version字段
5.测试
插件会自动给version加一
总是假设最好的情况,每次去拿数据的时候都认为别人不会修改,所以不会上锁,但是在更新的时候会判断一下在此期间别人有没有去更新这个数据,可以使用版本号机制和CAS算法实现。
缺点:
1.首先在实体类中用@TableField注解file来设置什么时候自动填充
2.继承MetaObject
1.数据库中的表需要一个对应的deleted字段
2.实体类
3.springBoot的配置文件
1.首先表要有对应的字段
后端接收前端传来的String类型参数需要用枚举来接收的话,就需要定义一个自定义转换器
- import com.dingli.framework.eunm.MarryEumn;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.core.convert.converter.Converter;
- import org.springframework.format.FormatterRegistry;
- import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
-
- @Configuration
- public class WebConfig implements WebMvcConfigurer {
-
- @Override
- public void addFormatters(FormatterRegistry registry) {
- registry.addConverter(new Converter<String, MarryEumn>() {
- @Override
- public MarryEumn convert(String s) {
- MarryEumn marryEumn = null;
- if (s.equals("未婚")){
- marryEumn = MarryEumn.NOMARRY;
- }
- if (s.equals("订婚")){
- marryEumn = MarryEumn.RESERVEMARRY;
- }
- if (s.equals("已婚")){
- marryEumn = MarryEumn.MARRY;
- }
- if (s.equals("离婚")){
- marryEumn = MarryEumn.DIVORCE;
- }
- return marryEumn;
- }
- });
- }
- }
2.定义枚举:继承IEnum接口
@EnumValue:指定数据库中存的值
@JsonValue:指定返回给前端的值
- import com.baomidou.mybatisplus.annotation.EnumValue;
- import com.baomidou.mybatisplus.core.enums.IEnum;
- import com.fasterxml.jackson.annotation.JsonValue;
-
- public enum MarryEumn implements IEnum
{ -
- NOMARRY(0,"未婚"),
- RESERVEMARRY(1,"订婚"),
- MARRY(2,"已婚"),
- DIVORCE(3,"离婚");
-
- @EnumValue
- private int value;
- @JsonValue
- private String desc;
-
- MarryEumn(int value, String desc) {
- this.value = value;
- this.desc = desc;
- }
-
- @Override
- public Integer getValue() {
- return this.value;
- }
-
- @Override
- public String toString() {
- return this.desc;
- }
-
- }
3.springboot中配置
default-enum-type-handler:这个也很重要,这里不配的话,如果是自己写xml文件,在sql中也会写
#{marriage,typeHandler=org.apache.ibatis.type.EnumTypeHandler}
- mybatis-plus:
- global-config:
- db-config:
- id-type: auto
- configuration:
- log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
- default-enum-type-handler: org.apache.ibatis.type.EnumOrdinalTypeHandler
- type-enums-package: com.dingli.framework.eunm
- mapper-locations: classpath*:mappers/*Mapper.xml
4.修改实体