spring - 带有条件列表的 jpa findby
问题描述
我仍然是java的初学者,在这里我尝试使用JPA从mysql数据库中获取数据。
但是我在使用 find by function 设置条件列表时遇到了问题。即使它拒绝以下输入,我也可以设置日期对象,这是我的参考代码。
我的查询:
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
解决方案
使用这种方式,
_
使用附加下划线转义
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
推荐阅读
- angular - 无响应:无法 dockerize 和运行 Angular 应用程序
- python - 计算列中gps坐标之间的距离
- python - 使用 pdf2image 将 PDF 转换为图像不起作用
- gradle - 如何将子项目生成的类添加到 Gradle 中 WAR 文件的 WEB-INF/classes 目录
- android - 长度过滤器不适用于 textview ?- 安卓
- typescript - api 数据验证的最佳实践
- python - 在 Python Clicker 游戏中每秒添加硬币
- amazon-web-services - 使用 AWS cli 所需的 Amazon EMR 集群详细信息
- python - 在元数据之后检测 CSV 的开头
- reactjs - 从子组件访问 NextJs Link href 属性