当前位置:首页 > 开发 > 系统架构 > 架构 > 正文

JPA入门(一)

发表于: 2013-12-23   作者:antlove   来源:转载   浏览次数:
摘要: 1.POJO  Employee.java package pojo; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="T_EMPLOYEE") public class

1.POJO 

Employee.java

package pojo;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="T_EMPLOYEE")
public class Employee {
    @Id
    private String id;
    private String name;
    private double salary;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

	@Override
	public String toString() {
		return "Employee [id=" + id + ", name=" + name + ", salary=" + salary
				+ "]";
	}

}

 

2.Dao 

EmployeeDao.java

package dao;
import javax.persistence.EntityManager;

import pojo.Employee;

public class EmployeeDao {
    private EntityManager em=null;
    
	public EmployeeDao(EntityManager em) {
		this.em = em;
	}

	public Employee createEmployee(String id,String name,double salary){
        Employee emp = new Employee();
        emp.setId(id);
        emp.setName(name);
        emp.setSalary(salary);
        em.persist(emp);
        return emp;
    }
    
    public void removeEmployee(String id){
        Employee emp = findEmployee(id);
        if(emp==null){
            throw new RuntimeException("The employee whose id is "+ id+" is not existed");
        }
        em.remove(emp);                        
    }
    
    public Employee findEmployee(String id){
        return em.find(Employee.class,id);
    }
    
    public Employee updateEmployee(String id,String name,double salary){
        Employee emp=findEmployee(id);
        if(emp==null){
            throw new RuntimeException("The employee whose id is "+ id+" is not existed");
        }
        emp.setName(name);
        emp.setSalary(salary);
        return emp;
    }
    
}

 

3.Configuration file

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
	xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/persistence 
               http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
	<persistence-unit name="emp" transaction-type="RESOURCE_LOCAL">
		<provider>org.hibernate.ejb.HibernatePersistence</provider>
		<properties>
			<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
			<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=UTF-8" />
			<property name="javax.persistence.jdbc.user" value="root" />
			<property name="javax.persistence.jdbc.password" value="root" />
		</properties>
	</persistence-unit>
</persistence>

 

4. Test class

Main.java

package test;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import pojo.Employee;
import dao.EmployeeDao;
public class Main {

	public static void main(String[] args) {
	    EntityManagerFactory emf=Persistence.createEntityManagerFactory("emp");
	    EntityManager em=emf.createEntityManager();
	    
	    EmployeeDao employeeDao = new EmployeeDao(em);
	    
	    // create employee 
	    em.getTransaction().begin();
	    employeeDao.createEmployee("1", "xiao", 100);
	    em.getTransaction().commit();
	    
	    // update employee
	    em.getTransaction().begin();
	    employeeDao.updateEmployee("1", "xiao", 1000);
	    em.getTransaction().commit();
	    
	    // select employee
	    Employee e=employeeDao.findEmployee("1");
	    System.out.println(e);
	    
	    // remove employee
	    em.getTransaction().begin();
	    employeeDao.removeEmployee("1");
	    em.getTransaction().commit();
	}
}

 

 

JPA入门(一)

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
JPA全称java Persistence API.是java对象持久化的API.是javaEE5.0平台的标准ORM规范,使得应用程序以
原文链接: http://sishuok.com/forum/blogPost/list/7000.html Spring Data是什么 Spring Data是一
spring data 是一个用于建行数据库访问,并支持云服务的开源框架,其主要目标是使得对数据库的访问变
(1)、JPA介绍: JPA全称为Java Persistence API ,Java持久化API是Sun公司在Java EE 5规范中提出
写实体bean,映射的数据可以采用XML配置方式,也可以采用注解方式,在JPA中推荐大家用注解的方式,
1 1.1 eclipse 3.6.2 1.2 hibernate3.6.1 http://sourceforge.net/projects/hibernate/files/hibern
spring-data-jpa 入门资料 maven环境安装 <dependency> <groupId>org.springframework.
基础测试: 第一步:配置依赖: <dependency> <groupId>junit</groupId> <artifa
1.JPA概述 JPA全称Java Persistence API. JPA通过JDK 5.0注解或XML描述对象-关系表的映射关系,并
Order.java package cn.itcast.bean; import java.util.HashSet; import java.util.Set; import jav
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号