1、JPA是什么东西?
JPA就是Java Persistence API的缩写,是JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中。
2、JPA具有什么优势?
a、标准化
JPA 是 JCP 组织发布的 Java EE 标准之一,因此任何声称符合 JPA 标准的框架都遵循同样的架构,提供相同的访问API,这保证了基于JPA开发的企业应用能够经过少量的修改就能够在不同的JPA框架下运行。
b、容器级特性的支持
JPA框架中支持大数据集、事务、并发等容器级事务,这使得 JPA 超越了简单持久化框架的局限,在企业应用发挥更大的作用。
c、简单方便
JPA的主要目标之一就是提供更加简单的编程模型:在JPA框架下创建实体和创建Java 类一样简单,没有任何的约束和限制,只需要使用 javax.persistence.Entity进行注释,JPA的框架和接口也都非常简单,没有太多特别的规则和设计模式的要求,开发者可以很容易的掌握。JPA基于非侵入式原则设计,因此可以很容易的和其它框架或者容器集成。
d、查询能力
JPA的查询语言是面向对象而非面向数据库的,它以面向对象的自然语法构造查询语句,可以看成是Hibernate HQL的等价物。JPA定义了独特的JPQL(Java Persistence Query Language),JPQL是EJB QL的一种扩展,它是针对实体的一种查询语言,操作对象是实体,而不是关系数据库的表,而且能够支持批量更新和修改、JOIN、GROUP BY、HAVING 等通常只有 SQL 才能够提供的高级查询特性,甚至还能够支持子查询。
e、高级特性
JPA 中能够支持面向对象的高级特性,如类之间的继承、多态和类之间的复杂关系,这样的支持能够让开发者最大限度的使用面向对象的模型设计企业应用,而不需要自行处理这些特性在关系数据库的持久化。
3、代码实战
导入相关pom依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
在application.yml文件中进行配置:
spring:
jpa:
hibernate:
ddl-auto: update//自动更新
show-sql: true//在日志中显示SQL语句
jpa.hibernate.ddl-auto是hibernate的配置属性,其主要作用是:自动创建、更新、验证数据库表结构。该参数的几种配置如下:
·create:每次加载hibernate时都会删除上一次的生成的表,然后根据你的model类再重新来生成新表,哪怕两次没有任何改变也要这样执行,这就是导致数据库表数据丢失的一个重要原因。
·create-drop:每次加载hibernate时根据model类生成表,但是sessionFactory一关闭,表就自动删除。
·update:最常用的属性,第一次加载hibernate时根据model类会自动建立起表的结构(前提是先建立好数据库),以后加载hibernate时根据model类自动更新表结构,即使表结构改变了但表中的行仍然存在不会删除以前的行。要注意的是当部署到服务器后,表结构是不会被马上建立起来的,是要等应用第一次运行起来后才会。
·validate:每次加载hibernate时,验证创建数据库表结构,只会和数据库中的表进行比较,不会创建新表,但是会插入新值。
自动建表相关实体类:
@Entity
@Getter
@Setter
@Table(name = "t_springboot_book")
public class Book {
@Id
@GeneratedValue
private Integer bid;
@Column(length = 100)
private String bname;
@Column
private Float price;
}
数据库自动建表截图,并且会创建一个序列以及t_springboot_order表:
jpa值增删改查:
dao层:
package com.zking.dao;
import com.zking.entity.Order;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
/**
* @author LJ
* @site www.lijun.com
* @Date 2019年02月22日
* @Time 16:10
* 只要继承JpaRepository,通常所用的增删查改方法都有
* 第一个参数:操作的实体类
* 第二个参数:实体类对应数据表的主键
*/
public interface OrderDao extends JpaRepository<Order,Integer> {
}
controller层:
package com.zking.controller;
import com.zking.dao.OrderDao;
import com.zking.entity.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.List;
/**
* @author LJ
* @site www.lijun.com
* @Date 2019年02月22日
* @Time 16:12
*/
@RestController
@RequestMapping("/order")
public class OrderController {
@Autowired
private OrderDao orderDao;
@RequestMapping("/add")
public Order add(Order order){
Order save = this.orderDao.save(order);
return save;
}
@RequestMapping("/del")
public String del(Integer oid){
this.orderDao.deleteById(oid);
return "success";
}
@RequestMapping("/edit")
public Order edit(Order order){
Order save = this.orderDao.save(order);
return save;
}
@RequestMapping("/getOne")
public Order getOne(Integer oid){
// 这样子写会出现异常
// return this.orderDao.getOne(oid);
return this.orderDao.findById(oid).get();
}
@RequestMapping("/getAll")
public List<Order> getAll(){
return this.orderDao.findAll();
}
}
浏览器输入请求进行测试:
http://localhost:8080/springboot/order/getOne?oid=11
http://localhost:8080/springboot/order/getAll
http://localhost:8080/springboot/order/add?orderName=你是最好的自己&price=234
http://localhost:8080/springboot/order/add?orderName=阿弥陀佛,么么哒&price=234&oid=11
http://localhost:8080/springboot/order/del?oid=11
jpa值复杂查询:
dao层:
package com.zking.dao;
import com.zking.entity.Order;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
/**
* @author LJ
* @site www.lijun.com
* @Date 2019年02月22日
* @Time 16:10
* 要使用高级查询必须继承org.springframework.data.jpa.repository.JpaSpecificationExecutor<T>接口
*/
public interface OrderDao extends JpaRepository<Order,Integer>, JpaSpecificationExecutor<Order> {
}
controller层:
@RequestMapping("/getCondition")
public List<Order> getCondition(Order order){
return this.orderDao.findAll(new Specification<Order>() {
@Override
public Predicate toPredicate(Root<Order> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
Predicate predicate = criteriaBuilder.conjunction();
if (order != null && order.getOrderName() != null && !"".equals(order.getOrderName())){
predicate.getExpressions().add(criteriaBuilder.like(root.get("orderName"),"%"+order.getOrderName()+"%"));
}
return predicate;
}
});
}
浏览器访问一波: