首页 > 应用开发 > Java > Spring+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;
|
|
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
|
|
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
|
|
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" />
|
|
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 + "}";
}
}
|
|
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();
}
}
|
|
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);
}
}
|
|
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);
}
}
|
|
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
|
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29917475/viewspace-2120885/ ,如需转载,请注明出处,否则将追究法律责任。