1:新建实体类
@Data
@Entity
@Table(name = "student")
@DynamicUpdate
@DynamicInsert
public class Student {
@Id
@JsonSerialize(using = ToStringSerializer.class)
@Column(name = "id")
private Long id;
@Column(name = "name")
private String name;
@Column(name = "address")
private String address;
@Column(name = "create_time")
@JsonIgnore
private Date createTime;
@Column(name = "update_time")
@JsonIgnore
private Date updateTime;
@Version
@JsonIgnore
@Column(name = "version")
private int version;
@PreUpdate
protected void onUpdate() {
updateTime = new Date();
}
@PrePersist
protected void onCreate() {
createTime = new Date();
updateTime = new Date();
}
}
2.Jpa接口
当传递的参数address值为null时,查询的就是全部学生,当address有值时,查询的就是某一地区的所有学生信息
@Repository
public interface StudentRepository extends JpaRepository<Student, Long>, JpaSpecificationExecutor<Student> {
@Query("select student.name, student.address from Student student " +
" where (student.name = ?1 or ?1 is null) ")
List<TestVo> find(String address);
}
3:编写自定义接收查询结果的TestVo接口
public interface TestVo{
String getName();
String getAddress();
}
4:自己编写测试类调用即可