準備:建表
用MySQL在名為STMS數(shù)據(jù)庫中建表person
在MyEclipse中建連接數(shù)據(jù)庫的連接名為MySQL_localhost,在Driver JARS中導入MySQL的驅動程序mysql-connector-java-5.1.6-bin.jar
一.新建名為MySQLHibernate的Web Project
File—new—project—MyEclipse—Java Enterprice Projects—Web Project(Optional Maven Support)
在Project Name中輸入MySQLHibernate---點擊Finsh完成
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
7
<hibernate-configuration>
8
9
<session-factory>
10
11
<property name="connection.username">rootproperty>
12
13
14
<property name="connection.url">
15
jdbc:mysql://localhost:3306/STMS?useUnicode=true&characterEncoding=GBK
16
property>
17
18
19
<property name="dialect">
20
org.hibernate.dialect.MySQLDialect
21
property>
22
23
24
<property name="myeclipse.connection.profile">
25
MySql_localhost
26
property>
27
28
29
<property name="connection.password">rootproperty>
30
31
32
<property name="connection.driver_class">
33
com.mysql.jdbc.Driver
34
property>
35
36
37
<property name="show_sql">trueproperty>
38
39
40
<mapping resource="org/lxh/hibernate/demo01/Person.hbm.xml" />
41
42
session-factory>
43
44
hibernate-configuration>
二:給項目加入Hibernate支持
選中剛剛新建的項目—MyEcplipse—Project Capabilites—Add Hibernate Capabilites
Next—Next—選擇連接數(shù)據(jù)庫的連接名—Next--
點擊Finash完成
三:建立Person表的POJO類
1
package org.lxh.hibernate.demo01;
2
3
/** *//**
4
* @author ∪∩BUG E-mail: tidelgl@163.com
5
* @version Aug 30, 2008 8:05:41 AM
6
* @person表的POJO類,類名與表名相同
7
*/
8
public class Person
{
9
10
//以下的屬性與Person表中的字段名相同
11
private String id;
12
private String name;
13
private String password;
14
private String sex;
15
private String email;
16
17
public String getId()
{
18
return id;
19
}
20
21
public void setId(String id)
{
22
this.id = id;
23
}
24
25
public String getName()
{
26
return name;
27
}
28
29
public void setName(String name)
{
30
this.name = name;
31
}
32
33
public String getPassword()
{
34
return password;
35
}
36
37
public void setPassword(String password)
{
38
this.password = password;
39
}
40
41
public String getSex()
{
42
return sex;
43
}
44
45
public void setSex(String sex)
{
46
this.sex = sex;
47
}
48
49
public String getEmail()
{
50
return email;
51
}
52
53
public void setEmail(String email)
{
54
this.email = email;
55
}
56
57
}
58
四:通過Hibernate反向工程建立person表與Person類的映射
首先調出DB Browser視圖(Windows—view show—other—MyEclipse datebase—DB Browser)—展開MySQL_localhost至表person—右鍵表person—Hibernate Reverse Engineering
Finash完成
1
xml version="1.0" encoding="utf-8"?>
2
DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
3
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
4
7
<hibernate-mapping>
8
13
<class name="Dao.Student" table="student" catalog="stms">
14
<id name="sno" type="java.lang.String">
15
<column name="Sno" length="20" />
16
17
<generator class="increment" />
18
id>
19
<property name="sname" type="java.lang.String">
20
<column name="Sname" length="20" not-null="true" />
21
property>
22
<property name="password" type="java.lang.String">
23
<column name="Password" length="20" />
24
property>
25
class>
26
hibernate-mapping>
27
五:建立具體操作Hibernate的類
src/org.lxh.hibernate.demo01.PersonOperate.java
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
* @author ∪∩BUG E-mail: tidelgl@163.com
14
* @version Aug 30, 2008 8:27:53 AM
15
* @ 具體操作Hibernate的類
16
* @ 功能:增加,刪除,個性,按ID查詢,模糊查詢,查詢全部操作
17
* @注意導入的包:從org.hibernate.*;
18
*/
19
public class PersonOperate
{
20
// 在Hibernate中所有的操作都是通過Session來完成
21
private Session session = null;
22
23
// Session 是一個接口,必須實例化
24
// 在構造方法中實例實化Session對象
25
public PersonOperate()
{
26
// 找到Hibernate配置文件
27
Configuration config = new Configuration().configure();
28
29
// 從全局配置文件中取出SessionFactory
30
SessionFactory factory = config.buildSessionFactory();
31
32
// 從SessionFactory中取出一個Session
33
this.session = factory.openSession();
34
35
}
36
37
// 所有的操作都是通過Session進行
38
// (1)實現(xiàn)向數(shù)據(jù)庫中插入數(shù)據(jù)
39
public void insert(Person p)
{
40
// 1.開始事務
41
Transaction tran = this.session.beginTransaction();
42
43
// 2.執(zhí)行語句
44
this.session.save(p);
45
46
// 提交事務
47
tran.commit();
48
}
49
50
// (2)實現(xiàn)修改數(shù)據(jù)庫的數(shù)據(jù)
51
public void update(Person p)
{
52
// 1.開始事務
53
Transaction tran = this.session.beginTransaction();
54
55
// 2.執(zhí)行語句
56
this.session.update(p);
57
58
// 提交事務
59
tran.commit();
60
61
}
62
63
// (3)實現(xiàn)查詢數(shù)據(jù)庫的數(shù)據(jù)
64
// 使用HQL按ID查詢
65
public Person queryById(String id)
{
66
Person p = null;
67
68
// 使用Hibernate查詢語句(HQL)
69
String hql = "From Person as p where p.id=?";// 從Person類中對象p的id查
70
71
// 通過Query接口查詢
72
Query q = this.session.createQuery(hql);
73
q.setString(0, id); // 下標從0開始(id是第一個屬性)
74
List l = q.list(); // 導入的包是 java.util.List;
75
Iterator iter = l.iterator();
76
if (iter.hasNext())
{
77
p = (Person) iter.next();
78
}
79
return p;
80
}
81
82
// (4)實現(xiàn)刪除數(shù)據(jù)庫的數(shù)據(jù)
83
public void delete(Person p)
{
84
// 1.開始事務
85
Transaction tran = this.session.beginTransaction();
86
87
// 2.執(zhí)行語句
88
this.session.delete(p);
89
90
// 提交事務
91
tran.commit();
92
93
}
94
95
//通過HQL語句實現(xiàn)刪除數(shù)據(jù)庫的數(shù)據(jù)(推薦)
96
public void delete(String id)
{
97
String hql = "delete Person where id=?";
98
Query q = this.session.createQuery(hql);
99
//設置參數(shù)
100
q.setString(0, id);
101
//執(zhí)行更新語句
102
q.executeUpdate();
103
//提交事務處理
104
this.session.beginTransaction().commit();
105
106
}
107
108
//通過HQL查詢全部數(shù)據(jù)
109
public List queryAll()
{
110
List l = null;
111
String hql = "From Person as p";
112
Query q = this.session.createQuery(hql);
113
l = q.list();
114
return l;
115
}
116
117
//通過HQL查詢全部數(shù)據(jù)
118
public List queryByLike(String cond)
{
119
List l = null;
120
//條件
121
String hql = "From Person as p where p.name like ?";
122
Query q = this.session.createQuery(hql);
123
//設置參數(shù)
124
q.setString(0, "%"+cond+"%");
125
l = q.list();
126
return l;
127
}
128
129
}
130
六:建立測試類
src/org.lxh.hibernate.demo01.TestPO.java
1
package org.lxh.hibernate.demo01;
2
3
import java.util.Iterator;
4
import java.util.List;
5
6
/** *//**
7
* @author ∪∩BUG E-mail: tidelgl@163.com
8
* @version Aug 29, 2008 9:53:52 PM
9
* @POJO類的測試類
10
*/
11
public class TestPO
{
12
13
/** *//**
14
* @param args
15
*/
16
public static void main(String[] args)
{
17
18
// 生成POJO類實例化對象
19
Person p = new Person();
20
21
// p.setId("Hibernate");
22
// p.setId("MySQL");
23
// p.setName("學習筆記");
24
p.setName("使用用Hibernate");
25
p.setPassword("123");
26
p.setEmail("tidelgl@163.com");
27
p.setSex("男");
28
29
// 實例化PersonOperate對象
30
PersonOperate po = new PersonOperate();
31
32
// 1.插入數(shù)據(jù)
33
// po.insert(p);
34
35
// 2.修改數(shù)據(jù)
36
// po.update(p);
37
38
//3.查詢數(shù)據(jù)
39
// Person p = po.queryById("Hibernate");
40
// System.out.println(p.getName());
41
42
//通過HQL查詢全部數(shù)據(jù)
43
// List l = po.queryAll();
44
//通過HQL模糊查詢
45
// List l = po.queryByLike("用");
46
// Iterator iter = l.listIterator();
47
// while(iter.hasNext()){
48
// Person p = (Person)iter.next();
49
// System.out.println(p.getName());
50
//
51
// }
52
53
//4.刪除數(shù)據(jù)
54
// po.delete(p); //通過查詢結果刪除
55
// po.delete("Hibernate"); //通過HQL語句刪除
56
57
58
}
59
60
}
61
例子結構:
