为什么要引入 Mybatis 增强插件
从一个业务开发者的角度来看,这种类似的增强框架使用起来很爽。
- 单表情况下不必再把思路从
Service
切换到Mapper
,从业务思维(业务流程)切换到数据库思维(数据库字段,编写),一定程度上减少了代码的开发量。 - 易于维护 数据库增改删字段 不必再去
xml
里改,一处Refactor,多处修改,也可以避免一些cv 过程中的低级错误
Mybatis-Flex 与 Mybatis-Plus 的区别
基础查询:
Mybatis-Flex:
查询:
QueryWrapper query = QueryWrapper.create()
.where(EMPLOYEE.LAST_NAME.like(searchWord)) //条件为null时自动忽略
.and(EMPLOYEE.GENDER.eq(1))
.and(EMPLOYEE.AGE.gt(24));
List<Employee> employees = employeeMapper.selectListByQuery(query);
Mybatis-Plus:
LambdaQueryWrapper<Employee> queryWrapper = Wrappers.<Employee>lambdaQuery()
.like(StringUtils.isNotEmpty(searchWord), Employee::getUserName,"B")
.eq(Employee::getGender, 1)
.gt(Employee::getAge, 24);
List<Employee> employees = employeeMapper.selectList(queryWrapper);
更新:
Mybatis-Flex:
Account account = UpdateEntity.of(Account.class);
account.setId(100); //设置主键
account.setUserName("michael");
account.setAge(18);
account.setBirthday(null);
accountMapper.update(account);
Mybatis-Plus:
UpdateWrapper<Account> updateWrapper = new UpdateWrapper<>();
updateWrapper.eq("id", 100);
updateWrapper.set("user_name", "michael");
updateWrapper.set("age", 18);
updateWrapper.set("birthday", null);
accountMapper.update(null, updateWrapper);
查询集合函数:
Mybatis-Flex:
QueryWrapper query = QueryWrapper.create()
.select(
ACCOUNT.ID,
ACCOUNT.USER_NAME,
max(ACCOUNT.BIRTHDAY),
avg(ACCOUNT.SEX).as("sex_avg")
);
List<Employee> employees = employeeMapper.selectListByQuery(query);
Mybatis-Plus:
QueryWrapper<Employee> queryWrapper = Wrappers.query()
.select(
"id",
"user_name",
"max(birthday)",
"avg(birthday) as sex_avg"
);
List<Employee> employees = employeeMapper.selectList(queryWrapper);
联表查询 flex 是支持的 mp 不支持,虽然支持但是复杂联表可读性不高,个人觉得关于联表的 sql 还是自行维护在 xml 里面
附示例:
QueryWrapper query = QueryWrapper.create()
.select().from(ACCOUNT)
.leftJoin(ARTICLE).on(ACCOUNT.ID.eq(ARTICLE.ACCOUNT_ID))
.where(ACCOUNT.AGE.ge(10));
List<Account> accounts = mapper.selectListByQuery(query);
QueryWrapper query = new QueryWrapper()
.select(
ACCOUNT.ID
, ACCOUNT.USER_NAME
, ARTICLE.ID.as("articleId")
, ARTICLE.TITLE)
.from(ACCOUNT.as("a"), ARTICLE.as("b"))
.where(ACCOUNT.ID.eq(ARTICLE.ACCOUNT_ID));
关于性能提升:
环境:jdk17
Mybatis-Plus:
Mybatis-Flex:
发现一些问题
mp 的 selectone 方法源码如下
全量获取了表数据 有内存泄露的风险
这一点 mf 就处理的好一点
标签:Flex,ACCOUNT,QueryWrapper,Plus,Mybatis,query,ID From: https://www.cnblogs.com/yuylsjs/p/17608501.html