Skip to content

jiangchengshi/mybatis-assistant

Repository files navigation

欢迎使用 Mybatis Assistant

Maven License SpringBoot

简介

Mybatis助手 - 简化配置,自动生成CURD代码,拦截器带飞!

特点

配置灵活,基于MybatisO包,没有改变任何框架结构,只为简化; 简单配置,即可实现数据库操作

使用指引

  1. 引入数据库驱动包(以MySql为里)
implementation("mysql:mysql-connector-java:latest")
  1. 配置数据源属性(以MySql为里)
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://192.168.13.213:3336/mybatis-assistant
spring.datasource.username=root
spring.datasource.password=1234.abcd

或者

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://192.168.13.213:3336/mybatis-assistant
    username: root
    password: 1234.abcd
  1. 引入mybatis-assistant-boot-starter包
implementation("cool.doudou:mybatis-assistant-boot-starter:latest")
  1. 配置mybatis-assistant属性
mybatis.assistant.mapper-locations=classpath*:mapper/*Mapper.xml

或者

mybatis:
  assistant:
    mapper-locations: classpath*:mapper/*Mapper.xml

方法说明

查询

测试用例(推荐):分页参数PageDTO实体中赋值pageNum、pageSize即可

public class pageServiceTests {
    @Autowired
    private UserMapper userMapper;

    @Test
    public void select(PageDTO<User> pageDTO) {
        LambdaQuery<User> lambdaQuery = new LambdaQuery<>();
        // pageDTO.data 赋值 LambdaQuery
        List<User> userList = userMapper.selectPage(pageDTO.page(), lambdaQuery);
    }
}

字段填充

测试用例(推荐):实现IFieldFillHandler,完成插入、修改时字段值填充

public class MyFieldFillHandler implements IFieldFillHandler {
    @Override
    public void insert(MetaObject metaObject) {
        this.fill(metaObject, "createBy", 1L);
        this.fill(metaObject, "createTime", LocalDateTime.now());
    }

    @Override
    public void update(MetaObject metaObject) {
        this.fill(metaObject, "updateBy", 1L);
        this.fill(metaObject, "updateTime", LocalDateTime.now());
    }
}

逻辑删除

测试用例(推荐):实现IDeletedHandler,完成插入、修改时逻辑删除字段填充

public class MyDeletedHandler implements IDeletedHandler {
}

逻辑值:

  • 0:正常
  • -1:删除

多租户填充

测试用例(推荐):实现ITenantFillHandler,完成插入时字段值填充

public class MyTenantFillHandler implements ITenantFillHandler {
    @Override
    public Long getTenantId() {
        return 0L;
    }
}

脱敏

测试用例(推荐):实现IDesensitizeHandler,实体字段添加注解@Desensitize指定策略,完成数据返回时字段脱敏

public class User {
    @Desensitize(strategy = "userName")
    private String name;
}

默认策略:

  • 用户名:userName => 用户名前一后一
  • 身份证:idCard => 身份证前三后四
  • 移动电话:phoneNumber => 手机号码前三后四

版权

Apache License 2.0

鼓励一下,喝杯咖啡

欢迎提出宝贵意见,不断完善 Mybatis-Assistant

鼓励一下,喝杯咖啡

About

Mybatis助手

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages