1. @Entity注解实体类,User代表数据库中的一张表,id,name,age代表表中的字段,表中每行的新数据代表实体类创建的对象。
@Id 代表主键,@Column代表字段
@Entity
@XmlRootElement // mediatype 转为xml
public class User implements Serializable{
private static final long serialVersionUID = 1L;
@Id // 主键
@GeneratedValue(strategy = GenerationType.IDENTITY) // 自增长策略
private long id; // 用户的唯一标识
@Column(nullable = false) // 映射为字段,值不能为空
private String name;
@Column(nullable = false)
private int age;
2. 继承jpa的资源库,不需要自己写具体实现类,即可实现对数据库基本的增删改查。
(自带操作数据库的各种方法,例如:userRepository.findAll(),userRepository.save(user) 等)
package com.waylau.spring.boot.jpa.repository;
public interface UserRepository extends CrudRepository<User, Long> {
}