忽略null值,忽略空字符串,自动对字符串trim操作
说明
mybatis-mp 默认不对 null 、空字符 进行处理,这是一种安全的设计!!!
如何同时忽略和空白字符串优化?
java
SysUser sysUser = QueryChain.of(sysUserMapper)
// 忽略 null 条件参数
// 忽略 空字符串 条件参数
// 对字符串进行trim 去空格操作
.forSearch()
.select(SysUser::getId)
.from(SysUser.class)
.eq(SysUser::getUserName, null )
.eq(SysUser::getUserName, "" )
.eq(SysUser::getUserName," admin ");
等同于:
java
SysUser sysUser = QueryChain.of(sysUserMapper)
// 忽略 null 条件参数
.ignoreNullValueInCondition(true)
// 忽略 空字符串 条件参数
.ignoreEmptyInCondition(true)
// 对字符串进行trim 去空格操作
.trimStringInCondition(true);
手动方式忽略?
java
Integer id= XXX;
String userName= YYY;
SysUser sysUser = QueryChain.of(sysUserMapper)
.select(SysUser.class,SysRole.class)
.from(SysUser.class)
.eq(SysUser::getId,id,Objects::nonNull)
.like(SysUser::getUserName,userName,StringUtils::isNotBlank)
.get();
或
java
Integer id= XXX;
String userName= YYY;
SysUser sysUser = QueryChain.of(sysUserMapper)
.select(SysUser.class,SysRole.class)
.from(SysUser.class)
.eq(Objects.nonNull(id),SysUser::getId,id)
.like(StringUtils.isNotBlank(userName),SysUser::getUserName,userName)
.get();