前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring Boot集成Spring Data Jpa

Spring Boot集成Spring Data Jpa

作者头像
用户2146693
发布2019-08-08 11:38:50
2K0
发布2019-08-08 11:38:50
举报
文章被收录于专栏:架构师进阶架构师进阶

前言

之前写过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文件中直接配置就好。

代码语言:javascript
复制
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

配置数据库

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

代码语言:javascript
复制
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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • 目录结构
  • 配置jar包
  • 配置数据库
  • 测试
  • 参考
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档