雜家
學習復習
|
首頁
|
發新隨筆
|
發新文章
|
聯系
|
聚合
|
管理
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手机在线观看
|
国产精彩免费视频
|
久久成人永久免费播放
|
一区二区三区免费视频播放器
|
狠狠热精品免费观看
|
免费人成又黄又爽的视频在线电影
|
国产精品亚洲专区无码唯爱网
|
久久亚洲精品11p
|
亚洲av无码专区青青草原
|
欧洲亚洲综合一区二区三区
|
啦啦啦www免费视频
|
成人免费无码大片a毛片软件
|
成年在线观看免费人视频草莓
|
在线免费观看污网站
|
日本免费一本天堂在线
|
国产一级高清免费观看
|
亚洲区不卡顿区在线观看
|
亚洲色精品88色婷婷七月丁香
|
亚洲va中文字幕无码久久不卡
|
亚洲一区二区三区电影
|
亚洲免费二区三区
|
亚洲精品无码日韩国产不卡av
|
羞羞漫画在线成人漫画阅读免费
|
一级女性全黄久久生活片免费
|
青柠影视在线观看免费
|
国产精品成人免费福利
|
国产精品无码一二区免费
|
久久精品国产亚洲精品
|
亚洲AV天天做在线观看
|
精品亚洲成在人线AV无码
|
国产精品亚洲精品日韩电影
|
a级毛片视频免费观看
|
精品国产免费人成电影在线观看
|
一级毛片人与动免费观看
|
精品免费tv久久久久久久
|
免费大片黄在线观看yw
|
日本免费人成黄页在线观看视频
|
中文字幕在线亚洲精品
|
亚洲无限乱码一二三四区
|
最新亚洲人成无码网www电影
|
久久久久免费视频
|