隨筆:15 文章:206 評論:35 引用:0
fenglin
創新、創意、挑戰
C++博客
首頁
發新隨筆
發新文章
聯系
聚合
管理
hibernate 學習筆記之二 Hello World
包括以下三個必須文件和一個測試用例文件:
Student.java
1
2
package
com.bebig.hibernate.model;
3
4
/** */
/**
5
*
@author
fenglin
6
* @filename Student.java
7
*/
8
public
class
Student
{
9
private
int
id;
10
11
public
int
getId()
{
12
return
id;
13
}
14
15
public
void
setId(
int
id)
{
16
this
.id
=
id;
17
}
18
19
public
String getName()
{
20
return
name;
21
}
22
23
public
void
setName(String name)
{
24
this
.name
=
name;
25
}
26
27
public
int
getAge()
{
28
return
age;
29
}
30
31
public
void
setAge(
int
age)
{
32
this
.age
=
age;
33
}
34
35
private
String name;
36
private
int
age;
37
38
}
39
Student.hbm.xml,保存到與上一個文件同一文件夾(推薦)
1
<!--
filename:Student.hbm.xml
-->
2
<?
xml version="1.0"
?>
3
<!
DOCTYPE hibernate-mapping PUBLIC
4
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
5
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
>
6
7
<
hibernate-mapping
package
="com.bebig.hibernate.model"
>
8
<
class
name
="Student"
>
9
<
id
name
="id"
>
10
<!--
<generator class="native" />
-->
11
</
id
>
12
<
property
name
="name"
></
property
>
13
<
property
name
="age"
></
property
>
14
</
class
>
15
</
hibernate-mapping
>
hibernate.cfg.xml,保存到與src文件夾里:
1
<!--
filename:hibernate.cfg.xml
-->
2
<?
xml version='1.0' encoding='utf-8'
?>
3
<!
DOCTYPE hibernate-configuration PUBLIC
4
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
5
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"
>
6
7
<
hibernate-configuration
>
8
9
<
session-factory
>
10
11
<!--
Database connection settings
-->
12
<
property
name
="connection.driver_class"
>
com.microsoft.sqlserver.jdbc.SQLServerDriver
</
property
>
13
<
property
name
="connection.url"
>
jdbc:sqlserver://localhost:1433;DatabaseName=hibernate
</
property
>
14
<
property
name
="connection.username"
>
sa
</
property
>
15
<
property
name
="connection.password"
>
********
</
property
>
16
17
<!--
JDBC connection pool (use the built-in)
-->
18
<!--
<property name="connection.pool_size">1</property>
-->
19
20
<!--
SQL dialect
-->
21
<
property
name
="dialect"
>
org.hibernate.dialect.SQLServerDialect
</
property
>
22
23
<!--
Enable Hibernate's automatic session context management
-->
24
<!--
<property name="current_session_context_class">thread</property>
-->
25
26
<!--
Disable the second-level cache
-->
27
<
property
name
="cache.provider_class"
>
org.hibernate.cache.NoCacheProvider
</
property
>
28
29
<!--
Echo all executed SQL to stdout
-->
30
<
property
name
="show_sql"
>
true
</
property
>
31
<
property
name
="format_sql"
>
true
</
property
>
32
33
<!--
Drop and re-create the database schema on startup
-->
34
<
property
name
="hbm2ddl.auto"
>
create
</
property
>
35
36
<
mapping
resource
="com/bebig/hibernate/model/Student.hbm.xml"
/>
37
38
</
session-factory
>
39
40
</
hibernate-configuration
>
測試用例:StudentTest.java
1
import
org.hibernate.Session;
2
import
org.hibernate.SessionFactory;
3
import
org.hibernate.cfg.Configuration;
4
5
import
com.bebig.hibernate.model.Student;
6
7
8
public
class
StudentTest
{
9
public
static
void
main(String[] args)
{
10
Student s
=
new
Student();
11
s.setId(
2
);
12
s.setName(
"
Don
"
);
13
s.setAge(
26
);
14
15
Configuration cfg
=
new
Configuration();
16
SessionFactory sf
=
cfg.configure().buildSessionFactory();
17
Session session
=
sf.openSession();
18
session.beginTransaction();
19
session.save(s);
20
session.getTransaction().commit();
21
22
session.close();
23
sf.close();
24
}
25
}
26
發表于 2010-09-22 15:57
風林
閱讀(100)
評論(0)
編輯
收藏
引用
所屬分類:
JAVA
、
Hibernate
只有注冊用戶
登錄
后才能發表評論。
【推薦】100%開源!大型工業跨平臺軟件C++源碼提供,建模,組態!
相關文章:
[轉]java編程中'為了性能'一些盡量做到的地方
JAVAC命令行錯誤
Java基礎_Collection接口下的子類存儲特性
Android_常用控件使用
Android_Activity&Intent&Layout
Integer自動裝箱、拆箱問題
自定義比較器、排序
JDBC操作實例
常用數據庫的JDBC連接代碼
[轉]J2SE_內部類
網站導航:
博客園
IT新聞
BlogJava
博問
Chat2DB
管理
CALENDER
<
2025年7月
>
日
一
二
三
四
五
六
29
30
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
1
2
3
4
5
6
7
8
9
常用鏈接
我的隨筆
我的評論
我參與的隨筆
留言簿
給我留言
查看公開留言
查看私人留言
隨筆分類
jBPM
(rss)
隨筆檔案
2011年7月 (1)
2011年4月 (1)
2011年2月 (1)
2010年11月 (1)
2010年10月 (2)
2010年9月 (3)
2010年8月 (4)
2010年7月 (2)
文章分類
AJAX(2)
(rss)
Android(2)
(rss)
C#(20)
(rss)
C++(6)
(rss)
ckeditor&ckfinder(1)
(rss)
CSS
(rss)
Delphi(2)
(rss)
Hibernate(39)
(rss)
JAVA(95)
(rss)
jQuery(1)
(rss)
JSP(9)
(rss)
Maven(1)
(rss)
MySQL(4)
(rss)
OOP(1)
(rss)
Python(42)
(rss)
Spring(31)
(rss)
SQL Server(4)
(rss)
Struts2(35)
(rss)
SVN(1)
(rss)
Tomcat(1)
(rss)
Ubuntu(1)
(rss)
軟件加解密技術
(rss)
雜文(1)
(rss)
文章檔案
2011年8月 (1)
2011年7月 (3)
2011年6月 (19)
2011年5月 (2)
2011年4月 (1)
2011年2月 (1)
2010年12月 (2)
2010年11月 (21)
2010年10月 (67)
2010年9月 (48)
2010年8月 (37)
2010年7月 (4)
新聞檔案
2010年7月 (1)
相冊
CSS
Hibernate
搜索
最新評論
1.?re: Struts2_三種傳參數方法
方式的發生
--阿飛史蒂夫
2.?re: 在Win7上搭建JSP開發環境
評論內容較長,點擊標題查看
--鄒
3.?re: ckeditor&ckfinder&s2sh集成
評論內容較長,點擊標題查看
--庸幾何
4.?re: 在Win7上搭建JSP開發環境
下個 myeclipse@lou
--孫毅
5.?re: 在Win7上搭建JSP開發環境
@lou
運行 -cmd 找到startup.bat 在java 環境中運行
--孫毅
閱讀排行榜
1.?開始找Java開發類工作了(510)
2.?給一家公司的軟件做加密方案(443)
3.?一流、二流、三流(421)
4.?周一到周五都得出差,周末才能回(420)
5.?從WIN32開發轉型到WEB開發(381)
評論排行榜
1.?周一到周五都得出差,周末才能回(2)
2.?給力2011(2)
3.?最近工作有點忙(0)
4.?生活(0)
5.?在博客園開博了(0)
Powered By:
博客園
模板提供
:
滬江博客
久久夜色精品国产亚洲av
|
久久亚洲私人国产精品
|
国产精品亚洲美女久久久
|
久久综合欧美成人
|
日批日出水久久亚洲精品tv
|
亚洲国产精品高清久久久
|
好属妞这里只有精品久久
|
久久久久久av无码免费看大片
|
国产香蕉久久精品综合网
|
久久久国产精品亚洲一区
|
久久国产精品免费一区二区三区
|
亚洲国产精品综合久久网络
|
精品免费久久久久久久
|
久久伊人中文无码
|
99久久免费国产特黄
|
中文字幕精品无码久久久久久3D日动漫
|
精品久久久中文字幕人妻
|
久久亚洲高清综合
|
日本免费久久久久久久网站
|
伊人久久大香线蕉亚洲
|
久久免费香蕉视频
|
国产呻吟久久久久久久92
|
久久精品国产99久久无毒不卡
|
婷婷久久精品国产
|
久久综合视频网站
|
国产亚洲成人久久
|
热99re久久国超精品首页
|
蜜臀av性久久久久蜜臀aⅴ
|
亚洲午夜精品久久久久久浪潮
|
99久久精品国产综合一区
|
欧美亚洲国产精品久久蜜芽
|
久久精品国产亚洲欧美
|
国产欧美久久久精品影院
|
久久久久亚洲?V成人无码
|
久久久久国产视频电影
|
精品国产91久久久久久久a
|
国产成人无码精品久久久免费
|
精品无码久久久久久尤物
|
久久久无码精品亚洲日韩蜜臀浪潮
|
亚洲精品无码久久久
|
久久精品国产日本波多野结衣
|