第一步 在创建项目时,用idea自动导包
第二步 写entity
@Data
@NoArgsConstructor
@AllArgsConstructor
//标识对应的数据库名称
@Table(name = "td_student")
//代表此类为实体类
@Entity
public class Student {
//表示id为主键
@Id
Integer id;
String name;
Integer age;
}
第三步 写dao层
public interface StudentDao extends JpaRepository<Student, Integer> {}
//Student代表实体类名称
//Integer是主键的类型
第四步 测试
@Test
void contextLoads() {
//findAll()是JpaRepository自带的方法
List<Student> all = studentDao.findAll();
all.forEach(System.out::println);
}
结果