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