Spring Boot (6) Spring Data JPA
JPA
全称Java Persistence API,JPA通过JDK 5.0注解或xml描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中
JPA是sun官方提出的java持久化规范,它为java开发人员提供了一种对象/关系映射工具来管理java应用中的关系数据。
持久化:把数据(内存中的对象)保存到可永久保存的存储设备中(如磁盘)。持久化的主要作用是将内存中的对象存储在数据库中,或磁盘文件中,xml文件中等。jdbc,io都是持久化机制。
规范:所谓的规范就是明文规定或约定俗成的标准,如道德规范,技术规范等。
持久化规范就是sun针对持久化这一层操作指定的规范。
Hibernate
hibernate是一个开源的对象关系映射框架,它对jdbc进行了封装,将pojo与数据库表建立映射关系,是一个全自动的orm框架,hibernate可以自动生成sql语句,自动执行,使java程序员可以像操作对象一样操作数据库。
Spring Data
spring data 是一个用于简化数据库访问,并支持云服务的开源框架,主要目标是使数据库的访问变的方便快捷,并支持map-reduce框架和云计算数据服务,此外,他还支持基于关系型数据库的数据服务,如Oracle RAC等。对于拥有海量数据的项目,可以使用Spring Data 来简化项目的开发,就如spring framework 对jdbc 、orm的支持一样,spring data会让数据的访问变的更加方便。
Spring Data JPA
可以极大简化JPA的写法,可以在几乎不用写实现的情况下,实现对数据的访问和操作。除了增删改查外,还包括分页、排序等一些常用功能。
spring data 是一个开源框架,在这个框架中spring data jpa 只是这个框架中的一个模块,所以名称才叫 spring data jpa。如果单独使用jpa开发,你会发现这个代码量和jdbc开发一样有点烦人,所以spring data jpa的出现就是为了简化jpa的写法,让你只需要编写一个接口继承一个类就可以实现增删改查操作了。
JPA/Hibernate关系
JPA是一种规范,而hibernate是它的一种实现。所以使用JPA的一个好处是,可以更换实现而不必改动太多代码。
使用Spring Data JPA进行数据库持久化操作:
首先在pom.xml中添加依赖
<!--mysql数据驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--spring data jpa -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
在application.properties中配置连接
# mysql连接
spring.datasource.url=jdbc:mysql://localhost:3306/david2018_db?characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=1234
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.max-active=20
spring.datasource.max-idle=8
spring.datasource.min-idle=8
spring.datasource.initial-size=10 # jpa配置
spring.jpa.database = MYSQL
# 显示sql语句
spring.jpa.show-sql = true
# Hibernate 语句策略 (create, create-drop, update)
spring.jpa.hibernate.ddl-auto = update
# 创建表结构
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
编写实体类
package com.david.bean; import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id; //当JPA检测到我们的实体类当中有@Entity时,会在数据库中生成对应的表结构信息
@Entity
public class Cat {
//使用@Id指定主键 @GeneratedValue指定主键的生成策略(mysql中自增)
@Id @GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String catName;
private Integer catAge; public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getCatName() {
return catName;
} public void setCatName(String catName) {
this.catName = catName;
} public Integer getCatAge() {
return catAge;
} public void setCatAge(Integer catAge) {
this.catAge = catAge;
}
}
运行启动类,会自动在数据库中生成cat表,根据驼峰命名catName 会转换为cat_name
编写CatRepository.java 接口
package com.david.repository; import com.david.bean.Cat;
import org.springframework.data.repository.CrudRepository; //继承CrudRepository类 第二个参数id的类型 即可继承常用方法save update findAll等
public interface CatRepository extends CrudRepository<Cat,Integer> { }
编写CatService.java
package com.david.service; import com.david.bean.Cat;
import com.david.repository.CatRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; @Service
public class CatService {
@Resource
private CatRepository catRepository; @Transactional
public void save(Cat c){
catRepository.save(c);
} @Transactional
public void delete(int id){
catRepository.delete(id);
} public Iterable<Cat> getAll(){
return catRepository.findAll();
}
}
编写CatController.java
package com.david.controller; import com.david.bean.Cat;
import com.david.service.CatService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; @RestController
@RequestMapping("/cat")
public class CatController { @Resource
private CatService catService; @RequestMapping("/save")
public String save(){
Cat cat = new Cat();
cat.setCatName("tom");
cat.setCatAge(3);
catService.save(cat);
return "save";
} @RequestMapping("/delete")
public String delete(){
catService.delete(1);
return "delete";
} @RequestMapping("/getall")
public Iterable<Cat> getAll(){
return catService.getAll();
}
}
完整框架
浏览器访问 localhost:8088/cat/save delete getall方法进行测试
Repository
我们的CatRepository.java接口继承了CrudRepository接口,CrudRepository接口又继承了Repository接口,Repository是一个空接口,如果我们定义的接口继承了Repository,则该接口会被IOC容器识别为一个Repository Bean纳入到IOC容器中,今儿可以在该接口中定义满足一定规范的方法,实际上也可以通过@RepositoryDefinition注解来替代继承Repository接口。
查询方法以find | read | get开头,设计查询条件时,条件的属性用条件关键字连接,要注意的是条件属性以首字母大写,使用@Query注解可以自定义JPQL语句实现更灵活的查询。
CrudRepository
该接口提供了最基本的对实体类的增删改查操作
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
// package org.springframework.data.repository; import java.io.Serializable; @NoRepositoryBean
public interface CrudRepository<T, ID extends Serializable> extends Repository<T, ID> {
<S extends T> S save(S var1); //保存单个实体 <S extends T> Iterable<S> save(Iterable<S> var1); //保存集合 T findOne(ID var1); //根据id查找一个 boolean exists(ID var1); //根据id判断是否存在 Iterable<T> findAll(); //查找所有 Iterable<T> findAll(Iterable<ID> var1); //查询所有 根据条件 long count(); //查询实体数量 void delete(ID var1); //根据id删除实体 void delete(T var1); //删除一个实体 void delete(Iterable<? extends T> var1); //删除一个实体集合 void deleteAll(); //删除所有实体
}
PagingAndSortingRepository
该接口提供了分页与排序功能
Iterable<T> findAll(Sort sort); //排序
Page<T> findAll(Pageable pageable) //分页查询(包含排序)
其他接口
JpaRepository:查找所有实体、排序、查找所有实体,执行缓存与数据库同步
JpaSpecificationExecutor:不属于Repository体系,实现一组JPA Criteria 查询相关的方法,封装JPA Criteria 查询条件,通常使用匿名内部类的方式来创建该接口的对象。
自定义Repository:可以自定义一个MyRepository接口。
Spring Boot (6) Spring Data JPA的更多相关文章
- spring boot系列(五)spring boot 配置spring data jpa (查询方法)
接着上面spring boot系列(四)spring boot 配置spring data jpa 保存修改方法继续做查询的测试: 1 创建UserInfo实体类,代码和https://www.cnb ...
- Spring Boot 整合Spring Data JPA
Spring Boot整合Spring Data JPA 1)加入依赖 <dependency> <groupId>org.springframework.boot</g ...
- Spring Boot 之Spring data JPA简介
文章目录 添加依赖 添加entity bean 创建 Dao Spring Data Configuration 测试 Spring Boot 之Spring data JPA简介 JPA的全称是Ja ...
- Spring Boot 结合Spring Data结合小项目(增,删,查,模糊查询,分页,排序)
本次做的小项目是类似于,公司发布招聘信息,因此有俩个表,一个公司表,一个招聘信息表,俩个表是一对多的关系 项目整体结构: Spring Boot和Spring Data结合的资源文件 applicat ...
- Spring Boot中Spring data注解的使用
文章目录 Spring Data Annotations @Transactional @NoRepositoryBean @Param @Id @Transient @CreatedBy, @Las ...
- Spring Boot集成Spring Data Reids和Spring Session实现Session共享
首先,需要先集成Redis的支持,参考:http://www.cnblogs.com/EasonJim/p/7805665.html Spring Boot集成Spring Data Redis+Sp ...
- spring boot 集成 Mybatis,JPA
相对应MyBatis, JPA可能大家会比较陌生,它并不是一个框架,而是一组规范,其使用跟Hibernate 差不多,原理层面的东西就不多讲了,主要的是应用. Mybatis就不多说了,SSM这三个框 ...
- 一:Spring Boot、Spring Cloud
上次写了一篇文章叫Spring Cloud在国内中小型公司能用起来吗?介绍了Spring Cloud是否能在中小公司使用起来,这篇文章是它的姊妹篇.其实我们在这条路上已经走了一年多,从16年初到现在. ...
- spring boot 与 spring cloud 关系
公司使用spring cloud,所以稍微了解一下 看了一下spring官网对 spring boot 以及 spring cloud 的解释 Spring Boot Spring Boot make ...
- spring boot与spring mvc的区别是什么?
Spring 框架就像一个家族,有众多衍生产品例如 boot.security.jpa等等.但他们的基础都是Spring 的 ioc和 aop ioc 提供了依赖注入的容器 aop ,解决了面向横切面 ...
随机推荐
- BZOJ 3489: A simple rmq problem KDtree
Code: #include<bits/stdc++.h> #define maxn 200000 #define inf 100000000 #define mid ((l+r)> ...
- HTML学习笔记之标签基础
目录 1.基本标签 2.链接 3.图像 4.表格 5.列表 6.块与布局 1.基本标签 (1)标题与段落 标签 <h1> ~ <h6> 分别用于定义一至六级标题,标签 < ...
- [bzoj2502]清理雪道[上下界网络流]
bzoj状态里有两种,一种时间是个位数,一种是四位数,我就是四位数的那种,,,估计都是看了hzwer.. #include <bits/stdc++.h> #define INF 0x3f ...
- SecureCRT的设置和美化
一 . SecureCRT 7.1 的 安装 http://liufei888.blog.51cto.com/2625545/1306231 1.下载注册机SecureCRT.v.6.7. ...
- CF #329 C
C题我还以为是拉格朗日插值... 其实可以想象到,必须有这样一个函数,经过某一点时,其它圆相关的函数要为0. 于是,可以构造这样的一个函数,对于x有 (x/2)*(1-abs(t-i)+abs(1-a ...
- IntelliJ IDEA中出现could not auto wired错误提示处理方式
IntelliJ IDEA中出现could not auto wired错误提示处理方式 程序可以正常运行,就是出现错误提示: 学习了:http://blog.csdn.net/xlxxybz1314 ...
- CentOS出错You don't have permission to access on this server
检查http.conf发现没错之后.查找资料后发现时selinux的问题,所以须要关闭这个服务: 1 vi /etc/sysconfig/selinux 2 SELINUX=enforcing 改为 ...
- 【cocos2d-x 3.7 飞机大战】 决战南海I (十) 游戏主场景
主场景要包括其它类的头文件 #include "cocos2d.h" #include "MyPlane.h" #include "Bullet.h& ...
- crm使用soap启用和停用记录
function demo() { //操作记录的id var targetId = "a8a46444-ba10-e411-8a04-00155d002f02"; ...
- Ant报错之out of memory
用Ant打包一个比較大的项目的时候,遇到OutOfMemory的问题,求助于Google和百度,网上的解决方式非常多,可是个人认为不够具体全面.我的问题须要综合两种方法才解决.把方案记下来.以期帮助大 ...