1,About JPA
JPA是sun公司定义的一套持久化API,他只是定义了相关的Interface,实现产品多种多样如Hibernate,Ibatis,topLink,openJPA等等,JPA的伟大之处就是结束了多少年以来个大ORM产品各自为营的局面,使得各ORM产品统一,使用JPA,如过你的实现产品是HIbernate,以后换其他的ORM产品就变的很方便,使我们的代码变的非常灵活。
2.这里做一个简单的事例,实现产品使用Hibernate,所使用的jar包如下所示:
3. 在src下新建一个名为M“ETA-INF”的文件夹,在其下创建一个persistence.xml,他的内容为:
<?xml version="1.0" encoding="UTF-8"?> <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="JPA" transaction-type="RESOURCE_LOCAL"> <properties> <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/> <property name="hibernate.hbm2ddl.auto" value="update"/> <property name="hibernate.show_sql" value="true"/> <property name = "hibernate.connection.driver_class" value = "com.mysql.jdbc.Driver"/> <property name = "hibernate.connection.url" value = "jdbc:mysql://localhost:3306/test"/> <property name = "hibernate.connection.username" value = "root"/> <property name = "hibernate.connection.password" value = "rootroot"/> </properties> </persistence-unit> </persistence>
4.实体bean
package com.youcent; import java.io.Serializable; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @Entity public class User implements Serializable{ private Integer id; private String name; @Id @GeneratedValue(strategy=GenerationType.AUTO) 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; } @Override public String toString() { return "User [id=" + id + ", name=" + name + "]"; } }
5.建立test类进行测试
package com.youcent; import java.util.List; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; import javax.persistence.Query; import org.junit.Test; public class TestUser { //添加 @Test public void testSave() { EntityManagerFactory factory = Persistence.createEntityManagerFactory("JPA"); EntityManager em = factory.createEntityManager(); em.getTransaction().begin(); User u = new User(); u.setName("jerry"); em.persist(u); em.getTransaction().commit(); em.close(); factory.close(); } //删除 @Test public void testRemove() { EntityManagerFactory factory = Persistence.createEntityManagerFactory("JPA"); EntityManager em = factory.createEntityManager(); em.getTransaction().begin(); User u = em.find(User.class,2); em.remove(u); em.getTransaction().commit(); em.close(); factory.close(); } //修改 @Test public void update() { EntityManagerFactory factory = Persistence.createEntityManagerFactory("JPA"); EntityManager em = factory.createEntityManager(); em.getTransaction().begin(); //第一种方法 //User u = em.find(User.class,1); //u.setName("tom"); //第二种方法 User u1 = new User(); u1.setId(3); u1.setName("xyz"); em.merge(u1); em.getTransaction().commit(); em.close(); factory.close(); } //hql 取得所有对象 @Test public void findAll() { EntityManagerFactory factory = Persistence.createEntityManagerFactory("JPA"); EntityManager em = factory.createEntityManager(); Query query = em.createQuery("select u from User u"); List<User> list = query.getResultList(); for(User u : list){ System.out.println(u.toString()); } em.close(); factory.close(); } //hql 取得单一结果 ,使用位"置参数查询" @Test public void findById() { EntityManagerFactory factory = Persistence.createEntityManagerFactory("JPA"); EntityManager em = factory.createEntityManager(); Query query = em.createQuery("select u from User u where u.id=?1"); //表示明确指定从1开始设置参数 query.setParameter(1, 4); User u = (User) query.getSingleResult(); //取得单一结果 System.out.println(u.toString()); em.close(); factory.close(); } //hql 取得单一结果 ,同样任何查询可以使用"命名参数"查询 @Test public void findByHql() { EntityManagerFactory factory = Persistence.createEntityManagerFactory("JPA"); EntityManager em = factory.createEntityManager(); Query query = em.createQuery("select u from User u where u.id=:id"); //表示明确指定从1开始设置参数 query.setParameter("id", 4); User u = (User) query.getSingleResult(); //取得单一结果 System.out.println(u.toString()); em.close(); factory.close(); } //hql分页查询 @Test public void findByPage() { EntityManagerFactory factory = Persistence.createEntityManagerFactory("JPA"); EntityManager em = factory.createEntityManager(); Query query = em.createQuery("select u from User u"); query.setFirstResult(1); //表示从第二条记录开始 query.setMaxResults(2); //向后去两条 List<User> list = query.getResultList(); for(User u : list){ System.out.println(u.toString()); } em.close(); factory.close(); } //hql更新查询,须开事务 @Test public void updateByhql() { EntityManagerFactory factory = Persistence.createEntityManagerFactory("JPA"); EntityManager em = factory.createEntityManager(); em.getTransaction().begin(); Query query = em.createQuery("update User u set u.name=:name where u.id=:id"); query.setParameter("name", "tom1"); query.setParameter("id", 1); int rows = query.executeUpdate(); //更新查询要执行的方法 if(rows>0){ System.out.println("更新成功!"); } em.getTransaction().commit(); em.close(); factory.close(); } //hql删除查询,须开事务 @Test public void removeByhql() { EntityManagerFactory factory = Persistence.createEntityManagerFactory("JPA"); EntityManager em = factory.createEntityManager(); em.getTransaction().begin(); Query query = em.createQuery("delete from User u where u.id=:id"); query.setParameter("id", 4); int rows = query.executeUpdate(); //更新查询要执行的方法 if(rows>0){ System.out.println("删除成功!"); } em.getTransaction().commit(); em.close(); factory.close(); } }
6.总结一下常用注解
1、日期:
@Temporal(TeporalType.DATE) -----> Date
2、枚举:
@Enumerated(EnumType.STAING) ---> Enum
3、大文本,二进制数据:
@Lob ------> String,Byte[]
4、不和数据库映射
@Transient
5、延迟加载
@Basc(fetch=FetchType.LAZY),只有当使用属性的get方法
是才从数据中获取!