1 //课程表 2 @Entity 3 public class Class { 4 @GeneratedValue(strategy = GenerationType.AUTO) 5 @Id 6 private Long classID;// 课程编号 varchar(20) not null, 7 private String className;// 课程名称 varchar(50), 8 @Temporal(TemporalType.DATE)//(精确到年月日) 9 private Date beginTime;// 开始时间 date, 10 @Temporal(TemporalType.DATE)//(精确到年月日) 11 private Date endTime;//结束时间 date, 12 private String classInfo;// 课程简介 text, 13 private double price;// 价格 numeric, 14 private int times;// 课时数 integer, 15 private int classMan;// 课程人数 int, 16 17 @ManyToOne(cascade = CascadeType.ALL) 18 private Teacher teacher;//主教练 varchar(20), 19 20 21 @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "class_") 22 private List<Lesson> lessons; 23 //...省略getter和setter方法
1 public interface ClassRepository extends JpaRepository<Class,Long> { 2 3 @Query(value = "SELECT * FROM class WHERE classid=:ID",nativeQuery = true) 4 Class findClassByID(@Param("ID")Long ID); 5 6 @Modifying 7 @Transactional 8 @Query(value = "UPDATE class SET times=:times WHERE classid=:classid",nativeQuery = true) 9 void updateClassTimes(@Param("times")int times,@Param("classid") Long classid); 10 11 12 @Query(value = " select * FROM class where DATE_FORMAT(begin_time,‘%Y%m%d‘) >=NOW()",nativeQuery = true) 13 List<Class> findClassByAfter(); 14 15 @Query(value = " select * FROM class where DATE_FORMAT(begin_time,‘%Y%m%d‘) <NOW()",nativeQuery = true) 16 List<Class> findClassByBefore(); 17 18 Page<Class> findByBeginTimeBefore(Date date,Pageable pageable); 19 20 }
如果实体类里面属性名开头用大写字母,
在JPA中就不能通过查询的方法名和参数名来自动构造一个JPA OQL查询,
如18行的方法不能通过编译,
控制台会提示找不到该属性名,
千万告诫自己编码一定要规范。
原文地址:https://www.cnblogs.com/zhu573514187/p/9418691.html
时间: 2024-10-21 01:54:30