一、JPA简介:
JPA全称Java Persistence API。JPA通过JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中。
二、这里用OpenJPA 2.0 和Spring 3.0作为例子
三、搭建Spring 和 JPA的框架步骤
1.搭建Spring3.0框架,然后Finish。
2.然后搭建JPA框架,这里选用OpenJPA2.0版本
Next
选上所链接的数据库,还有数据库驱动,这里用的是Oracle数据库。接着Finish
四、框架搭建完成后,映射实体类
1.当框架搭建完之后,会在src目录下发现一个新建的文件夹和文件
2.映射实体,选择JPA的反向工程
勾上以下选项,会自动生成接口类,实现类,和工具类。接着Next就行了,和Hibernate的差不多。
五、Spring的配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd" xmlns:tx="http://www.springframework.org/schema/tx"> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean"> <property name="persistenceUnitName" value="E276-JPA" /> </bean> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory" /> </bean> <!-- dao --> <bean id="employeeDao" class="org.e276.dao.impl.EmployeeDAO"> <property name="entityManagerFactory" ref="entityManagerFactory" /> </bean> <!-- 注解式事务的配置 --> <tx:annotation-driven transaction-manager="transactionManager" /> </beans>
七、测试类
package org.e276.test; import java.util.Date; import java.util.List; import org.e276.dao.IEmployeeDAO; import org.e276.entity.Department; import org.e276.entity.Employee; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { /** * @param args */ public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); // 注意这里的写法Spring 3.0不同 IEmployeeDAO employeeDao = context.getBean(IEmployeeDAO.class); // 查询所有的员工 List<Employee> employees = employeeDao.findAll(); for (Employee employee : employees) { System.out.println("姓名:" + employee.getName() + ",性别:" + employee.getSex()); } //添加新的员工 Employee employee = new Employee(null, new Department((short) 5), "小屁孩", true, 1438d, new Date()); employeeDao.save(employee); } }
八、又或者添加单元测试框架来测试
package org.e276.test; import java.util.Date; import java.util.List; import javax.persistence.EntityManager; import org.e276.entity.Department; import org.e276.entity.Employee; import org.e276.util.EntityManagerHelper; import org.junit.After; import org.junit.Before; import org.junit.Test; public class TestEmployee { // 得到EntityManager对象 EntityManager manager = null; @Before public void setUp() throws Exception { manager = EntityManagerHelper.getEntityManager(); // 开始事务 EntityManagerHelper.beginTransaction(); } @After public void tearDown() throws Exception { // 提交事务 EntityManagerHelper.commit(); EntityManagerHelper.closeEntityManager(); } public void saveEmployee() { Department department = new Department((short) 2); // 因为主键是通过序列产生,所以一定要设置成null Employee employee = new Employee(null, department, "李培翔", true, 4500d, new Date()); manager.persist(employee); System.out.println(employee.getName() + "添加成功!"); } public void findEmployee() { Employee employee = manager.find(Employee.class, 1004); System.out.println("找到一只小伙伴:" + employee.getName()); } public void deleteEmployee() { Employee employee = manager.find(Employee.class, 1005); manager.remove(employee); System.out.println("含泪忍痛删掉一小伙伴,他名字是:" + employee.getName()); } public void updateEmployee() { Employee employee = manager.find(Employee.class, 1); employee.setSalary(15000d); manager.merge(employee); } /** * 查询某部门的员工人数 */ public void findDepartmentCount() { // JPA-QL不支持*号 Long count = (Long) manager .createQuery("select count(e) from Employee e where e.department.name=:name") .setParameter("name", "生产部").getSingleResult(); System.out.println("生产部的人数是:" + count); } /** * 查询某个部门的所有员工 */ @Test @SuppressWarnings("unchecked") public void findEmployeeByDepartment() { List<Employee> list = manager .createQuery("select e from Employee e where e.department.name=:name") .setParameter("name", "生产部").getResultList(); for (Employee employee : list) { System.out.println(employee.getName() + "工资:" + employee.getSalary()); } } }
九、demo