杂家
学习复习
|
首页
|
发新随笔
|
发新文章
|
联系
|
聚合
|
管理
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
淘声依旧
阅读(617)
评论(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)
实用连接
天堂露珠
我的心情
西安信息资源网
积分与排名
积分 - 95657
排名 - 602
最新评论
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获取各种常用时间方法
很好很强大
--`万物皆对象`
hits
Casino