写在前面:IBatisNet是在.NET下使用IBatis的开源项目,一般用了ORM映射,它的功能很强大,可以较好地实现对象与数据之间的映射,简单的说就是使用IBatisNet格式的映射文件,把该定的sql保存在配置中,调用时返回对象。IBatis是java项目中使用得很多,不过在.NET中使用得不是很多。下面介绍到调用Oracle存储过程返回数据集的信息。
为什么.NET中IBatisNET使用得还不多,至少没有在Java平台那么流行,笔者窃以为是.NET或Windows平台下的东西实在太简单好用,太人性化,令开发员也变得傻瓜化,对于配置太多的东西,实在不好处理,也不知道怎处理。而Java平台上,连安装个IDE都配置都晕,那使用IBatis有什么难度呢?呵呵。配置性的东西肯定是功能灵活,不过所有问题都有对立面的,功能灵活但是很不直观。
1, Oracle的存储过程返回数据集是要使用Package的
定义一个Package也不是一个简单的事,网上找一个例子,有时候也不容易。
下面提供一个网址,介绍Package返回数据集
http://www.enet.com.cn/article/2007/1221/A20071221962461.shtml
实质上Package使用了Cursor来返回数据集。
2, IBatisNET的配置
下面是IBatisNet配置,Oracle的存储过程返回数据集。
<?xml version="1.0" encoding="UTF-8"?>
<sqlMap namespace="Mynamespace" xmlns="http://ibatis.apache.org/mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<!--重命名类-->
<alias>
<typeAlias alias="MyTestEntity" type="Mynamespace.MyTestEntity,Mynamespace" />
</alias>
<parameterMaps>
<!--设置存储过程的输入输出-->
<parameterMap id="GetMyProcParam" >
<parameter property="param1" column="ID" dbType="Int32" type="int" direction="Input"/>
<!--设置返回的数据,数据库类型为Cursor-->
<parameter property="result" column="RETURNTABLE" dbType="Cursor" direction="Output"/>
</parameterMap>
</parameterMaps>
<resultMaps>
<!--返回的结果类-->
<resultMap id="MyTestEntityMap" class="MyTestEntity" >
<result property="TestId" column="TestId" />
<result property="TestName" column="TestName" />
</resultMap>
</resultMaps>
<statements>
<!--设置存储过程-->
<procedure id="GetMyProc" parameterMap="GetMyProcParam" resultMap="MyTestEntityMap">
GetMyProc
</procedure>
</statements>
</sqlMap>
<sqlMap namespace="Mynamespace" xmlns="http://ibatis.apache.org/mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<!--重命名类-->
<alias>
<typeAlias alias="MyTestEntity" type="Mynamespace.MyTestEntity,Mynamespace" />
</alias>
<parameterMaps>
<!--设置存储过程的输入输出-->
<parameterMap id="GetMyProcParam" >
<parameter property="param1" column="ID" dbType="Int32" type="int" direction="Input"/>
<!--设置返回的数据,数据库类型为Cursor-->
<parameter property="result" column="RETURNTABLE" dbType="Cursor" direction="Output"/>
</parameterMap>
</parameterMaps>
<resultMaps>
<!--返回的结果类-->
<resultMap id="MyTestEntityMap" class="MyTestEntity" >
<result property="TestId" column="TestId" />
<result property="TestName" column="TestName" />
</resultMap>
</resultMaps>
<statements>
<!--设置存储过程-->
<procedure id="GetMyProc" parameterMap="GetMyProcParam" resultMap="MyTestEntityMap">
GetMyProc
</procedure>
</statements>
</sqlMap>
上面的配置是根据实际情况修改出来的,不排除有部分的错误,如果是人工写上面的文件,很容易会让人发狂的。这也是之前说的,很容易就出错了。
出处:小作坊网Chakman
添加到百度搜藏
添加到雅虎收藏