概念:JPA是Java Persistence API的简称,中文名Java持久层API,是JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中。
JPA与Spring-data-jpa的区别:转载https://my.oschina.net/u/3080373/blog/1828589
JPA为我们提供了:
1)ORM映射元数据:JPA支持XML和注解两种元数据的形式,元数据描述对象和表之间的映射关系,框架据此将实体对象持久化到数据库表中;
如:@Entity、@Table、@Column、@Transient等注解。
2)JPA 的API:用来操作实体对象,执行CRUD操作,框架在后台替我们完成所有的事情,开发者从繁琐的JDBC和SQL代码中解脱出来。
如:entityManager.merge(T t);
3)JPQL查询语言:通过面向对象而非面向数据库的查询语言查询数据,避免程序的SQL语句紧密耦合。
如:from Student s where s.name = ?
但是:
JPA仅仅是一种规范,也就是说JPA仅仅定义了一些接口,而接口是需要实现才能工作的。所以底层需要某种实现,而Hibernate就是实现了JPA接口的ORM框架。
也就是说:
JPA是一套ORM规范,Hibernate实现了JPA规范!如图:
什么是spring data jpa?
spirng data jpa是spring提供的一套简化JPA开发的框架,按照约定好的【方法命名规则】写dao层接口,就可以在不写接口实现的情况下,实现对数据库的访问和操作。同时提供了很多除了CRUD之外的功能,如分页、排序、复杂查询等等。
Spring Data JPA 可以理解为 JPA 规范的再次封装抽象,底层还是使用了 Hibernate 的 JPA 技术实现。如图:
接口约定命名规则:
实例:
springboot集成spring data jpa只需两步:
第一步:导入maven坐标
第二步:yml配置文件中配置jpa信息
自解:JPA是一种规范,其有不同的实现。hibernate便是一种实现,spring-data-jpa也是一种实现,不过spring-data-jpa的封装程度更高些。本篇的练习就是学习hibernate的JPA实现和使用,下面开始做demo测试。
项目结构:
pojo层:User
package com.cy.pojo;
import org.hibernate.annotations.GenericGenerator;
import javax.persistence.*;
import java.util.Date;
@Entity
@Table(name = User.TABLE)
public class User {
public static final String TABLE = "SYS_USERS";
@Id
@GenericGenerator(name = "idGenerator", strategy = "native")
@GeneratedValue(generator = "idGenerator")
@Column
private Integer id;
@Column
private String username;
@Column
private String password;
@Column
private String salt;
@Column
private String email;
@Column
private String mobile;
@Column
private Integer valid;
@Column
private Integer deptId;
@Column
private Date createdTime;
@Column
private Date modifiedTime;
@Column
private String createdUser;
@Column
private String modifiedUser;
public User() {
}
public User(Integer id, String username, String password, String salt, String email, String mobile, Integer valid, Integer deptId, Date createdTime, Date modifiedTime, String createdUser, String modifiedUser) {
this.username = username;
this.password = password;
this.salt = salt;
this.email = email;
this.mobile = mobile;
this.valid = valid;
this.deptId = deptId;
this.createdTime = createdTime;
this.modifiedTime = modifiedTime;
this.createdUser = createdUser;
this.modifiedUser = modifiedUser;
}
public static String getTABLE() {
return TABLE;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getSalt() {
return salt;
}
public void setSalt(String salt) {
this.salt = salt;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public Integer getValid() {
return valid;
}
public void setValid(Integer valid) {
this.valid = valid;
}
public Integer getDeptId() {
return deptId;
}
public void setDeptId(Integer deptId) {
this.deptId = deptId;
}
public Date getCreatedTime() {
return createdTime;
}
public void setCreatedTime(Date createdTime) {
this.createdTime = createdTime;
}
public Date getModifiedTime() {
return modifiedTime;
}
public void setModifiedTime(Date modifiedTime) {
this.modifiedTime = modifiedTime;
}
public String getCreatedUser() {
return createdUser;
}
public void setCreatedUser(String createdUser) {
this.createdUser = createdUser;
}
public String getModifiedUser() {
return modifiedUser;
}
public void setModifiedUser(String modifiedUser) {
this.modifiedUser = modifiedUser;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", salt='" + salt + '\'' +
", email='" + email + '\'' +
", mobile='" + mobile + '\'' +
", valid=" + valid +
", deptId=" + deptId +
", createdTime=" + createdTime +
", modifiedTime=" + modifiedTime +
", createdUser='" + createdUser + '\'' +
", modifiedUser='" + modifiedUser + '\'' +
'}';
}
}
配置文件:persistence.xml
org.hibernate.jpa.HibernatePersistenceProvider
测试实现:
package com.cy.connDB;
import com.cy.pojo.User;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import java.util.Date;
public class JpaConn {
private static EntityManagerFactory entityManagerFactory;
public static void main(String[] args) {
/**
* JPA连接数据库,注意问题persistence.xml的配置文件必须放在META-INF文件夹下:
* 1.Persistence创建entityFactory对象
* 2.通过entityManagerFactory对象创建entityManager对象
* 3.entityManager获取事务对象
* 4.开启事务
* 5.操作数据库
* 6.关闭资源
*/
entityManagerFactory = Persistence.createEntityManagerFactory("myJpa");
JpaConn jpaConn = new JpaConn();
jpaConn.saveUser();
jpaConn.findUserById();
jpaConn.updateUserByUsername();
jpaConn.deleteUserById();
}
private void deleteUserById() {
EntityManager entityManager = entityManagerFactory.createEntityManager();
EntityTransaction tx = entityManager.getTransaction();
tx.begin();
User user = entityManager.find(User.class, 18);
entityManager.remove(user);
tx.commit();
entityManager.close();
}
private void updateUserByUsername() {
EntityManager entityManager = entityManagerFactory.createEntityManager();
EntityTransaction tx = entityManager.getTransaction();
tx.begin();
User user = entityManager.find(User.class, 12);
user.setUsername("zhouyu");
entityManager.merge(user);
tx.commit();
entityManager.close();
}
private void findUserById() {
EntityManager entityManager = entityManagerFactory.createEntityManager();
EntityTransaction transaction = entityManager.getTransaction();
transaction.begin();
User user = entityManager.find(User.class, 12);
System.out.println(user);
}
private void saveUser() {
EntityManager entityManager = entityManagerFactory.createEntityManager();
EntityTransaction transaction = entityManager.getTransaction();
transaction.begin();
User user = new User();
user.setUsername("xiaoqiao");
user.setEmail("[email protected]");
user.setMobile("18755556666");
user.setCreatedTime(new Date());
user.setModifiedTime(new Date());
user.setCreatedUser("admin");
user.setModifiedUser("admin");
entityManager.persist(user);
transaction.commit();
entityManager.close();
}
}
总结:使用JPA时需要特别注意,persistence.xml配置文件的位置,必须放在src/META-INF下面才可以!!!
使用JPA连接数据库步骤:
1.通过Persistence读取配置文件,创建entityManagerFactory对象
2.通过entityManagerFactory对象创建entityManager对象
3.entityManager获取事务对象
4.开启事务
5.使用entityManeger操作数据库
6.关闭资源
entityManager常用操作数据库的方法: void persist(Object var1);保存对象T merge(T var1);将 Detached状态的Entity实例转至Managed状态; void remove(Object var1);删除数据 T find(Class var1, Object var2);根据主键进行查询 Query createQuery(CriteriaUpdate var1); eg:用法实例
EntityManager em = …;
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery cq = cb.createQuery(Entity.class);
Root from = cq.from(Entity.class);
cq.select(Entity);
//必须要有CriteriaQuery对象,才能用此方法
TypedQuery q = em.createQuery(cq);
List allitems = q.getResultList();
Query createNamedQuery(String var1);可以通过sql语句查询数据库 CriteriaBuilder getCriteriaBuilder();创建criteria查询数据库