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&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(); } }