zoukankan      html  css  js  c++  java
  • Example.of查询的条件控制,怎么进行where查询?

    【问题描述-】

    使用Example.of(POJO)和findAll(example)查询时一直没有数据,返回的List集合的size()为0,导致重复的数据不断插入。

    经搜索已知findAll(example)查询时会自动的忽略掉POJO中值为NULL的属性

    【问题分析-】

    设置spring.jpa.show-sql=true
    观察日志生成SQL语句
    select 
    
    product0_.id as id1_0_, 
    product0_.endtime as endtime2_0_, 
    product0_.proauthor as proautho3_0_, 
    product0_.probackcount as probackc4_0_, product0_.probackermoney as probacke5_0_, 
    product0_.proid as proid6_0_, 
    product0_.proimgpath as proimgpa7_0_, product0_.promoneypercent as promoney8_0_, 
    product0_.prostatus as prostatu9_0_,
    product0_.protitle as protitl10_0_, 
    product0_.protype as protype11_0_, 
    product0_.starttime as startti12_0_
     from md_item product0_ 
    
    where product0_.id=0 and product0_.proid=?
    可以注意到where后面有两个条件
      findAll(example)查询时会自动的忽略掉POJO中值为NULL的属性,给与Example.of的POJO的确只设置了一个值
    //如果实体的属性是null,它就会忽略它,这里只传一个proId参数就好
    Product proExample=new Product();
    proExample.setProid(proId);
    //查询并判断数据是否存在
    List<Product> examples = productService.findAll(proExample);
    System.out.println("list有无数据:"+examples.size());
    if(examples.size()>0){
       System.out.println("===数据已存在===");
       continue;
    }

    那么product0_.id=0是怎么出现的呢?

    【问题解决】

    找到POJO类,原本POJO类的id属性对应数据库的属性,被设置为long型,所以出现了默认值0;

    最终修改为String 问题解决。

    findAll(Example)可以进行些许的String转换,故和数据库列数据类型不同也不会有问题。

  • 相关阅读:
    一个经典的页面布局
    巧避new的一个原型设计模式
    讲解关于javascript的继承
    根据指定日期 算出该周的一周日期
    原型实现的一个观察者模式
    减少类之间的耦合性
    Android 代码下载
    (转)open gl 实例 demo vs2005 环境
    Tile editor 快捷键
    这是第一篇博客~
  • 原文地址:https://www.cnblogs.com/YFEYI/p/15233114.html
Copyright © 2011-2022 走看看