Java & Assembly
首頁
新隨筆
聯系
管理
隨筆-208 評論-469 文章-30 trackbacks-0
用hibernate3實現增刪改查
程序結構圖
SQLServer中的表設計
db.HibernateUtil
package
?db;
import
?org.hibernate.HibernateException;
import
?org.hibernate.Session;
import
?org.hibernate.SessionFactory;
import
?org.hibernate.cfg.Configuration;
/**?*/
/**
?*?Configures?and?provides?access?to?Hibernate?sessions,?tied?to?the?current
?*?thread?of?execution.?Follows?the?Thread?Local?Session?pattern,?see
?*?{
@link
?
http://hibernate.org/42.html
}.
?
*/
public
?
class
?HibernateUtil
{
????
/**?*/
/**
?????*?Location?of?hibernate.cfg.xml?file.?NOTICE:?Location?should?be?on?the
?????*?classpath?as?Hibernate?uses?#resourceAsStream?style?lookup?for?its
?????*?configuration?file.?That?is?place?the?config?file?in?a?Java?package?-?the
?????*?default?location?is?the?default?Java?package.<br>
?????*?<br>
?????*?Defaults:?<br>
?????*?<code>CONFIG_FILE_LOCATION?=?"/hibernate.conf.xml"</code>?You?can
?????*?change?location?with?setConfigFile?method?session?will?be?rebuilded?after
?????*?change?of?config?file
?????
*/
????
private
?
static
?String?CONFIG_FILE_LOCATION?
=
?
"
/hibernate.cfg.xml
"
;
????
private
?
static
?
final
?ThreadLocal?threadLocal?
=
?
new
?ThreadLocal();
????
private
?
static
?Configuration?configuration?
=
?
new
?Configuration();
????
private
?
static
?SessionFactory?sessionFactory;
????
private
?
static
?String?configFile?
=
?CONFIG_FILE_LOCATION;
????
private
?HibernateUtil()
????
{
????}
????
/**?*/
/**
?????*?Returns?the?ThreadLocal?Session?instance.?Lazy?initialize?the
?????*?<code>SessionFactory</code>?if?needed.
?????*?
?????*?
@return
?Session
?????*?
@throws
?HibernateException
?????
*/
????
public
?
static
?Session?getCurrentSession()?
throws
?HibernateException
????
{
????????Session?session?
=
?(Session)?threadLocal.get();
????????
if
?(session?
==
?
null
?
||
?
!
session.isOpen())
????????
{
????????????
if
?(sessionFactory?
==
?
null
)
????????????
{
????????????????rebuildSessionFactory();
????????????}
????????????session?
=
?(sessionFactory?
!=
?
null
)?
?
?sessionFactory.openSession()
????????????????????:?
null
;
????????????threadLocal.set(session);
????????}
????????
return
?session;
????}
????
/**?*/
/**
?????*?Rebuild?hibernate?session?factory
?????*?
?????
*/
????
public
?
static
?
void
?rebuildSessionFactory()
????
{
????????
try
????????
{
????????????configuration.configure(configFile);
????????????sessionFactory?
=
?configuration.buildSessionFactory();
????????}
?
catch
?(Exception?e)
????????
{
????????????System.err.println(
"
%%%%?Error?Creating?SessionFactory?%%%%
"
);
????????????e.printStackTrace();
????????}
????}
????
/**?*/
/**
?????*?Close?the?single?hibernate?session?instance.
?????*?
?????*?
@throws
?HibernateException
?????
*/
????
public
?
static
?
void
?closeCurrentSession()?
throws
?HibernateException
????
{
????????Session?session?
=
?(Session)?threadLocal.get();
????????threadLocal.set(
null
);
????????
if
?(session?
!=
?
null
)
????????
{
????????????session.close();
????????}
????}
????
/**?*/
/**
?????*?return?session?factory
?????*?
?????
*/
????
public
?
static
?SessionFactory?getSessionFactory()
????
{
????????
return
?sessionFactory;
????}
????
/**?*/
/**
?????*?return?session?factory
?????*?
?????*?session?factory?will?be?rebuilded?in?the?next?call
?????
*/
????
public
?
static
?
void
?setConfigFile(String?configFile)
????
{
????????HibernateUtil.configFile?
=
?configFile;
????????sessionFactory?
=
?
null
;
????}
????
/**?*/
/**
?????*?return?hibernate?configuration
?????*?
?????
*/
????
public
?
static
?Configuration?getConfiguration()
????
{
????????
return
?configuration;
????}
}
model.User
package
?model;
public
?
class
?User
{
????
private
?Integer?id;
????
private
?String?username;
????
private
?String?password;
????
public
?User()
????
{
????????
????}
????
public
?User(?String?password)
????
{
????????
this
.password?
=
?password;
????}
????
public
?Integer?getId()
????
{
????????
return
?id;
????}
????
public
?
void
?setId(Integer?id)
????
{
????????
this
.id?
=
?id;
????}
????
public
?String?getPassword()
????
{
????????
return
?password;
????}
????
public
?
void
?setPassword(String?password)
????
{
????????
this
.password?
=
?password;
????}
????
public
?String?getUsername()
????
{
????????
return
?username;
????}
????
public
?
void
?setUsername(String?username)
????
{
????????
this
.username?
=
?username;
????}
}
model.hbm.xml
<?
xml?version='1.0'?encoding='UTF-8'
?>
<!
DOCTYPE?hibernate-mapping?PUBLIC
??????????"-//Hibernate/Hibernate?mapping?DTD?2.0//EN"
??????????"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"
>
<
hibernate-mapping
>
<
class?
name
="model.User"
?table
="user1"
?
>
<
id?
name
="id"
>
<
generator?
class
="identity"
/>
</
id
>
<
property?
name
="username"
/>
<
property?
name
="password"
/>
</
class
>
</
hibernate-mapping
>
hibernate.cfg.xml
<?
xml?version='1.0'?encoding='UTF-8'
?>
<!
DOCTYPE?hibernate-configuration?PUBLIC
??????????"-//Hibernate/Hibernate?Configuration?DTD?2.0//EN"
??????????"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd"
>
<!--
?Generated?by?MyEclipse?Hibernate?Tools.???????????????????
-->
<
hibernate-configuration
>
<
session-factory
>
????
<
property?
name
="dialect"
>
org.hibernate.dialect.SQLServerDialect
</
property
>
????
<
property?
name
="connection.url"
>
jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=login
</
property
>
????
<
property?
name
="connection.username"
>
lzqdiy
</
property
>
????
<
property?
name
="connection.password"
>
lzqdiy
</
property
>
????
<
property?
name
="connection.driver_class"
>
com.microsoft.jdbc.sqlserver.SQLServerDriver
</
property
>
????
<
property?
name
="myeclipse.connection.profile"
>
conn2
</
property
>
????
<
mapping?
resource
="model/model.hbm.xml"
?
/>
</
session-factory
>
</
hibernate-configuration
>
HibernateTest
import
?java.util.Iterator;
import
?java.util.List;
import
?org.hibernate.HibernateException;
import
?org.hibernate.Query;
import
?org.hibernate.Session;
import
?org.hibernate.Transaction;
import
?model.User;
import
?db.HibernateUtil;
public
?
class
?HibernateTest
{
????
/**?*/
/**
?????*?
@param
?args
?????*?
@throws
?HibernateException
?????
*/
????
public
?
void
?insertUser()?
????
{
????????Session?session?
=
?HibernateUtil.getCurrentSession();
????????Transaction?tx?
=
?session.beginTransaction();
????????User?user?
=
?
new
?User();
????????user.setUsername(
"
lzqdiy
"
);
????????user.setPassword(
"
lzqdiy
"
);
????????session.save(user);
????????tx.commit();
????????HibernateUtil.closeCurrentSession();
????????System.out.println(
"
新增成功!
"
);
????}
????
public
?List?getUsers()?
????
{
????????Session?session?
=
?HibernateUtil.getCurrentSession();
????????Transaction?tx?
=
?session.beginTransaction();
????????String?hql?
=
?
"
select?new?User(password)?from?model.User??where?username=:name
"
;
????????Query?query?
=
?session.createQuery(hql);
????????query.setString(
"
name
"
,?
"
lzqdiy
"
);
????????List?list?
=
?query.list();
????????tx.commit();
????????HibernateUtil.closeCurrentSession();
????????
return
?list;
????}
????
public
?
void
?updateUsers()?
????
{
????????Session?session?
=
?HibernateUtil.getCurrentSession();
????????Transaction?tx?
=
?session.beginTransaction();
????????String?hql?
=
?
"
update??User??set?password='gm'?
"
;
????????Query?query?
=
?session.createQuery(hql);
????????query.executeUpdate();
????????tx.commit();
????????HibernateUtil.closeCurrentSession();
????????System.out.println(
"
更新成功!
"
);
????}
????
public
?
void
?deleteUsers()?
????
{
????????Session?session?
=
?HibernateUtil.getCurrentSession();
????????Transaction?tx?
=
?session.beginTransaction();
????????String?hql?
=
?
"
delete??User??where?id=1
"
;
????????Query?query?
=
?session.createQuery(hql);
????????query.executeUpdate();
????????tx.commit();
????????HibernateUtil.closeCurrentSession();
????????System.out.println(
"
刪除成功!
"
);
????}
????
public
?
static
?
void
?main(String[]?args)?
????
{
????????
//
?TODO?Auto-generated?method?stub
????????
new
?HibernateTest().insertUser();
????????
new
?HibernateTest().updateUsers();
????????
new
?HibernateTest().deleteUsers();
????????List?list?
=
?
new
?HibernateTest().getUsers();
????????
for
?(Iterator?iter?
=
?list.iterator();?iter.hasNext();)
????????
{
????????????User?user?
=
?(User)?iter.next();
????????????System.out.println(user.getPassword());
????????}
????}
}
其中使用的hql語句比較初級,大家不要見笑,以后我將做深入的研究。
補充:其實對于更新和刪除并不需要使用hql就可以實現。
將HibernateTest更改如下,同樣可以實現更新和刪除。
import
?java.util.Iterator;
import
?java.util.List;
import
?org.hibernate.HibernateException;
import
?org.hibernate.Query;
import
?org.hibernate.Session;
import
?org.hibernate.Transaction;
import
?model.User;
import
?db.HibernateUtil;
public
?
class
?HibernateTest
{
????
/**?*/
/**
?????*?
@param
?args
?????*?
@throws
?HibernateException
?????
*/
????
public
?
void
?insertUser()?
????
{
????????Session?session?
=
?HibernateUtil.getCurrentSession();
????????Transaction?tx?
=
?session.beginTransaction();
????????User?user?
=
?
new
?User();
????????user.setUsername(
"
lzqdiy
"
);
????????user.setPassword(
"
lzqdiy
"
);
????????session.save(user);
????????tx.commit();
????????HibernateUtil.closeCurrentSession();
????????System.out.println(
"
新增成功!
"
);
????}
????
public
?List?getUsers()?
????
{
????????Session?session?
=
?HibernateUtil.getCurrentSession();
????????Transaction?tx?
=
?session.beginTransaction();
????????String?hql?
=
?
"
select?new?User(password)?from?model.User??where?username=:name
"
;
????????Query?query?
=
?session.createQuery(hql);
????????query.setString(
"
name
"
,?
"
lzqdiy
"
);
????????List?list?
=
?query.list();
????????tx.commit();
????????HibernateUtil.closeCurrentSession();
????????
return
?list;
????}
????
public
?
void
?updateUsers()?
????
{
????????Session?session?
=
?HibernateUtil.getCurrentSession();
????????Transaction?tx?
=
?session.beginTransaction();
????????User?user
=
new
?User();
????????user.setId(
new
?Integer(
1
));
????????user.setUsername(
"
lzqdiy
"
);
????????user.setPassword(
"
gm
"
);
????????session.saveOrUpdate(user);
????????tx.commit();
????????HibernateUtil.closeCurrentSession();
????????System.out.println(
"
更新成功!
"
);
????}
????
public
?
void
?deleteUsers()?
????
{
????????Session?session?
=
?HibernateUtil.getCurrentSession();
????????Transaction?tx?
=
?session.beginTransaction();
????????User?user
=
new
?User();
????????user.setId(
new
?Integer(
1
));
????????session.delete(user);
????????tx.commit();
????????HibernateUtil.closeCurrentSession();
????????System.out.println(
"
刪除成功!
"
);
????}
????
public
?
static
?
void
?main(String[]?args)?
????
{
????????
//
?TODO?Auto-generated?method?stub
????????
new
?HibernateTest().insertUser();
????????
new
?HibernateTest().updateUsers();
????????
new
?HibernateTest().deleteUsers();
????????List?list?
=
?
new
?HibernateTest().getUsers();
????????
for
?(Iterator?iter?
=
?list.iterator();?iter.hasNext();)
????????
{
????????????User?user?
=
?(User)?iter.next();
????????????System.out.println(user.getPassword());
????????}
????}
}
posted on 2007-06-10 21:43
EricWong
閱讀(2258)
評論(1)
編輯
收藏
評論:
#
re: 用hibernate3實現增刪改查 [未登錄] 2007-08-09 15:36 |
haha
頂
回復
更多評論
#
re: 用hibernate3實現增刪改查 2007-09-06 16:15 |
吳楊明
好 在學hibernate 應用
謝謝啦 我的博客 m.tkk7.com/coacoa2008
大家一起進步
回復
更多評論
#
re: 用hibernate3實現增刪改查
2007-09-06 16:56 |
吳楊明
"
http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
用hibernate3實現增刪改查
"
http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
回復
更多評論
新用戶注冊
刷新評論列表
只有注冊用戶
登錄
后才能發表評論。
網站導航:
博客園
IT新聞
Chat2DB
C++博客
博問
管理
進入生活博客
印度漂泊
<
2007年6月
>
日
一
二
三
四
五
六
27
28
29
30
31
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
1
2
3
4
5
6
7
留言簿
(44)
給我留言
查看公開留言
查看私人留言
隨筆分類
(120)
Assembly(1)
C&C++ (9)
Hibernate
J2EE(1)
Java(62)
JavaScript
Linux(5)
others(38)
Spring
Sql server(4)
Struts
相冊
人在印度
我愛的車啊
明星相冊
雜7雜8的
收藏夾
(15)
car(1)
english(2)
others(10)
technology(2)
links
flox's blog
linuxforum
Pegy的午夜場
Rock王霏
中印留學生論壇
值得一看的
像"瘋顛婆"一樣的記者
室內設計的一個小才女
我以前的CSDN'S blog
我大學老師的小窩
段紹譯老師
真的是美人如畫
英語聽力狂人
財經高人
搜索
最新評論
1.?re: Java API中文版下載
怎么下載呢?怎么沒有反應
--何偉杰
2.?re: 基于java的權限控制系統設計
評論內容較長,點擊標題查看
--zuidaima
3.?re: 很形象的接口的使用——針對初學者
@中華乞丐
呵呵,通俗易懂,真不錯!
--sir
4.?re: 很形象的接口的使用——針對初學者 [未登錄]
碉堡了
--張
5.?re: Java API中文版下載
非常詳細,全面
--彭勝波
6.?re: 基于java的權限控制系統設計
抱著學習的心理來看,結果越看越看不懂。。。
--zhangyibin8111
7.?re: Java API中文版下載
kankan
--look
8.?re: Java API中文版下載
個級別、你看了就
--猜猜
9.?re: Java API中文版下載
不錯
--張晨
10.?re: Java API中文版下載
不知道
--不知道
閱讀排行榜
1.?Java API中文版下載 (70293)
2.?ERROR 2003: Can't connect to MySQL server on 'localhost' (10061)解決方法(15934)
3.?WAP瀏覽器(模擬器)大全(13777)
4.?msn登錄不了錯誤代碼80048820擴展錯誤代碼 80048412(10704)
5.?基于java的權限控制系統設計(8768)
Powered by:
博客園
模板提供:
滬江博客
Copyright ©2025 EricWong
主站蜘蛛池模板:
久草福利资源网站免费
|
h视频在线免费看
|
亚洲国产综合专区在线电影
|
国产自国产自愉自愉免费24区
|
亚洲天堂中文字幕
|
日韩免费观看视频
|
国产自国产自愉自愉免费24区
|
老司机福利在线免费观看
|
伊人久久亚洲综合
|
免费99精品国产自在现线
|
黄页网址在线免费观看
|
亚洲国产精品人久久
|
国产精品免费看香蕉
|
久久久久久免费一区二区三区
|
亚洲综合成人婷婷五月网址
|
亚洲欧洲一区二区三区
|
午夜国产精品免费观看
|
中文无码日韩欧免费视频
|
亚洲欧洲另类春色校园网站
|
亚洲中文字幕不卡无码
|
午夜dj在线观看免费视频
|
爱丫爱丫影院在线观看免费
|
国产成人精品日本亚洲语音
|
337p日本欧洲亚洲大胆艺术
|
亚洲av无码专区在线观看素人
|
亚洲视频在线免费看
|
国产精品黄页免费高清在线观看
|
久久永久免费人妻精品
|
亚洲av无码专区青青草原
|
亚洲理论片在线观看
|
永久亚洲成a人片777777
|
永久黄网站色视频免费观看
|
中文字幕在线免费观看
|
美女巨胸喷奶水视频www免费
|
亚洲精品无码aⅴ中文字幕蜜桃
|
亚洲国产一区国产亚洲
|
亚洲精品成人a在线观看
|
国产网站在线免费观看
|
无码日韩人妻av一区免费
|
99在线免费观看视频
|
一个人看的www免费视频在线观看
|