JPA按两个日期之间的日期查找 [英] JPA find by date between two dates
本文介绍了JPA按两个日期之间的日期查找的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在这里,我试图使用JPA从MYSQL DB中获取数据,但是我在设置数据对象以处理条件时遇到了麻烦,据我所知我必须创建并使用数据对象来处理它,我该怎么办?创建日期对象以处理所需的
here I'm trying to use JPA fetch the data from MYSQL DB, but I'm facing trouble to set data object to handle the condition, as I understand I have to create and data object to handle it,how do i creat date object to handle the needed
这是我的常规查询:
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_repository类:
Bulk_repository class :
@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
Bulk_repositoryRepository类:
Bulk_repositoryRepository class :
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);
}
推荐答案
一旦您更改了方法,如我在评论中所建议的那样
Once you have changed the method, as I suggested in comments
AccessingDataJpaApplication 中的
更改方法演示
change method demo in AccessingDataJpaApplication as below
@Bean
public CommandLineRunner demo(Bulk_repositoryRepository repository) {
return (args) -> {
Date currentDate = new Date();
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -2);
Date dateTwoDaysAgo = cal.getTime();
// fetch customers by Status
log.info("Customer found with findByStatus('0'):");
log.info("--------------------------------------------");
repository. findAllByStatusAndCampTypeAndCampStartDateBetween(1,2,currentDate,dateTwoDaysAgo).forEach(on -> {
log.info(on.toString());
});
};
}
这篇关于JPA按两个日期之间的日期查找的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文