使用反射完成mybatis-plus自动装配查询条件

简介: 使用反射完成mybatis-plus自动装配查询条件

先上DO代码:


@Data
@TableName("dict")
public class DictDo {
  @TableId(type=IdType.AUTO)
  private String id;
  @TableField("`key`")
  private String key;
  private String value;
  private String memo;
}

此处使用了lombok,自动生成对象方法(此处是否与我们的思想有异曲同工之妙呢)!

使用原生的查询条件拼装:

public List<DictDo> selectListEq(DictDo one) {
    QueryWrapper<DictDo> wrapper = new QueryWrapper<>();
    if (null != one.getId()) {
      wrapper.eq("id", one.getId());
    }
    if (null != one.getKey() && !"".equals(one.getKey())) {
      wrapper.eq("`key`", one.getKey());
    }
    if (null != one.getValue() && !"".equals(one.getValue())) {
      wrapper.eq("value", one.getValue());
    }
    if (null != one.getMemo() && !"".equals(one.getMemo())) {
      wrapper.eq("memo", one.getMemo());
    }
    return dictDao.selectList(wrapper);
  }

如上所示,每个对象如果只有几条属性还好说,但是如果有几十条属性呢?

我们应该想到所有重复的劳动都是低价值的、可以被替代的!

工具代码:

public static QueryWrapper parseQuery(Object service) throws Exception {
    QueryWrapper<Object> wrapper = new QueryWrapper<>();
    Class<? extends Object> doClass = service.getClass();
    Method[] methods = doClass.getDeclaredMethods();
    Field[] fields = doClass.getDeclaredFields();
    for (Field field : fields) {
      for (Method method : methods) {
        if (method.getName().equalsIgnoreCase("get" + field.getName())) {
          String value = doClass.getDeclaredMethod(method.getName()).invoke(service) == null ? ""
              : (String) doClass.getDeclaredMethod(method.getName()).invoke(service);
          if (null != value && !"".equals(value)) {
            wrapper.eq("`"+field.getName()+"`", doClass.getDeclaredMethod(method.getName()).invoke(service));
            break;
          }
        }
      }
    }
    return wrapper;
  }


Service进行调用:


/**
  * 按对象属性匹配
  * @throws Exception 
  */
  public List<DictDo> selectListEq(DictDo one) throws Exception {
  QueryWrapper<DictDo> wrapper = ParamSettingUtil.parseQuery(one);
  return dictDao.selectList(wrapper);
  }


进行测试:


@SuppressWarnings("unchecked")
  @Test
  public void testSelect() {
  System.out.println("## testSelect");
  DictDo dictDo=new DictDo();
  dictDo.setKey("233");
  QueryWrapper<DictDo> wrapper;
    try {
    wrapper = ParamSettingUtil.parseQuery(dictDo);
    List<DictDo> dictDos= dictDao.selectList(wrapper);
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
  }


测试结果:

image.png

如上图看到,我们新加进去的条件已经生效了!


相关文章
|
3月前
|
XML Java 数据库连接
mybatis中在xml文件中通用查询结果列如何使用
mybatis中在xml文件中通用查询结果列如何使用
189 0
|
3月前
|
Java 数据库连接 mybatis
Mybatis 多级分类查询
Mybatis 多级分类查询
46 0
|
3月前
|
SQL
MyBatis-Plus-Join关联查询
MyBatis-Plus-Join关联查询
189 2
|
3月前
|
SQL Java 关系型数据库
Mybatis多表关联查询与动态SQL(下)
Mybatis多表关联查询与动态SQL
93 0
|
3月前
|
SQL Java 数据库连接
Mybatis多表关联查询与动态SQL(上)
Mybatis多表关联查询与动态SQL
80 0
|
3月前
|
SQL 缓存 Java
mybatis 一对多查询
mybatis 一对多查询
58 0
|
3月前
|
SQL XML Java
MyBatis-Plus多表关联查询
MyBatis-Plus多表关联查询
294 0
|
1月前
|
Java 数据库连接 mybatis
Mybatis查询传递单个参数和传递多个参数用法
Mybatis查询传递单个参数和传递多个参数用法
39 11
MybatisPlus-标准CRUD制作,新增boolean save(T t),删除 ~ delete(int id),修改 ~ update(T t),根据id查询,T getById....
MybatisPlus-标准CRUD制作,新增boolean save(T t),删除 ~ delete(int id),修改 ~ update(T t),根据id查询,T getById....
MyBatisPlus如何根据id批量查询?Required request parameter ‘id‘ for method 解决方法是看青戈大佬MybatisPlus的教程
MyBatisPlus如何根据id批量查询?Required request parameter ‘id‘ for method 解决方法是看青戈大佬MybatisPlus的教程