初學Hibernate3
我只用的是Eclipse3.2+MyEclipse6.0+JDK6+Tomcat+MSSQL-2000。1.首先建立數據庫表(非常簡單就不用說了)。
DROP TABLE person ;
CREATE TABLE person
(
id varchar(32) not null primary key ,
name varchar(20) not null ,
password varchar(20) not null ,
sex varchar(2) ,
email varchar(30)
) ;
2.啟動Eclipse,切到MyEclipse Database Explorer透視圖,在左窗體中點menu->新建,在Drive Temple中選擇你的模板,然后起個名字。在Connection URL中填入你的鏈接如:Mssql:jdbc:microsoft:sqlserver://localhost:1433。填入你的數據庫名、密碼(可填可不填)。在Drive JARs中加入你的數據庫驅動類庫。
3.在Eclipse中建立一個Web工程
選中當前項目,點菜單欄中的MyEclipse->project Capabilities->ADD Hibernate Capabilities。
MyEclipse會自動生成。.cfg.xml文件。代碼如下:
1
<?xml version='1.0' encoding='UTF-8'?>
2
<!DOCTYPE hibernate-configuration PUBLIC
3
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
4
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
5
6
<!-- Generated by MyEclipse Hibernate Tools. -->
7
<hibernate-configuration>
8
9
<session-factory>
10
<property name="myeclipse.connection.profile">Oracle 9</property>
11
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:MLDN</property>
12
<property name="connection.username">scott</property>
13
<property name="connection.password">tiger</property>
14
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
15
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
16
<property name="show_sql">true</property>
17
<mapping resource="org/lxh/hibernate/demo01/Person.hbm.xml" />
18
</session-factory>
19
20
</hibernate-configuration>

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

4.切換到MyEclipse Database Explorer透視圖,找到你的表后右鍵Hibernate Reverse Engineering,接下來可以選擇自動生成pojo和映射文件。下面是這兩個文件的代碼:
pojo: person.java
1
ackage org.lxh.hibernate.demo01;
2
// POJO類
3
public class Person {
4
// 寫入若干屬性
5
private String id ;
6
private String name ;
7
private String password ;
8
private String sex ;
9
private String email ;
10
public String getEmail() {
11
return email;
12
}
13
public void setEmail(String email) {
14
this.email = email;
15
}
16
public String getId() {
17
return id;
18
}
19
public void setId(String id) {
20
this.id = id;
21
}
22
public String getName() {
23
return name;
24
}
25
public void setName(String name) {
26
this.name = name;
27
}
28
public String getPassword() {
29
return password;
30
}
31
public void setPassword(String password) {
32
this.password = password;
33
}
34
public String getSex() {
35
return sex;
36
}
37
public void setSex(String sex) {
38
this.sex = sex;
39
}
40
}
41

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

映射文件:Person.hbm.xml






















1
package org.lxh.hibernate.demo01;
2
3
import java.util.Iterator;
4
import java.util.List;
5
6
import org.hibernate.Query;
7
import org.hibernate.Session;
8
import org.hibernate.SessionFactory;
9
import org.hibernate.Transaction;
10
import org.hibernate.cfg.Configuration;
11
12
/*
13
* 具體操作Hibernate的類:
14
* 增加、刪除、修改、按ID查詢、模糊查詢、查詢全部操作
15
* */
16
public class PersonOperate {
17
// 在Hibernate中,所有的操作都是通過Session完成
18
// 此Session不同于JSP的Session
19
private Session session = null ;
20
21
// 在構造方法之中實例化session對象
22
public PersonOperate()
23
{
24
// 找到Hibernate配置
25
Configuration config = new Configuration().configure() ;
26
// 從配置中取出SessionFactory
27
SessionFactory factory = config.buildSessionFactory() ;
28
// 從SessionFactory中取出一個Session
29
this.session = factory.openSession() ;
30
}
31
32
// 所有的操作都是通過session進行的
33
// 向數據庫中增加數據
34
public void insert(Person p)
35
{
36
// 開始事務
37
Transaction tran = this.session.beginTransaction() ;
38
// 執行語句
39
this.session.save(p) ;
40
// 提交事務
41
tran.commit() ;
42
// 關閉Session
43
this.session.close() ;
44
}
45
46
// 修改
47
public void update(Person p)
48
{
49
// 開始事務
50
Transaction tran = this.session.beginTransaction() ;
51
// 執行語句
52
this.session.update(p) ;
53
// 提交事務
54
tran.commit() ;
55
}
56
57
// 按ID查詢:推薦使用HQL —— 是Hibernate官方推薦的查詢語言
58
public Person queryById(String id)
59
{
60
Person p = null ;
61
// 使用Hibernate查詢語言
62
String hql = "FROM Person as p WHERE p.id=?" ;
63
// 通過Query接口查詢
64
Query q = this.session.createQuery(hql) ;
65
q.setString(0,id) ;
66
List l = q.list() ;
67
Iterator iter = l.iterator() ;
68
if(iter.hasNext())
69
{
70
p = (Person)iter.next() ;
71
}
72
return p ;
73
}
74
75
// 刪除數據
76
// Hibernate2、Hibernate 3通用的刪除
77
// 使用此方法刪除數據之前,必須先查找到數據對象,性能呢?
78
public void delete(Person p)
79
{
80
Transaction tran = this.session.beginTransaction() ;
81
// 執行語句
82
this.session.delete(p) ;
83
// 提交事務
84
tran.commit() ;
85
}
86
87
// 在Hibernate 3之中根據HQL中的語句進行了修改,增加了刪除指令
88
public void delete(String id)
89
{
90
String hql = "DELETE Person WHERE id=?" ;
91
Query q = this.session.createQuery(hql) ;
92
// 把參數設置
93
q.setString(0,id) ;
94
// 執行更新語句
95
q.executeUpdate() ;
96
// 進行事務處理
97
this.session.beginTransaction().commit() ;
98
}
99
100
// 查詢全部數據,寫HQL
101
public List queryAll()
102
{
103
List l = null ;
104
String hql = "FROM Person as p" ;
105
Query q = this.session.createQuery(hql) ;
106
l = q.list() ;
107
return l ;
108
}
109
110
// 模糊查詢
111
public List queryByLike(String cond)
112
{
113
List l = null ;
114
String hql = "FROM Person as p WHERE p.name like ?" ;
115
Query q = this.session.createQuery(hql) ;
116
q.setString(0,"%"+cond+"%") ;
117
l = q.list() ;
118
return l ;
119
}
120
}
121
6.寫一個測試類:TestOP.java
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

1
package org.lxh.hibernate.demo01;
2
3
import java.util.Iterator;
4
import java.util.List;
5
6
public class TestPO {
7
8
/**
9
* @param args
10
*/
11
public static void main(String[] args) {
12
// TODO 自動生成方法存根
13
// 生成POJO類實例化對象
14
Person p = new Person() ;
15
p.setId("10001") ;
16
p.setName("古天樂") ;
17
p.setPassword("www.woailou.cn") ;
18
p.setSex("男") ;
19
p.setEmail("asdffdsa@163.com") ;
20
PersonOperate po = new PersonOperate() ;
21
po.insert(p) ;
22
//以上是向數據庫中插入所需代碼。
23
Person p = po.queryById("10001") ;
24
System.out.println(p.getName()) ;
25
//以上是根據ID查詢所需代碼。
26
po.delete("10001") ;
27
//刪除ID為10001的數據
28
List l = po.queryByLike("天") ;
29
Iterator iter = l.iterator() ;
30
while(iter.hasNext())
31
{
32
Person p = (Person)iter.next() ;
33
System.out.println(p.getName()) ;
34
}
35
//以上是模糊查詢
36
}
37
38
}
39
注:本文章是自我學習。

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

posted on 2008-07-23 10:45 生命的綻放 閱讀(343) 評論(0) 編輯 收藏 所屬分類: Hibernate