MyBits 自己造轮子 查询时根据 PO类封装一个Example类

导读:本篇文章讲解 MyBits 自己造轮子 查询时根据 PO类封装一个Example类,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

##直接贴代码记录一下,简单的字符串替换隐藏了一下相关的隐私

/**
     * 查询出数据库中满足这些条件的数据,只处理字段相等的的情况
     * @param source  将数据库需要匹配的字段,根据数据库表与该类的对应关系,赋值到传入的实例中。
     * @return 查询到的数据
     * @throws Exception 反射的异常,mybits的异常
     */
public List<Source> getByPropertiesEquals(Source source) throws Exception{

        // 想写个 List<Source> selectByBean(Source source);
        Class<? extends Source> sourceClass = source.getClass();
         SourceExample example = new SourceExample();
         SourceExample.Criteria  exampleCriteria = example.createCriteria();
//         example.createCriteria().andMobileEqualTo("");
//         example.createCriteria().andMobileEqualTo("").andTransitionStatusEqualTo("");
//         example.or().andMobileEqualTo("");
         Class<? extends SourceExample.Criteria> exampleCriteriaClass = exampleCriteria.getClass();

         Method[] methods = sourceClass.getDeclaredMethods();
         for (Method method : methods){
             //获取方法名
             String propertyGetMethodName = method.getName();
             //获取返回类型
             Type type=method.getGenericReturnType();
//             (Class.forName(type.toString()))method.invoke(source);

         }
         Field[] fields = sourceClass.getDeclaredFields();
         for (Field field : fields){
             //属性名
             String fieldNameTmp = field.getName();
             //获得get方法
             PropertyDescriptor pd = new PropertyDescriptor(fieldNameTmp,sourceClass);
             Method getMethod = pd.getReadMethod();
             //执行get方法返回一个Object
//             Object obj = getMethod.invoke(sourceClass);
//             System.out.println(obj.toString());
//             String typeString = field.getGenericType().toString();//貌似不用转化了
             //这块是数据库的实体类类型还是可控的就都写出来了
//             if ("class java.lang.String".equals(typeString)){//貌似不用转化了
//                 String propertyStringTmp = (String) getMethod.invoke(sourceClass);
//             Object propertyStringTmp = getMethod.invoke(sourceClass);
             //invoke  传入的是实体  上边的是错误的
                 Object propertyStringTmp = getMethod.invoke(source);
             Type type=getMethod.getGenericReturnType();
             //没有赋值的属性不添加到查询条件里,避免出现不理想的sql语句 其他字段必须为null才查的出来 该条件
                 //理想sql语句select a,b,c   from xxx where (a=xx and b=xx);
                 //不理想sql语句select a,b,c   from xxx where (a=xx and b=xx and c is null);
//                 if (propertyStringTmp.equals(null)){//宛若一个智障。哈哈
                 if (propertyStringTmp==null){
                     continue;
                 }
                 // 将属性的首字母大写
                 fieldNameTmp = fieldNameTmp.replaceFirst(fieldNameTmp.substring(0, 1), fieldNameTmp.substring(0, 1)
                         .toUpperCase());
                 //获取SourceExample.Criteria  中的对应方法
//             如果你供应GetMethod方法只有方法名,然后它会假设你正在寻找一个无参数的方法。GetMethod方法为您寻找方法的参数的类变参数。
//             https://stackoverflow.com/questions/15575585/java-lang-nosuchmethodexception
//                 Method exampleCriteriaMethod = exampleCriteriaClass.getMethod("and"+fieldNameTmp+"EqualTo");
//                 Method exampleCriteriaMethod = exampleCriteriaClass.getMethod("and"+fieldNameTmp+"EqualTo",Class.forName(type.toString()));//不能的到class报错Class not found
                 Method exampleCriteriaMethod = exampleCriteriaClass.getMethod("and"+fieldNameTmp+"EqualTo",Class.forName(type.getTypeName()));
//                 exampleCriteria.andMobileEqualTo("");
             //执行SourceExample.Criteria  中的对应方法,参数为Source中的属性
//            执行这句话 exampleCriteria.andMobileEqualTo(String.valueOf(propertyStringTmp));
             exampleCriteriaMethod.invoke(exampleCriteria,propertyStringTmp);
//             exampleCriteriaMethod.invoke(exampleCriteria,null);//看看null能不能传入object
//             }//貌似不用转化了  end
         }
         return sourceMapper.selectByExample(example);

    }

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/76487.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!