jpa入门之环境搭建和CRUD基本操作

来源:互联网 发布:淘宝上如何收藏店铺 编辑:程序博客网 时间:2024/05/18 03:24

hibernate虽然好用但编写映射文件还是比较麻烦,虽然可以借助插件但是后期的维护还是比较麻烦,jpa的全称是Java Persistence API,实现该规范的产品很多像hibernate就是其中比较出名的一个,原则上应该尽量不要使用hibernate,可惜jpa只是一个接口规范,自 己按照规范写一套也不现实,只能通过hibernate间接的使用jpa.

1 使用hibernate的jpa实现需要的jar包如下

20140906194058718.jpg

我用的是hibernate3.6的版本,如果是低版本的hibernate则还需要hibernate-commons-annotations.jar、hibernate-annotations.jar

 

2  persistence.xml的相关配置

jpa和hibernate类似需要一个类似hibernate.cfg.xml的配置文件,该文件指定要操作的相关数据库,内容如下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?xmlversion="1.0"encoding="UTF-8"?> 
<persistencexmlns="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-unitname="myPersistUnit"transaction-type="RESOURCE_LOCAL"
       <provider>org.hibernate.ejb.HibernatePersistence</provider
       <properties>   
               <propertyname="hibernate.hbm2ddl.auto"value="update"/> 
               <!-- 数据库的相关配置 -->   
               <propertyname="hibernate.connection.driver_class"value="com.mysql.jdbc.Driver"/>   
               <propertyname="hibernate.connection.url"value "jdbc:mysql://127.0.0.1:3306/exercise?useUnicode=true&characterEncoding=UTF-8"/> 
               <propertyname="hibernate.connection.username"value="root"/>   
               <propertyname="hibernate.connection.password"value="123456"/>  
               <!-- 指定方言 -->  
               <propertyname="hibernate.dialect"value="org.hibernate.dialect.MySQL5Dialect"/>   
               <propertyname="hibernate.show_sql"value="false"/>   
               <propertyname="hibernate.format_sql"value="true"/>  
        </properties>   
    </persistence-unit
</persistence>

相信学过hibernate应该能看懂这个配置文件,需要注意的是persistence-unit节点的name属性,这个名称后面将会用到,另外这个 文件的名称只能是persistence.xml不能是别的名称且必须放在src下面的META-INF文件夹下面,如果不存在可以手动创建。

3  编写实体类

实体是jpa中比较重要的一部分,它承担着POJO和类似hibernate映射文件的功能,算得上是jpa的核心文件,下面这个是其中一个例子

 
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
packageorg.lxh.info; 
   
importjava.util.Date; 
   
importjavax.persistence.Column; 
importjavax.persistence.Embedded; 
importjavax.persistence.Entity; 
importjavax.persistence.EnumType; 
importjavax.persistence.Enumerated; 
importjavax.persistence.GeneratedValue; 
importjavax.persistence.GenerationType; 
importjavax.persistence.Id; 
importjavax.persistence.Table; 
importjavax.persistence.Temporal; 
importjavax.persistence.TemporalType; 
   
@Entity 
@Table(name = "m_users"
publicclass User { 
       
    privateint id; 
    privateString name; 
    privateDate birthday; 
    privateSex sex; 
    @Id 
    @GeneratedValue 
    publicint getId() { 
        returnid; 
    
   
    publicvoid setId(intid) { 
        this.id = id; 
    
    @Column(length=20,nullable=false
    publicString getName() { 
        returnname; 
    
    publicvoid setName(String name) { 
        this.name = name; 
    
    @Temporal(TemporalType.DATE) 
    publicDate getBirthday() { 
        returnbirthday; 
    
   
    publicvoid setBirthday(Date birthday) { 
        this.birthday = birthday; 
    
    @Enumerated(EnumType.STRING) 
    @Column(length=5,nullable=false
    publicSex getSex() { 
        returnsex; 
    
   
    publicvoid setSex(Sex sex) { 
        this.sex = sex; 
    
}

下面是注解的功能描述

@Entity   这个注解表明这个java类是一个实体对象

@Table  该注解用来指定实体对应的表,默认情况下表名和实体类的名称相同

@Id         该注解用来指定主键

@GeneratedValue   该注解配置的是主键的生成策略

@Column    该注解用于指定数据库表对于的列名、唯一约束、非空约束等

@Temporal   主要用于日期属性上面,可以指定日期的类型

@Lob      指定映射到数据库的字段为大文本数据或者字节数组

@Enumerated  指定对于的属性为枚举

4  编写简单的JPA工具类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
packageorg.lxh.util; 
   
importjavax.persistence.EntityManager; 
importjavax.persistence.EntityManagerFactory; 
importjavax.persistence.Persistence; 
   
publicfinal class JpaUtil { 
  privatestatic EntityManagerFactory em; 
  static
      em=Persistence.createEntityManagerFactory("myPersistUnit"); 
  
  publicstatic EntityManager getEntityManager(){ 
      returnem.createEntityManager(); 
  
}

5  JPA的crud基本操作
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
packageorg.lxh.test; 
   
importstatic org.junit.Assert.*; 
   
importjava.util.*; 
importjavax.persistence.EntityManager; 
importjavax.persistence.EntityTransaction; 
importjavax.persistence.Query; 
   
importorg.lxh.info.Sex; 
importorg.lxh.info.User; 
importorg.lxh.util.JpaUtil; 
   
publicclass Test { 
   
    @org.junit.Test 
    publicvoid testInsert() { 
        EntityManager em=null
        EntityTransaction tx=null
        try
            em=JpaUtil.getEntityManager(); 
            tx=em.getTransaction(); 
            tx.begin(); 
            User u=newUser(); 
            u.setBirthday(newDate()); 
            u.setName("潘玮柏"); 
            u.setSex(Sex.MAN); 
            em.persist(u); 
            tx.commit(); 
        }catch(Exception e){ 
            e.printStackTrace(); 
        }finally
            if(em!=null){ 
                em.close(); 
            
        
           
    
    @org.junit.Test 
    publicvoid testUpdate() { 
        EntityManager em=null
        EntityTransaction tx=null
        try
            em=JpaUtil.getEntityManager(); 
            tx=em.getTransaction(); 
            tx.begin(); 
            User u=newUser(); 
            u.setId(2); 
            u.setName("周杰伦"); 
            u.setSex(Sex.MAN); 
            u.setBirthday(newDate()); 
            em.merge(u); 
            tx.commit(); 
        }catch(Exception e){ 
            e.printStackTrace(); 
        }finally
            if(em!=null){ 
                em.close(); 
            
        
           
    
    @org.junit.Test 
    publicvoid testDelete() { 
        EntityManager em=null
        EntityTransaction tx=null
        try
            em=JpaUtil.getEntityManager(); 
            tx=em.getTransaction(); 
            tx.begin(); 
            User u=em.find(User.class2); 
            em.remove(u); 
            tx.commit(); 
        }catch(Exception e){ 
            e.printStackTrace(); 
        }finally
            if(em!=null){ 
                em.close(); 
            
        
           
    
    /**
     * 最简单的查询
     */ 
    @org.junit.Test 
    publicvoid testJPLQuery() { 
        EntityManager em=null
        EntityTransaction tx=null
        try
            em=JpaUtil.getEntityManager(); 
            String jpl="select u from User u"
            Query q=em.createQuery(jpl); 
            List<User> all=q.getResultList(); 
            Iterator<User> it=all.iterator(); 
            while(it.hasNext()){ 
                User user=it.next(); 
                System.out.println(user.getName()+","+user.getBirthday()); 
            
        }catch(Exception e){ 
            e.printStackTrace(); 
        }finally
            if(em!=null){ 
                em.close(); 
            
        
           
    
    /**
     * 使用命名参数的方式更新数据
     */ 
    @org.junit.Test 
    publicvoid testJPLUpdate() { 
        EntityManager em=null
        EntityTransaction tx=null
        try
            em=JpaUtil.getEntityManager(); 
            String jpl="update User u set u.name=:name where u.id=:id"
            Query q=em.createQuery(jpl); 
            q.setParameter("name""加菲猫"); 
            q.setParameter("id"3); 
            q.executeUpdate(); 
              
        }catch(Exception e){ 
            e.printStackTrace(); 
        }finally
            if(em!=null){ 
                em.close(); 
            
        
           
    
}

最后来分析一下jpa的缺点:

1> 提供的主键生成策略较少

2>没有缓存机制

3> 实体类中注解和java代码混合在一起,可读性降低了

来自:http://blog.csdn.net/walkcode/article/details/39103277

0 0
原创粉丝点击