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 }
若是實體類裏面屬性名開頭用大寫字母,app
在JPA中就不能經過查詢的方法名和參數名來自動構造一個JPA OQL查詢,less
如18行的方法不能經過編譯,fetch
控制檯會提示找不到該屬性名,編碼
千萬告誡本身編碼必定要規範。spa