实体bean(entity)配置,jpa增删改api,JPAsql增删改

1.ORM框架必然发展趋势:

jdbc->hibernate(是产品,实现jpa规范)->jpa(是规范,不是产品)。

ps:运用jpa规范的API进行编程,不对Hiberbate,topLink等orm框架构成威胁。


2.JPA环境搭建[hibernate-distribution-3.6.10.Final]

1.准备lib包
2.jar包引入时,千万注意目录不能有中文或者空格

3.开发步骤:

1.先建表,再编写配置文件和bean-(面向过程,传统的数据库建模思想)
2.先编写配置文件和bean,在建表(OOP思想)-要求比较高

4.demo实例

事务种类:
1.本地事务:支持对同一个数据库的事务操作——大部分应用

2.全局事务:支持对多个数据库的事务操作(银行转账)-两次提交协议

步骤:

第一步:项目结构


2.持久化文件配置:

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <persistence xmlns="http://java.sun.com/xml/ns/persistence"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"  
  5.     version="2.0">  
  6.     <persistence-unit name="MyJpa" transaction-type="RESOURCE_LOCAL">  
  7.         <properties>  
  8.             <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />  
  9.             <property name="hibernate.hbm2ddl.auto" value="update" /><!--已存在则更新,不存在则创建  -->  
  10.             <property name="hibernate.connection.driver_class" value="org.gjt.mm.mysql.Driver" />  
  11.             <property name="hibernate.connection.username" value="root" />  
  12.             <property name="hibernate.connection.password" value="123456" />  
  13.             <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/jpadb?useUnicode=true&characterEncoding=UTF-8" />  
  14.         </properties>  
  15.     </persistence-unit>  
  16. </persistence>  

3.实体bean

知识点:字段的长度,是否为空,关键字,自增,字段名称的映射修改,表名称的映射修改,字段类型(Date类型)-不同格式要求,枚举类的注释(索引,枚举值)-性别,大文本类型数据,二进制数据映射,不想某个字段跟表有映射关系,为了防止某个字段数据量过大而占用内存过大因此对其进行延迟加载(懒惰加载,需要获取数据时才得到数据)。

  1. import java.util.Date;  
  2. import javax.persistence.Basic;  
  3. import javax.persistence.Column;  
  4. import javax.persistence.Entity;  
  5. import javax.persistence.EnumType;  
  6. import javax.persistence.Enumerated;  
  7. import javax.persistence.FetchType;  
  8. import javax.persistence.GeneratedValue;  
  9. import javax.persistence.Id;  
  10. import javax.persistence.Lob;  
  11. import javax.persistence.Table;  
  12. import javax.persistence.Temporal;  
  13. import javax.persistence.TemporalType;  
  14. import javax.persistence.Transient;  
  15.   
  16. @Entity  
  17. @Table(name="person")  
  18. public class Person {  
  19.     private Integer id;  
  20.     private String name;  
  21.     private Date birthday;  
  22.     private Sex sex;  
  23.     private String info;  
  24.     private Byte[] file;  
  25.     private String other;  
  26.       
  27.       
  28.     public Person() {  
  29.         super();  
  30.     }  
  31.     public Person(String name) {  
  32.         super();  
  33.         this.name = name;  
  34.     }  
  35.     public Person(String name, Date birthday) {  
  36.         super();  
  37.         this.name = name;  
  38.         this.birthday = birthday;  
  39.     }  
  40.       
  41.     public Person(String name, Date birthday, Sex sex) {  
  42.         super();  
  43.         this.name = name;  
  44.         this.birthday = birthday;  
  45.         this.sex = sex;  
  46.     }  
  47.     /** 
  48.      * 主键并自增 
  49.      * @return the id 
  50.      */  
  51.     @Id @GeneratedValue  
  52.     public Integer getId() {  
  53.         return id;  
  54.     }  
  55.     /** 
  56.      * @param id the id to set 
  57.      */  
  58.     public void setId(Integer id) {  
  59.         this.id = id;  
  60.     }  
  61.     /** 
  62.      * @return the name 
  63.      */  
  64.     @Column(length=10,nullable=false,name="personName")  
  65.     public String getName() {  
  66.         return name;  
  67.     }  
  68.     /** 
  69.      * @param name the name to set 
  70.      */  
  71.     public void setName(String name) {  
  72.         this.name = name;  
  73.     }  
  74.     /** 
  75.      * @return the birthday 
  76.      */  
  77.     @Temporal(TemporalType.DATE)  
  78.     public Date getBirthday() {  
  79.         return birthday;  
  80.     }  
  81.     /** 
  82.      * @param birthday the birthday to set 
  83.      */  
  84.     public void setBirthday(Date birthday) {  
  85.         this.birthday = birthday;  
  86.     }  
  87.     /** 
  88.      * @return the sex 
  89.      */  
  90.     @Enumerated(EnumType.STRING)  
  91.     public Sex getSex() {  
  92.         return sex;  
  93.     }  
  94.     /** 
  95.      * @param sex the sex to set 
  96.      */  
  97.     public void setSex(Sex sex) {  
  98.         this.sex = sex;  
  99.     }  
  100.     /** 
  101.      * @return the info 
  102.      */  
  103.     @Lob  
  104.     public String getInfo() {  
  105.         return info;  
  106.     }  
  107.     /** 
  108.      * @param info the info to set 
  109.      */  
  110.     public void setInfo(String info) {  
  111.         this.info = info;  
  112.     }  
  113.     /** 
  114.      * @return the file 
  115.      */  
  116.     @Lob @Basic(fetch=FetchType.LAZY) //当文件很大时,进行懒惰加载  
  117.     public Byte[] getFile() {  
  118.         return file;  
  119.     }  
  120.     /** 
  121.      * @param file the file to set 
  122.      */  
  123.     public void setFile(Byte[] file) {  
  124.         this.file = file;  
  125.     }  
  126.     /** 
  127.      * @return the other 
  128.      */  
  129.     @Transient //排除某个字段的映射  
  130.     public String getOther() {  
  131.         return other;  
  132.     }  
  133.     /** 
  134.      * @param other the other to set 
  135.      */  
  136.     public void setOther(String other) {  
  137.         this.other = other;  
  138.     }  
  139.       
  140. }  
枚举类:
  1. public enum Sex {  
  2.     MAN,WORMAN  
  3. }  

4.单元测试类

知识点:

1.把握异常出现的时机。
2.通过ID得到实体bean(1.彻底查询 2.用到查询)
3.保存实体bean到数据库
4.更新实体bean到数据库中
涉及到对象的状态:
1.新建
2.托管(设置实体的字段值,并通过提交可以同步到数据库)
3.游离(无法更新到数据库中,除非使用merge方法重新可将其更新到数据库中)
4.删除

  1. public class PersonTest {  
  2.     @Test  
  3.     public void save(){  
  4.         EntityManagerFactory factory=Persistence.createEntityManagerFactory("MyJpa");  
  5.         EntityManager em=factory.createEntityManager();  
  6.         em.getTransaction().begin();  
  7.         em.persist(new Person("techbirds",new Date(),Sex.MAN));  
  8.         em.getTransaction().commit();  
  9.         em.close();  
  10.         factory.close();  
  11.           
  12.     }  
  13.     @Test  
  14.     public void getPerson1(){  
  15.         EntityManagerFactory factory=Persistence.createEntityManagerFactory("MyJpa");  
  16.         EntityManager em=factory.createEntityManager();  
  17.         em.getTransaction().begin();  
  18.         Person p=em.find(Person.class1);  
  19.         em.getTransaction().commit();  
  20.         em.close();  
  21.         factory.close();  
  22.         System.out.println(p.getName());  
  23.     }  
  24.     @Test  
  25.     public void getPerson2(){  
  26.         EntityManagerFactory factory=Persistence.createEntityManagerFactory("MyJpa");  
  27.         EntityManager em=factory.createEntityManager();  
  28.         em.getTransaction().begin();  
  29.         Person p=em.getReference(Person.class1);  
  30.         //代理对象,用到才查询  
  31.         System.out.println(p.getName());  
  32.         em.getTransaction().commit();  
  33.         em.close();  
  34.         //System.out.println(p.getName());出错,事务已经关闭  
  35.         factory.close();  
  36.           
  37.     }  
  38.     @Test  
  39.     public void updatePerson1(){  
  40.         EntityManagerFactory factory=Persistence.createEntityManagerFactory("MyJpa");  
  41.         EntityManager em=factory.createEntityManager();  
  42.         em.getTransaction().begin();  
  43.         Person p=em.find(Person.class1);  
  44.         p.setName("bao");  
  45.         em.getTransaction().commit();  
  46.         em.close();  
  47.         factory.close();  
  48.     }     
  49.     @Test  
  50.     public void updatePerson2(){  
  51.         EntityManagerFactory factory=Persistence.createEntityManagerFactory("MyJpa");  
  52.         EntityManager em=factory.createEntityManager();  
  53.         em.getTransaction().begin();  
  54.         Person p=em.find(Person.class1);  
  55.         em.clear();//将所有实体管理器中的所有实体变成游离状态,无法跟数据库同步  
  56.         p.setName("techbirds");  
  57.         em.getTransaction().commit();  
  58.         em.close();  
  59.         factory.close();  
  60.     }  
  61.     @Test  
  62.     public void updatePerson3(){  
  63.         EntityManagerFactory factory=Persistence.createEntityManagerFactory("MyJpa");  
  64.         EntityManager em=factory.createEntityManager();  
  65.         em.getTransaction().begin();  
  66.         Person p=em.find(Person.class1);  
  67.         em.clear();//将所有实体管理器中的所有实体变成游离状态,无法跟数据库同步  
  68.         p.setName("techbirds");  
  69.         em.merge(p);//此时又可以进行同步  
  70.         em.getTransaction().commit();  
  71.         em.close();  
  72.         factory.close();  
  73.     }  
  74.       
  75.     @Test  
  76.     public void delPerson(){  
  77.         EntityManagerFactory factory=Persistence.createEntityManagerFactory("MyJpa");  
  78.         EntityManager em=factory.createEntityManager();  
  79.         em.getTransaction().begin();  
  80.         Person p=em.find(Person.class1);  
  81.         em.remove(p);  
  82.         em.getTransaction().commit();  
  83.         em.close();  
  84.         factory.close();  
  85.     }  
  86.       
  87. }  

5.jpa的(sql)查询
jpaSQL语句:面向对象的sql语句,jpa标准的sql语法
查询方法:
1.位参数查询   select o from Person o where o.id=?1—>query.setParameter(1,2);
2.命名查询     select o from Person o where o.id=:id—>query.setParameter("id",2);
查询结果:1.列表 2.唯一值(对象)
查询类型:普通查询,删除查询,更新查询

ps:进行数据的更改必须启动事务。-删除查询和更新查询必须开启事务

  1. @Test  
  2.     public void querysql(){  
  3.         EntityManagerFactory factory=Persistence.createEntityManagerFactory("MyJpa");  
  4.         EntityManager em=factory.createEntityManager();  
  5.         //面向对象的sql语句  
  6.         Query query=em.createQuery("select o from Person o where o.id=?");  
  7.         query.setParameter(12);  
  8.         Person p=(Person) query.getSingleResult();  
  9.         System.out.println(p.getName());  
  10.         em.close();  
  11.         factory.close();  
  12.           
  13.     }  
  14.       
  15.     @Test  
  16.     public void deletesql(){  
  17.         EntityManagerFactory factory=Persistence.createEntityManagerFactory("MyJpa");  
  18.         EntityManager em=factory.createEntityManager();  
  19.         em.getTransaction().begin();  
  20.         //面向对象的sql语句  
  21.         Query query=em.createQuery("delete from Person o where o.id=?");  
  22.         query.setParameter(13);  
  23.         query.executeUpdate();  
  24.         em.getTransaction().commit();  
  25.         em.close();  
  26.         factory.close();  
  27.           
  28.     }  
  29.       
  30.     @Test  
  31.     public void updatesql(){  
  32.         EntityManagerFactory factory=Persistence.createEntityManagerFactory("MyJpa");  
  33.         EntityManager em=factory.createEntityManager();  
  34.         em.getTransaction().begin();  
  35.         //面向对象的sql语句  
  36.         Query query=em.createQuery("update Person o set o.sex=? where o.id=?");  
  37.         query.setParameter(1, Sex.WORMAN);  
  38.         query.setParameter(23);  
  39.         query.executeUpdate();  
  40.         em.getTransaction().commit();  
  41.         em.close();  
  42.         factory.close();  
  43.           
  44.     }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值