1.上面的包除了persistence.xml不需要外,还有mysql-connector-java-5.1.8-bin.jar这个是根据自已的数据库驱动;
2.根据第二张图自已找;
3.类结构图(persistence.xml必须在META-INF下,好像官方是这么说的)
1.persistence.xml
<persistence 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_1_0.xsd" version="1.0"> <persistence-unit name="easier" transaction-type="RESOURCE_LOCAL" ><!-- 本地事务 --> <properties> <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/> <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/> <property name="hibernate.connection.username" value="root"/> <property name="hibernate.connection.password" value="123456"/> <property name="hibernate.connection.url" value="jdbc:mysql://127.0.0.1:3306/easier?useUnlcode=true&characterEncoding=UTF-8"/> <property name="hibernate.max_fetch_depth" value="3"/> <property name="hibernate.show_sql" value="true"/> <property name="hibernate.hbm2ddl.auto" value="update"/> <!-- hibernate.hbm2ddl.auto validate 加载hibernate时,验证创建数据库表结构 create 每次加载hibernate先删除表,重新创建数据库表结构 create-drop 加载hibernate先删除表后创建,退出是删除表结构 update 加载hibernate自动更新数据库结构 --> </properties> </persistence-unit> </persistence>
2.Person类/**
* */ package cn.easier.bean; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; /** * @author Administrator * */ @Entity public class Person { @Id @GeneratedValue private Integer id; private String name; public Person() {} /** * @param id * @param name */ public Person(String name) { this.name = name; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
3.junit测试类
/** * */ package cn.easier.test; import java.util.List; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; import org.junit.BeforeClass; import org.junit.Test; import cn.easier.bean.Person; /** * @author Administrator * */ public class PersonTest { private static EntityManagerFactory factory=null; private static EntityManager em=null; /** * @throws java.lang.Exception */ @BeforeClass public static void setUpBeforeClass() throws Exception { factory = Persistence.createEntityManagerFactory("easier"); em = factory.createEntityManager(); } @Test public void save(){ em.getTransaction().begin(); //开始事务 Person person=new Person("李四"); em.persist(person); //保存对象 em.getTransaction().commit(); em.close(); factory.close(); } @Test public void delete(){ Integer id=1; Person person = em.find(Person.class, id); if(person!=null){ em.getTransaction().begin(); //开始事务 em.remove(person); em.getTransaction().commit(); } em.close(); factory.close(); } @Test public void update(){ Integer id=2; Person person=new Person(); person.setId(id); //原来的id=2的name=李四 person = em.find(Person.class, person.getId()); person.setName("张三"); //更新名字 if(person!=null){ em.getTransaction().begin(); //开始事务 em.merge(person); em.getTransaction().commit(); } em.close(); factory.close(); } @Test public void getPerson(){ Integer id=1; Person person = em.find(Person.class, id); em.close(); factory.close(); System.out.println("id:\t"+person.getId()+"\tname:\t"+person.getName()); } @SuppressWarnings("unchecked") @Test public void getList(){ // 这不是“真正”SQL,而是类似sql的东西,称之为EJBQL 、JPQL List<Person> list=(List<Person>)em.createQuery("from Person").getResultList(); if(list!=null&&list.size()>0){ for(Person person:list){ System.out.println("id:\t"+person.getId()+"\tname:\t"+person.getName()); } } em.close(); factory.close(); } }
http://blog.csdn.net/itera/article/details/6092244 //JPQL学习