一、概述

  官网:https://projects.spring.io/spring-data-jpa/

  1.什么是spring-data-jpa

  Spring Data JPA, part of the larger Spring Data family, makes it easy to easily implement JPA based repositories. This module deals with enhanced support for JPA based data access layers. It makes it easier to build Spring-powered applications that use data access technologies.

  Spring Data JPA是Spring Data系列的一部分,可以轻松实现基于JPA的存储库。该模块处理对基于JPA的数据访问层的增强支持。这使得构建使用数据访问技术的Sprin应用程序变得更加容易。

  2.spring-data-jpa有什么

  

  继承关系图:

  

二、起步

    基本的HelloWorld完整代码在springboot快速入门中已有介绍,参考这里

    不过由于当时没有引入数据库连接池,这里对它进行改进,使用阿里巴巴druid作为数据源,添加监控

    阿里Druidhttps://github.com/alibaba/druid

    整合参考:https://www.jianshu.com/p/e84e2709f383

    1.引入依赖

<!-- alibaba-druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.6</version>
</dependency>

  // 其他依赖请参见前文

  2.在application.yml配置文件中配置数据源

    官方参考配置:点击查看

    各配置项解析https://www.cnblogs.com/lmaplet/p/6170105.html

spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/sakila
username: root
password: root
# druid配置项,默认spring-boot不支持,故需要config类来解析
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 20
filters: stat,wall,log4j
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
jpa:
hibernate:
ddl-auto: update
show-sql: true

  3.添加Druid配置类

package com.example.demo.dao;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary; import javax.sql.DataSource;
import java.sql.SQLException; /**
* 阿里Druid配置类
*
* @author zcc ON 2018/2/11
**/
@Configuration
public class DruidConfig {
private static final Logger log = LoggerFactory.getLogger(DruidConfig.class); @Value("${spring.datasource.url}")
private String dbUrl; @Value("${spring.datasource.username}")
private String username; @Value("${spring.datasource.password}")
private String password; @Value("${spring.datasource.driverClassName}")
private String driverClassName; @Value("${spring.datasource.initialSize}")
private int initialSize; @Value("${spring.datasource.minIdle}")
private int minIdle; @Value("${spring.datasource.maxActive}")
private int maxActive; @Value("${spring.datasource.maxWait}")
private int maxWait; @Value("${spring.datasource.timeBetweenEvictionRunsMillis}")
private int timeBetweenEvictionRunsMillis; @Value("${spring.datasource.minEvictableIdleTimeMillis}")
private int minEvictableIdleTimeMillis; @Value("${spring.datasource.validationQuery}")
private String validationQuery; @Value("${spring.datasource.testWhileIdle}")
private boolean testWhileIdle; @Value("${spring.datasource.testOnBorrow}")
private boolean testOnBorrow; @Value("${spring.datasource.testOnReturn}")
private boolean testOnReturn; @Value("${spring.datasource.poolPreparedStatements}")
private boolean poolPreparedStatements; @Value("${spring.datasource.maxPoolPreparedStatementPerConnectionSize}")
private int maxPoolPreparedStatementPerConnectionSize; @Value("${spring.datasource.filters}")
private String filters; @Value("{spring.datasource.connectionProperties}")
private String connectionProperties; @Bean
@Primary
public DataSource dataSource() {
DruidDataSource datasource = new DruidDataSource();
log.info("----------- druid datasource ----------");
datasource.setUrl(this.dbUrl);
datasource.setUsername(username);
datasource.setPassword(password);
datasource.setDriverClassName(driverClassName); datasource.setInitialSize(initialSize);
datasource.setMinIdle(minIdle);
datasource.setMaxActive(maxActive);
datasource.setMaxWait(maxWait);
datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
datasource.setValidationQuery(validationQuery);
datasource.setTestWhileIdle(testWhileIdle);
datasource.setTestOnBorrow(testOnBorrow);
datasource.setTestOnReturn(testOnReturn);
datasource.setPoolPreparedStatements(poolPreparedStatements);
datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
try {
datasource.setFilters(filters);
} catch (SQLException e) {
e.printStackTrace();
}
datasource.setConnectionProperties(connectionProperties); return datasource;
} @Bean
public ServletRegistrationBean registrationBean() {
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet()); // 添加初始化参数:initParams
servletRegistrationBean.addUrlMappings("/druid/*");
// 白名单
servletRegistrationBean.addInitParameter("allow", "127.0.0.1");
// 黑名单
servletRegistrationBean.addInitParameter("deny", "192.168.0.101");
// 登录查看信息的账号密码.
servletRegistrationBean.addInitParameter("loginUsername", "admin");
servletRegistrationBean.addInitParameter("loginPassword", "123456");
// 可重置数据.
servletRegistrationBean.addInitParameter("resetEnable", "false");
return servletRegistrationBean;
} @Bean
public FilterRegistrationBean filterRegistrationBean() {
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());
// 添加过滤规则.
filterRegistrationBean.addUrlPatterns("/*");
// 添加不需要忽略的格式信息.
filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
return filterRegistrationBean;
}
}

  其中,@Configuration注解该类,等价 与XML中配置beans;用@Bean标注方法等价于XML中配置bean

       @Primary  //在同样的DataSource中,首先使用被标注的DataSource

  4.配置QueryDsl

    后面介绍JPA进行补充!

  5.测试

    正常调试项目接口(使用IDEA自带插件或者Postman)

    查看监控页面(监控页面由Druid生成)

      访问:127.0.0.1:8080/druid/login.html

      

    其中,IP白名单(为空则为所有)与监控登录信息在Druid配置中!

spring-data-jpa快速入门(一)——整合阿里Druid的更多相关文章

  1. Spring Data JPA —— 快速入门

    一.概述 JPA : Java Persistence API, Java持久层API,是JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中. Spring D ...

  2. Spring data JPA 快速入门

    1需求 ​ 向客户中插入一条数据 ​ 如果使用Jpa框架可以不用先建表 可以使用框架生成表 ​ 2 实现步骤 ​ a 创建工程 使用maven管理工程 <properties>       ...

  3. Spring Data Redis —— 快速入门

    环境要求:Redis 2.6及以上,javase 8.0及以上: 一.Spring Data Redis 介绍 Spring-data-redis是spring的一部分,提供了在srping应用中通过 ...

  4. Spring Data Solr —— 快速入门

    Solr是基于Lucene(全文检索引擎)开发,它是一个独立系统,运行在Tomcat或Jetty(solr6以上集成了jetty,无需再部署到servlet容器上),但其原生中文的分词词功能不行,需要 ...

  5. Spring Data JPA -1-CRUD入门

    1) 引入jar包支持 <dependency> <groupId>org.springframework.boot</groupId> <artifactI ...

  6. 整合Spring Data JPA与Spring MVC: 分页和排序

    之前我们学习了如何使用Jpa访问关系型数据库.比较完整Spring MVC和JPA教程请见Spring Data JPA实战入门,Spring MVC实战入门. 通过Jpa大大简化了我们对数据库的开发 ...

  7. 整合Spring Data JPA与Spring MVC: 分页和排序pageable

    https://www.tianmaying.com/tutorial/spring-jpa-page-sort Spring Data Jpa对于分页以及排序的查询也有着完美的支持,接下来,我们来学 ...

  8. 【Spring Data 系列学习】了解 Spring Data JPA 、 Jpa 和 Hibernate

    在开始学习 Spring Data JPA 之前,首先讨论下 Spring Data Jpa.JPA 和 Hibernate 之前的关系. JPA JPA 是 Java Persistence API ...

  9. 【Spring Data 系列学习】Spring Data JPA 自定义查询,分页,排序,条件查询

    Spring Boot Jpa 默认提供 CURD 的方法等方法,在日常中往往时无法满足我们业务的要求,本章节通过自定义简单查询案例进行讲解. 快速上手 项目中的pom.xml.application ...

  10. Spring Boot从入门到精通(九)整合Spring Data JPA应用框架

    JPA是什么? JPA全称Java Persistence API,是Sun官方提出的Java持久化规范.是JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中. ...

随机推荐

  1. 团队项目个人进展——Day04

    一.昨天工作总结 冲刺第四天,昨天继续忙着整理数据结构与算法的知识,在项目上看了看有关视频,学习了有关视图的地方和文档说明 二.遇到的问题 无 三.今日工作规划 学习并实现地图的放大与缩小

  2. mysql使用笔记(网易Mysql实用手册)---1

    1帮助使用 1.1按层次查看帮助 1 当不知道帮助可提供什么时,可通过MySQL内置帮助文档,一层层往下看. 命令: mysql> ? contents ? 等效help,该文档涵盖了数据库操作 ...

  3. mac os maverick下安装nginx+php-fpm via homebrew

    自己虽然平时爱折腾,却很少有记下来的习惯,除非碰到特别多问题的部署才会有冲动.今天看同事折腾git,在旁边看着他mac上的evernote满满的记了好几篇,全是技术相关的记录,忽然很感慨.过去解决了很 ...

  4. Oracle EBS 查询客户报错 查询已超出 200 行。可能存在更多的行,请限制查询。

  5. 【matlab】 拉格朗日插值

    第一个函数  "lagrange1.m" 输入:X Y 与点x0 输出:插值函数对应函数值 y0 function y = lagrange1(X,Y,x0) n = length ...

  6. EntityFramework Code First便捷工具——数据迁移

    使用EntityFramework Code First开发,数据迁移是一个不得不提的技术. 在我们的开发过程中,难免需要对模型进行改进,模型改进后,会导致实体集与数据库不一致,当然我们可以通过删除数 ...

  7. September 03rd 2017 Week 36th Sunday

    What does it profit a man if he gains the whole world and loses his own soul? 失去灵魂,赢得世界又如何? It matte ...

  8. python的unittest框架中如何删除测试数据,清理环境,可以通过addCleanup函数

    def addCleanup(self, function, *args, **kwargs): """Add a function, with arguments, t ...

  9. 解决win7远程桌面连接时发生身份验证错误的方法

    远程桌面连接,是我们比较常用的一个功能了,但有时突然不能用了,以下是我遇到该问题,并解决该问题的方法.连接时报的是“发生身份验证错误,要求的函数不受支持”,解决之后细想一下,该问题好像是在我在电脑上安 ...

  10. 我对git的快速使用和理解

    收藏较好的,分享给大家 https://mp.weixin.qq.com/s/k4tU8snvssyKJ2WkvkFrZA