1、引入依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency>
2、application.yaml配置
spring: jpa: hibernate: ddl-auto: update show-sql: true
3、创建实体类
@Entity @Table(name = "t_user") @Getter @Setter public class JpaUser { @Id //主键id @GeneratedValue(strategy= GenerationType.IDENTITY)//主键生成策略 @Column(name="id")//数据库字段名 private Integer id; @Column(name = "name") private String name; @Column(name = "password") private String password; }
4、创建dao
public interface JapUserDao extends JpaRepository<JpaUser, Integer> { List<JpaUser> findAllById(Integer id); }
5、之后便可以在service中注入