- 依赖
<!-- jpa --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!-- H2 --> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency>
- 配置
# datasource spring.datasource.url=jdbc:h2:file:../db/meet_data spring.datasource.driver-class-name=org.h2.Driver spring.datasource.username=root spring.datasource.password=root1234 spring.jpa.database=h2 spring.jpa.hibernate.ddl-auto=update spring.jpa.show-sql=true spring.h2.console.path=/h2-console spring.h2.console.enabled=true
- 数据表 实体类
@Entity @Data @AllArgsConstructor @NoArgsConstructor public class BoardEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String userIds; private String title; private String content; private Date createTime; }
- jpa接口
@Repository public interface BoardRepository extends JpaRepository<BoardEntity, Long> { }
- 使用
@Autowired private BoardRepository boardRepository;