准备工作:
1.hibernate3
到这下载hibernate3:http://sourceforge.net/project/showfiles.phpgroup_id=40712&package_id=127784&release_id=403223
2.mysql (注意一定要用mysql5.0和最新驱动)
mysql官方网站http://www.mysql.com/
1
.建张表
CREATE
TABLE
`proctest` (
`id`
int
(
11
)
NOT
NULL
auto_increment,
`Name`
varchar
(
20
)
default
''''''
,
`age`
int
(
11
)
default
NULL
,
`address`
varchar
(
50
)
default
''
,
PRIMARY
KEY
(`id`)
) ENGINE
=
InnoDB
DEFAULT
CHARSET
=
gb2312;
插入几条记录
INSERT
INTO
`proctest`
VALUES
(
1
,
'
tom
'
,
22
,
'http://www.blogjava.net
'
);
INSERT
INTO
`proctest`
VALUES
(
2
,
'
wujun
'
,
25
,
'http://www.blogjava.net/wujun
'
);
INSERT
INTO
`proctest`
VALUES
(
3
,
'
jerry
'
,
30
,
'
深圳
'
);
INSERT
INTO
`proctest`
VALUES
(
4
,
'
wujun
'
,
40
,
'
南昌
'
);
创建存储过程
--
这只是一个例子,就来个简单存储过程
create
PROCEDURE
testProc()
begin
select
*
from
proctest;
end
;
打开eclipce新建个java工程,记的把hiberbate3类库也一起加进去..
看下结构图:
1.新建UserVO.java文件
package
net.wj.proc.vo;
public
class
UserVO
{
private
int
id;
private
String name;
private
int
age;
private
String address;
public
UserVO()
{}
顺便把它相对应的配置文件也写上。。
UserVO.hbm.xml
<?xml version="1.0" encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<!-- DO NOT EDIT: This is a generated file that is synchronized -->
<!-- by MyEclipse Hibernate tool integration. -->
<!-- Created Sat Apr 22 17:08:56 CST 2006 -->
<hibernate-mapping>
<class name="net.wj.proc.vo.UserVO" table="proctest">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="name" column="name" type="string" />
<property name="age" column="age" type="integer" />
<property name="address" column="address" type="string" />
</class>
<!--sql查询-->
<sql-query name="select">
<![CDATA[select {usr.*} from proctest usr ]]>
<return alias="usr" class="net.wj.proc.vo.UserVO" />
</sql-query>
<!--调用存储过程就在这里配了-->
<sql-query name="getUser" callable="true">
<return alias="user" class="net.wj.proc.vo.UserVO">
<return-property name="id" column="id" />
<return-property name="name" column="name" />
<return-property name="age" column="age" />
<return-property name="address" column="address" />
</return>
<!--这里就是我们刚刚创建的存储过程名-->
{call testProc()}
</sql-query>
</hibernate-mapping>
测试代码
package net.wj.proc.test;
import java.util.List;
import org.hibernate.*;
import org.hibernate.cfg.*;
import net.wj.proc.vo.*;
import org.apache.log4j.*;
public class ProcTest {
/** *//**
* @param args
*/
Logger log=Logger.getLogger(this.getClass());
public ProcTest()
{}
public static void main(String[] args) {
System.out.print("start.............................");
ProcTest tt=new ProcTest();
// tt.LoadAll();
// tt.ExampleSelect();
tt.ExampleProc();
}
//得到Session,
public Session getSession()
{
try
{
Configuration cfg = new Configuration().configure();
SessionFactory sf=cfg.buildSessionFactory();
Session ss= sf.openSession();
return ss;
}
catch(Exception ee)
{
System.out.print("失败"+ee.getMessage());
return null;
}
}
//这里调我们在UserVO.hbm.xml
//sql-query 写上的name属性getUser
public void ExampleProc()
{
Session ss=this.getSession();
List li=ss.getNamedQuery("getUser").list();
for(int i=0;i<li.size();i++)
{
UserVO vo=(UserVO)li.get(i);
log.info("name:"+vo.getName());
log.info("age"+vo.getAge());
log.info("address"+vo.getAddress());
}
ss.close();
}
//配置文件的sql查询
public void ExampleSelect()
{
Session ss=this.getSession();
List li= ss.getNamedQuery("select").list();
for(int i=0;i<li.size();i++)
{
UserVO vo=(UserVO)li.get(i);
log.info("name:"+vo.getName());
log.info("age"+vo.getAge());
log.info("address"+vo.getAddress());
}
ss.close();
}
} 记的用最新的驱动:
要不然可能会报这个错
Exception in thread "main" org.hibernate.exception.GenericJDBCException: could not execute query
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:91)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:79)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.loader.Loader.doList(Loader.java:2148)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2029)
at org.hibernate.loader.Loader.list(Loader.java:2024)
at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:111)
at org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:1674)
at org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:147)
at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:164)
at net.wj.proc.test.ProcTest.ExampleProc(ProcTest.java:45)
at net.wj.proc.test.ProcTest.main(ProcTest.java:22)
Caused by: java.sql.SQLException: Callable statments not supported.
at com.mysql.jdbc.Connection.prepareCall(Connection.java:1278)
at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:439)
at org.hibernate.jdbc.AbstractBatcher.prepareCallableQueryStatement(AbstractBatcher.java:115)
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1557)
at org.hibernate.loader.Loader.doQuery(Loader.java:661)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
at org.hibernate.loader.Loader.doList(Loader.java:2145)
8 more
09:38:18,837 INFO SessionFactoryImpl:153 - building session factory
09:38:18,917 WARN Configurator:126 - No configuration found. Configuring ehcache from ehcache-failsafe.xml found in the classpath: jar:file:/E:/lib/hibernate3/ehcache-1.1.jar!/ehcache-failsafe.xml
09:38:21,951 INFO SessionFactoryObjectFactory:82 - Not binding factory to JNDI, no JNDI name configured
Hibernate: {call testProc()}
09:38:22,482 WARN JDBCExceptionReporter:71 - SQL Error: 0, SQLState: S1C00
09:38:22,482 ERROR JDBCExceptionReporter:72 - Callable statments not supported.
源代码
http://www.blogjava.net/Files/wujun/Proc.rar是不是挺简单的.