ITPub博客

首页 > 应用开发 > Java > Spring+JPA入门

Spring+JPA入门

Java 作者:爆seed 时间:2016-06-24 10:40:37 0 删除 编辑

主要内容

  1. 简单介绍JPA

  2. Spring Hibernate JPA整合

1.JPA简介

 Java Persistence API (JPA),为开发人员提供了一种对象/关系映射(O-R Mapping)工具,用来管理 Java 应用中的关系数据。JPA是EJB3.0的一部分,现在公认为ORM行业标准。JPA本身只是一个规范,而不是产品; 它本身不能进行持续性或其他任何东西。JPA只是一组接口,需要一个实现。JPA允许通过标准的XML格式和注解格式声明,用于定义Java类如何映射到关系数据库表的映射规则。。JPA也定义了在对数据库中的对象处理查询和事务运行时的EntityManager的API。JPA定义一个对象级查询语言,JPQL,以允许从所述数据库中的对象的查询。JPA常用的解决方案:

    EclipseLink (Eclipse)
    Hibernate (RedHat)
    Open JPA (Apache)
    DataNucleus
    Ebean (SourceForge)
    TopLink Essentials (Glassfish)
    TopLink (Oracle)
    Kodo (Oracle)
JPA 规范也不是一成不变的。从2006年的JPA1.0,到2009年JPA2.0,最后到2013年JPA2.1。版本之间的特性具体参照

https://en.wikibooks.org/wiki/Java_Persistence/What_is_JPA%3F

https://en.wikipedia.org/wiki/Java_Persistence_API


2.Spring Hibernate JPA整合

准备

1
2
3
4
5
6
表
CREATE TABLE `Employee` (
  `id` int(11) unsigned NOT NULL,
  `name` varchar(20) DEFAULT NULL,
  `role` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;


2.1项目结构

wKiom1dqpY2xFPkdAAA9hhgoCj4031.png


2.2 pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<project xmlns="http: maven.apache.org="" pom="" 4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    4.0.0
    org.springframework
    gs-relational-data-access
    0.1.0
 
    
        
            org.springframework.boot
            spring-boot-starter-data-jpa
        
        
            com.h2database
            h2
        
        
            mysql
            mysql-connector-java
        
        
            org.springframework
            spring-test
        
        
            junit
            junit
            4.12
        
    
 
    
        
            spring-releases
            Spring Releases
            https://repo.spring.io/libs-release
        
        
            org.jboss.repository.releases
            JBoss Maven Release Repository
            https://repository.jboss.org/nexus/content/repositories/releases
        
    
    
        
            spring-releases
            Spring Releases
            https://repo.spring.io/libs-release
        
    
    
        
            
                io.spring.platform
                platform-bom
                1.1.2.RELEASE
                pom
                import
            
        
    

使用了platform-bom,方便了包的依赖管理。

依赖树结构

wKioL1dqpnbibnU9AAB22QiO8wc267.png

2.3 persistence.xml

1
2
3
4
5
6
7
<persistence version="2.1"
             xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    
        org.hibernate.jpa.HibernatePersistenceProvider
    
</persistence version="2.1"<>

2.4 spring.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<beans xmlns="http: "" schema="" beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
    
        
        
        
        
    
    
        
        
        
        
        
        
            
                true
            
        
    
 
    
        
        
    
 
    
 
    
    
    <bean id="transactionmanager" class="org.springframework.orm.jpa.jpatransactionmanager"
          p:entityManagerFactory-ref="entityManagerFactory" />
    
    
 
    
    
 
    
</bean id="transactionmanager" class="org.springframework.orm.jpa.jpatransactionmanager"<>

有的对象,不是必须的。为了保持完整性,java培训机构做了保留。

可以配置entityManagerFactory的packagesToScan属性,没有配置也是可以的。

2.5 entity

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.journaldev.spring.jpa.model;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Employee {
    @Id
    private int id;
    private String name;
    private String role;
 
    public int getId() {
        return id;
    }
 
    public void setId(int id) {
        this.id = id;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public String getRole() {
        return role;
    }
 
    public void setRole(String role) {
        this.role = role;
    }
 
    @Override
    public String toString() {
        return "{ID=" + id + ",Name=" + name + ",Role=" + role + "}";
    }
}

2.6 接口

1
2
3
4
5
6
7
8
9
10
11
12
public interface EmployeeDAO {
    //Create
    public void save(Employee employee);
    //Read
    public Employee getById(int id);
    //Update
    public void update(Employee employee);
    //Delete
    public void deleteById(int id);
    //Get All
    public List getAll();
}

实现类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.journaldev.spring.jpa.dao;
 
import com.journaldev.spring.jpa.model.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
 
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.transaction.Transactional;
 
import java.util.List;
 
/**
 * Created by zhaoguoyu on 2016/6/22.
 */
@Transactional
@Repository
public class EmployeeDAOImpl implements EmployeeDAO {
    @PersistenceContext
    EntityManager em;
 
 
    @Override
    public void save(Employee employee) {
        em.persist(employee);
    }
 
    @Override
    public Employee getById(int id) {
        return em.find(Employee.class, id);
    }
 
    public void update(Employee employee) {  
        em.merge(employee);
    }
 
    @Override
    public void deleteById(int id) {
        em.remove(this.getById(id));
    }
 
    @Override
    public List getAll() {
        CriteriaBuilder builder =em.getCriteriaBuilder();
        final CriteriaQuery query = builder.createQuery(Employee.class);
        return this.em.createQuery(query).getResultList();
    }
}

2.7测试

测试类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import com.journaldev.spring.jpa.dao.EmployeeDAO;
import com.journaldev.spring.jpa.model.Employee;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
 
import java.util.Random;
 
/**
 * Created by zhaoguoyu on 2016/6/22.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring.xml")
public class SpringJPATest extends AbstractTransactionalJUnit4SpringContextTests {
 
    @Resource
    private EmployeeDAO employeeDAO;
 
    @Test
    public void testSave(){
        Employee emp = new Employee();
        int rand = new Random().nextInt(1000);
        emp.setId(rand);
        emp.setName("Pankaj");
        emp.setRole("Java Developer");
        employeeDAO.save(emp);
    }
    @Test
    public void testUpdate(){
        Employee emp = new Employee();
        int rand = new Random().nextInt(1000);
        emp.setId(rand);
        emp.setName("Pankaj");
        emp.setRole("Java Developer");
        employeeDAO.save(emp);
        emp.setName(emp.getName()+"_update");
        employeeDAO.update(emp);
    }
}

OR

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import com.journaldev.spring.jpa.dao.EmployeeDAO;
import com.journaldev.spring.jpa.model.Employee;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.Random;
 
/**
 * Created by zhaoguoyu on 2016/6/22.
 */
public class Main {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
        final EmployeeDAO employeeDAO = ctx.getBean(EmployeeDAO.class);
        Employee emp = new Employee();
        int rand = new Random().nextInt(1000);
        emp.setId(rand);
        emp.setName("Pankaj");
        emp.setRole("Java Developer");
        employeeDAO.save(emp);
 
        Employee employee = employeeDAO.getById(rand);
        employee.setName(employee.getName() +"_update");
        employeeDAO.update(employee);
    }
}


有一点需要说明。默认使用的日志是logback.号称比log4j更优秀的一款。

默认的日志配置logback.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
    
        
            
                %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
            
        
    
    
        
    
 
    
        
    
 

总结

本文主要讲解了Spring对JPA的支持,和演示了整合过程。其中JPA的内容还有很多,不可能在本文全部讲完。本系列主要是为大家提供以递进的方式,通过前后的演示对比,感觉Spring DATA API的强大。

通过JPA的整合,实现了访问数据库,完全不需要写SQL。这样的好处之一:实现了SQL的透明,可以无缝实现不同数据库厂商和数据库版本间的切换。Spring 对JPA的支持还没有结束

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29917475/viewspace-2120885/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2014-09-26

  • 博文量
    28
  • 访问量
    34439