问题描述
在MyBatisPlus中经常会用到构造复杂查询条件的情况,比如:
- 测试代码
@SpringBootTest
class Query2WrapperTest {
@Resource
private UserMapper userMapper;
@Test
void fun() {
UserQuery userQuery = UserQuery.builder()
.state(1)
.gender(0)
.info("中")
.build();
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
qeury2Wrapper(userQuery, queryWrapper);
List<User> list = userMapper.selectList(queryWrapper);
list.forEach(System.out::println);
}
- 构造查询条件
private void qeury2Wrapper(UserQuery userQuery, QueryWrapper<User> queryWrapper) {
if (userQuery == null || queryWrapper == null) {
return;
}
//将UserQuery的值赋到queryWrapper中
if (userQuery.getId() != null) {
queryWrapper.eq("id", userQuery.getId());
}
if (userQuery.getNickname() != null) {
queryWrapper.like("nickname", userQuery.getNickname());
}
if (userQuery.getGender() != null) {
queryWrapper.eq("gender", userQuery.getGender());
}
if (userQuery.getBirthMin() != null) {
queryWrapper.ge("birth", userQuery.getBirthMin());
}
if (userQuery.getBirthMax() != null) {
queryWrapper.le("birth", userQuery.getBirthMax());
}
if (userQuery.getEmail() != null) {
queryWrapper.eq("email", userQuery.getEmail());
}
if (userQuery.getTel() != null) {
queryWrapper.eq("tel", userQuery.getTel());
}
if (userQuery.getAccount() != null) {
queryWrapper.eq("account", userQuery.getAccount());
}
if (userQuery.getInfo() != null) {
queryWrapper.like("info", userQuery.getInfo());
}
if (userQuery.getState() != null) {
queryWrapper.eq("state", userQuery.getState());
}
}
像上面这种手写转换不仅繁琐,而且容易出错,其实采用借助反射或内省的方式提取公共类,简化代码的书写。
反射方式
private void qeury2Wrapper2(UserQuery userQuery, QueryWrapper<User> queryWrapper) throws Exception {
Method[] methods = userQuery.getClass().getDeclaredMethods();
for (Method method : methods) {
String methodName = method.getName();
if (methodName.contains("get")) {
Object res = method.invoke(userQuery, null);
if (res != null) {
String columnName = getColumnName(methodName);
queryWrapper.like(columnName, res);
}
}
}
}
内省方式
private void qeury2Wrapper3(UserQuery userQuery, QueryWrapper<User> queryWrapper) throws Exception {
BeanInfo beanInfo = Introspector.getBeanInfo(userQuery.getClass());
PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor pd : pds) {
Method readMethod = pd.getReadMethod();
if (readMethod.getName().equals("getClass")) {
continue;
}
Object res = readMethod.invoke(userQuery, null);
if (res != null) {
String columnName = getColumnName(readMethod.getName());
queryWrapper.like(columnName, res);
}
}
}
公共代码
上面反射和内省用到的公共代码:
private static String getColumnName(String name) {
StringBuffer sb = new StringBuffer();
name = name.substring(3, name.length()); //去掉get前缀
char[] chars = name.toCharArray();
for (int i = 0; i < chars.length; i++) {
char item = chars[i];
if (item > 65 && item < 90) {//大写字母
sb.append("_");
sb.append((char) (item + 32));
} else {
sb.append(item);
}
}
return sb.deleteCharAt(0).toString();
}
标签:QueryWrapper,userQuery,MyBatisPlus,queryWrapper,res,UserQuery,反射,null,eq
From: https://blog.51cto.com/lianghecai/7908489