jpa查找条件列表

问题描述 投票:0回答:1

我还是Java的初学者,在这里我试图使用JPA从mysql数据库中获取数据。

但我在通过条件查找功能设置条件列表时遇到问题。我可以设置日期对象,即使它拒绝输入以下是我的代码以供参考。

我的查询:

select t.id, t.MSISDN, t.Param1, t.param2
  from BULK_REPOSITORY t
where t.Camp_Start_Date between Sysdate - 2 and sysdate
   and t.status = 0
   and t.camp_type = 1;

应用程序:

@SpringBootApplication
public class AccessingDataJpaApplication {

    private static final Logger log = LoggerFactory.getLogger(AccessingDataJpaApplication.class);
    Bulk_repository bulk ;


    public static void main(String[] args) {
        SpringApplication.run(AccessingDataJpaApplication.class);
    }


    @Bean
    public CommandLineRunner demo(Bulk_repositoryRepository repository) {
        return (args) -> {

            // fetch customers by Status
            log.info("Customer found with findByStatus('0'):");
            log.info("--------------------------------------------");
            repository.findAllByStatusAndCampTypeAndCampStart_dateBetween(1,2,Date,Date-2).forEach(on -> {
                log.info(on.toString());
            });
        };
    }

Bulk_repositoryRepository类:

    import org.springframework.data.repository.CrudRepository;

    public interface Bulk_repositoryRepository extends CrudRepository<Bulk_repository, Long> {
          List<Bulk_repository>  findAllByStatusAndCampTypeAndCampStart_dateBetween(int status, int campType,Date campStart_dateStart, Date campStart_dateEnd);
         Bulk_repository findById(long id);
    } 

批量存储库:

@Entity
@Table(name = "BULK_REPOSITORY")
public class Bulk_repository {

   @Id
   @GeneratedValue(strategy=GenerationType.AUTO)
   @Column(name = "id")
   private long id;

   @Column(name = "msisdn")
   private String msisdn;

   @Column(name = "camp_start_date")   
   private Date campStartDate;

   @Column(name = "camp_end_date")
   private Date campEndDate;

   @Column(name = "camp_type")
   private int campType;

   @Column(name = "camp_cd")
   private String camp_cd;

   @Column(name = "status")
   private int status;

   @Column(name = "process_date")
   private Date processDate;

   @Column(name = "entry_date")
   private Date entryDate;

   @Column(name = "entry_user")
   private String entry_user;

   @Column(name = "param1")
   private String param1;

   @Column(name = "param2")
   private String param2;

   @Column(name = "param3")
   private String param3;

   @Column(name = "param4")
   private String param4;

   @Column(name = "param5")
   private String param5;

   @Column(name = "error_desc")
   private String error_desc;

   @Column(name = "fulfilment_status")
   private int fulfilment_status;
## getter and setter and ToString
spring hibernate spring-boot jpa
1个回答
2
投票

使用这种方式,

通过使用下划线转义_

List<Bulk_repository>  findAllByStatusAndCamp__typeAndCamp__start__dateBetween(int status, int camp_type,Date camp_start_dateStart, Date camp_start_dateEnd);

我建议您在模型类中使用这种方式。如果是这样,您必须根据此更改存储库。

@Column(name = "camp_start_date")   
private Date campStartDate; // better to use this way
© www.soinside.com 2019 - 2024. All rights reserved.