雜家
學習復習
|
首頁
|
發新隨筆
|
發新文章
|
聯系
|
聚合
|
管理
spring+hibernate代碼的基本內容
可以執行的最小內容
1.pojo-User.java
package
com.xzl.pojo;
import
java.util.Date;
/** */
/**
* @hibernate.class table="jordan"
*/
public
class
User
{
public
Integer id;
public
String name;
public
Integer age;
public
Date birth;
public
String comment;
/** */
/**
* @hibernate.
*/
public
Integer getAge()
{
return
age;
}
/** */
/**
*
@param
age the age to set
*/
public
void
setAge(Integer age)
{
this
.age
=
age;
}
/** */
/**
*
@return
the birth
*/
public
Date getBirth()
{
return
birth;
}
/** */
/**
*
@param
birth the birth to set
*/
public
void
setBirth(Date birth)
{
this
.birth
=
birth;
}
/** */
/**
*
@return
the comment
*/
public
String getComment()
{
return
comment;
}
/** */
/**
*
@param
comment the comment to set
*/
public
void
setComment(String comment)
{
this
.comment
=
comment;
}
/** */
/**
*
@return
the id
*/
public
Integer getId()
{
return
id;
}
/** */
/**
*
@param
id the id to set
*/
public
void
setId(Integer id)
{
this
.id
=
id;
}
/** */
/**
*
@return
the name
*/
public
String getName()
{
return
name;
}
/** */
/**
*
@param
name the name to set
*/
public
void
setName(String name)
{
this
.name
=
name;
}
}
2.映射文件User.hbm.xml
<?
xml version="1.0"
?>
<!
DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"
>
<
hibernate-mapping
>
<!--
Created by the Middlegen Hibernate plugin 2.1
http://boss.bekk.no/boss/middlegen/
http://www.hibernate.org/
-->
<
class
name
="com.xzl.pojo.User"
table
="jordan"
>
<
meta
attribute
="class-description"
inherit
="false"
>
@hibernate.class
table="jordan"
</
meta
>
<
id
name
="id"
type
="java.lang.Integer"
column
="id"
>
<
meta
attribute
="field-description"
>
@hibernate.id
generator-class="assigned"
type="java.lang.Object"
column="id"
</
meta
>
<
generator
class
="native"
/>
</
id
>
<
property
name
="name"
type
="java.lang.String"
column
="name"
not-null
="true"
length
="10"
>
<
meta
attribute
="field-description"
>
@hibernate.property
column="name"
length="10"
not-null="true"
</
meta
>
</
property
>
<
property
name
="age"
type
="java.lang.Integer"
column
="age"
not-null
="true"
length
="10"
>
<
meta
attribute
="field-description"
>
@hibernate.property
column="age"
length="10"
not-null="true"
</
meta
>
</
property
>
<
property
name
="birth"
type
="java.sql.Timestamp"
column
="birth"
not-null
="true"
length
="19"
>
<
meta
attribute
="field-description"
>
@hibernate.property
column="birth"
length="19"
not-null="true"
</
meta
>
</
property
>
<
property
name
="comment"
type
="java.lang.String"
column
="comment"
not-null
="true"
length
="100"
>
<
meta
attribute
="field-description"
>
@hibernate.property
column="comment"
length="100"
not-null="true"
</
meta
>
</
property
>
<!--
Associations
-->
</
class
>
</
hibernate-mapping
>
3.DAO接口IUserDAO.java
package
com.xzl.star;
import
com.xzl.pojo.User;
public
interface
IUserDAO
{
public
User doQuery(java.lang.Integer id);
}
4.實現UserDAO繼承HibernateDaoSupport類,調用模板getHibernateTemplate
package
com.xzl.star;
import
org.apache.log4j.Logger;
import
org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import
com.xzl.pojo.User;
public
class
UserDAO
extends
HibernateDaoSupport
implements
IUserDAO
{
private
Logger logger
=
Logger.getLogger(
this
.getClass().getName());
public
User doQuery( java.lang.Integer id)
{
//
TODO Auto-generated method stub
logger.info(
"
進入doQuery方法
"
);
try
{
return
(User)getHibernateTemplate().get(User.
class
,id);
}
catch
(Exception e)
{
logger.info(
"
拋出異常
"
+
e.getMessage());
}
finally
{
logger.info(
"
doQuery方法完成
"
);
}
return
null
;
}
}
5.測試
package
com.xzl.star;
import
org.springframework.context.ApplicationContext;
import
org.springframework.context.support.FileSystemXmlApplicationContext;
import
org.hibernate.dialect.MySQL5Dialect;
public
class
TestMain
{
/** */
/**
*
@param
args
*/
public
static
void
main(String[] args)
{
//
TODO Auto-generated method stub
ApplicationContext ctx
=
new
FileSystemXmlApplicationContext(
"
src/hibernate-Context.xml
"
);
IUserDAO userDao
=
(IUserDAO)ctx.getBean(
"
userDAOProxy
"
);
System.out.println(userDao.doQuery(
1
).getName());
System.out.println(userDao.doQuery(
3
));
}
}
6.hibernate-Context.xml配置所有。。。
<?
xml version="1.0" encoding="UTF-8"
?>
<!
DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"
>
<
beans
>
<
bean
id
="dataSource"
class
="org.apache.commons.dbcp.BasicDataSource"
destroy-method
="close"
>
<
property
name
="driverClassName"
>
<
value
>
org.gjt.mm.mysql.Driver
</
value
>
</
property
>
<
property
name
="url"
>
<
value
>
jdbc:mysql://localhost:3306/SPORT
</
value
>
</
property
>
<
property
name
="username"
>
<
value
>
root
</
value
>
</
property
>
<
property
name
="password"
>
<
value
></
value
>
</
property
>
</
bean
>
<
bean
id
="sessionFactory"
class
="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
>
<
property
name
="dataSource"
>
<
ref
local
="dataSource"
/>
</
property
>
<
property
name
="mappingResources"
>
<
list
>
<
value
>
com/xzl/pojo/User.hbm.xml
</
value
>
</
list
>
</
property
>
<
property
name
="hibernateProperties"
>
<
props
>
<
prop
key
="hibernate.dialect"
>
org.hibernate.dialect.MySQL5Dialect
</
prop
>
<
prop
key
="hibernate.show_sql"
>
true
</
prop
>
</
props
>
</
property
>
</
bean
>
<
bean
id
="transactionManager"
class
="org.springframework.orm.hibernate3.HibernateTransactionManager"
>
<
property
name
="sessionFactory"
>
<
ref
local
="sessionFactory"
/>
</
property
>
</
bean
>
<
bean
id
="userDAO"
class
="com.xzl.star.UserDAO"
>
<
property
name
="sessionFactory"
>
<
ref
local
="sessionFactory"
/>
</
property
>
</
bean
>
<
bean
id
="userDAOProxy"
class
="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
>
<
property
name
="transactionManager"
>
<
ref
bean
="transactionManager"
/>
</
property
>
<
property
name
="target"
>
<
ref
local
="userDAO"
/>
</
property
>
<
property
name
="transactionAttributes"
>
<
props
>
<
prop
key
="do*"
>
PROPAGATION_REQUIRED,readOnly
</
prop
>
</
props
>
</
property
>
</
bean
>
</
beans
>
就結束了,自己留著用的!
ExtJS教程
-
Hibernate教程
-
Struts2 教程
-
Lucene教程
發表于 2007-04-19 22:25
淘聲依舊
閱讀(623)
評論(0)
編輯
收藏
所屬分類:
109.Spring
新用戶注冊
刷新評論列表
只有注冊用戶
登錄
后才能發表評論。
網站導航:
博客園
IT新聞
Chat2DB
C++博客
博問
管理
相關文章:
解決通過POST頁面提交后,顯示亂碼問題!
一種優雅的流行架構:Struts+Spring+Hibernate
spring+hibernate代碼的基本內容
Spring Quartz任務調度示例
web.xml樣板2.4的
*-servlet的Spring MVC樣板
Spring - PropertyPlaceholder
Spring MVC
Spring JavaMail的示例
<
2007年4月
>
日
一
二
三
四
五
六
25
26
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
公告
要冒一險!整個生命就是一場冒險,走得最遠的人常是愿意去做、愿意去冒險的人。
隨筆分類
(153)
100.Struts2(27)
(rss)
101.Compass
(rss)
102.ExtJS(23)
(rss)
103.Hibernate(25)
(rss)
104.Java(30)
(rss)
105.JavaScript(22)
(rss)
106.jBPM(1)
(rss)
107.Log4j(1)
(rss)
108.Lucene(13)
(rss)
109.Spring(9)
(rss)
110.Things(2)
(rss)
實用連接
天堂露珠
我的心情
西安信息資源網
積分與排名
積分 - 96700
排名 - 595
最新評論
1.?re: JSTL fmt:formatNumber 數字、貨幣格式化
非發放
--非發放
2.?re: JSTL fmt:formatNumber 數字、貨幣格式化
<fmt:formatNumber value="60000" pattern="#,#00#"/>
--非發放
3.?re: java圖片處理 (文字水印、圖片水印、縮放、補白)
好東西!
謝謝!
--rb
4.?re: JQuery1.2API中文文檔[未登錄]
hao
--123
5.?re: Java獲取各種常用時間方法
很好很強大
--`萬物皆對象`
Powered by:
博客園
模板提供:
滬江博客
Copyright ©2025 淘聲依舊
hits
Casino
主站蜘蛛池模板:
韩日电影在线播放免费版
|
美女被艹免费视频
|
亚洲国产精品免费视频
|
亚洲AV无码久久
|
七色永久性tv网站免费看
|
亚洲伊人久久大香线蕉综合图片
|
免费无码AV一区二区
|
亚洲精品视频免费观看
|
精品久久久久久国产免费了
|
精品久久香蕉国产线看观看亚洲
|
成全视成人免费观看在线看
|
亚洲AV无码一区二区乱孑伦AS
|
久久精品国产影库免费看
|
亚洲爆乳无码一区二区三区
|
久9这里精品免费视频
|
亚洲精品国产精品国自产网站
|
免费人成在线观看网站品爱网
|
亚洲成人在线电影
|
91精品成人免费国产片
|
亚洲国产日韩视频观看
|
免费人成网站在线播放
|
a国产成人免费视频
|
亚洲人成在线中文字幕
|
日本xxwwxxww在线视频免费
|
jizz日本免费
|
亚洲精品日韩中文字幕久久久
|
成人毛片18女人毛片免费
|
日韩大片免费观看视频播放
|
亚洲第一AV网站
|
丁香花免费完整高清观看
|
免费激情网站国产高清第一页
|
免费一级毛片无毒不卡
|
亚洲an日韩专区在线
|
亚洲国产精品成人网址天堂
|
无码国产精品一区二区免费3p
|
亚洲人成自拍网站在线观看
|
亚洲三区在线观看无套内射
|
啦啦啦高清视频在线观看免费
|
亚洲免费视频一区二区三区
|
亚洲日韩乱码久久久久久
|
亚洲国产精品无码久久青草
|