Spring Boot集成Spring Data Jpa

2017/06/13

Spring Boot 使用Jpa,很简单,也很方便,这里简单介绍一下。

前言

之前写过spring data jpa 入门,Spring Boot 使用Jpa,很简单,也很方便,这里简单介绍一下。

目录结构

spring data jpa 入门中已经介绍过jpa怎么玩,直接建立对应的类和包。

这里写图片描述

配置jar包

Spring Boot默认提供的jar是spring-boot-starter-data-jpa,它提供了以下关键依赖。

  • Hibernate — One of the most popular JPA implementations.
  • Spring Data JPA — Makes it easy to implement JPA-based repositories.
  • Spring ORMs — Core ORM support from the Spring Framework.

在你的pom文件中直接配置就好。

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

配置数据库

Spring Boot 默认提供tomcat-jdbc连接池,也可以通过spring.datasource.type属性配置其他的连接池。这里使用默认的。application.properties配置如下:

server.port=9091
# mysql
spring.datasource.url=jdbc:mysql://localhost/redis_test
spring.datasource.username=root
spring.datasource.password=admin
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
# Number of ms to wait before throwing an exception if no connection is available.
spring.datasource.tomcat.max-wait=10000
# Maximum number of active connections that can be allocated from this pool at the same time.
spring.datasource.tomcat.max-active=50
# Validate the connection before borrowing it from the pool.
spring.datasource.tomcat.test-on-borrow=true

测试

启动,测试一下。

这里写图片描述

使用postman联调一下。

这里写图片描述

优化一下时间格式。

这里写图片描述

这里写图片描述

成功了。。。

参考

29.3 JPA and ‘Spring Data’ 29.1.2 Connection to a production database


作者:Wuxinshui
出处:http://wuxinshui.github.io
版权归作者所有,转载请注明出处

Post Directory