springboot使用内置h2 数据库,数据保存本地文件夹防止丢失

  1. 依赖
 <!-- 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>
  1. 配置
#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
  1. 数据表 实体类
@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;
}
  1. jpa接口
@Repository
public interface  BoardRepository  extends JpaRepository<BoardEntity, Long> {
}
  1. 使用
   @Autowired
    private BoardRepository boardRepository;
posted @ 2020-11-26 18:12  qwer78  阅读(406)  评论(0)    收藏  举报