Batisnet这个开源框架在类对象和数据库对象之间建立起一种映射和反射的关系,也就是通常所说的ORM,严格来说,应该是类和SQL查询语句之间的映射。与nHibernate相比,他的ORM特性并不彻底。但正是这种不彻底,才更为我所能接受,nhibernate封装了HQL语法来替代标准的SQL,提供了很多类来完成或者描述数据之间的关系,以前,这些都是在数据库设计层,利用数据库的约束等来实现关系。nHibernate的做法更倾向于把数据关系和逻辑放在应用层和OO中,而数据库的角色仅仅是单纯的数据存储,关系和约束以及逻辑都不在数据库表设计阶段出现。而是在设计XML配置文件的时候进行。这与以前的应用系统做法相差较大,而且HQL在很多时候不能取代SQL,其广泛程度更是不能相比。性能问题,以及大量的配置文件始终都是避免不了的问题。。。。
BatisNet-执行存储过程
映射xml文件书写如下示例
<?xml version="1.0" encoding="utf-8" ?>
<sqlMap namespace="Member" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="SqlMap.xsd">
<resultMaps>
<resultMap id="SelectResult" class="PlatAdmin.Model.Member">
<result property="Id" column="id" />
<result property="Identityno" column="identityno" />
<result property="Telephone" column="telephone" />
<result property="Email" column="email" />
<result property="Linktel" column="linktel" />
<result property="Address" column="address" />
<result property="Content" column="content" />
<result property="Username" column="username" />
<result property="Password" column="password" />
<result property="Truename" column="truename" />
<result property="Enable" column="enable" />
<result property="Regdate" column="regdate" />
</resultMap>
</resultMaps>
<parameterMaps>
<parameterMap id="swapParas" class="PlatAdmin.Model.Member">
<parameter property="querystr" column="" />
<parameter property="keyfield" column="" />
<parameter property="pagesize" column="" />
<parameter property="pagenumber" column="" />
</parameterMap>
</parameterMaps>
<statements>
<procedure id="GetMemberList" parameterMap="swapParas" resultMap="SelectResult">
usp_GetRecordset
</procedure>
</statements>
</sqlMap>
public IList GetMemberList(string querystr,int pageNo)
{
Hashtable ht = new Hashtable();
ht.Add("querystr",querystr);
ht.Add("keyfield","id");
ht.Add("pagesize",2);
ht.Add("pagenumber",pageNo);
SqlMapper sqlMap = IBatisNet.DataMapper.Mapper.Instance();
try
{
return sqlMap.QueryForList("GetMemberList",ht);
}
catch(Exception e)
{
throw new IBatisNetException(e.Message,e);
}
}
我遇到的问题:
1.在配置和运行的时候出现如下错误
Invalid SqlMap.config document. cause :Cannot load schema for the namespace '' - Could not find file "C:/WINDOWS/system32/SqlMapConfig.xsd"
原因:IBatisNet的配置文件的格式采用了标准的XML的命名空间:
解决方法:
更改provider.config,sqlmap.config
原来的providers.config 文件
<?xml version="1.0" encoding="utf-8" ?>
<providers>
<clear/>
<provider
……
</providers>
更改为
<?xml version="1.0" encoding="utf-8"?>
<providers xmlns="http://ibatis.apache.org/providers" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<clear/>
<provider
……
</providers>
原来的SqlMap.config文件
<?xml version="1.0" encoding="utf-8"?>
<sqlMapConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="SqlMapConfig.xsd">
……
</sqlMapConfig>
更改为
<?xml version="1.0" encoding="utf-8"?>
<sqlMapConfig xmlns="http://ibatis.apache.org/dataMapper" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
原来的映射Person.xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<sqlMap namespace="Sequence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="SqlMap.xsd">
更改为
<?xml version="1.0" encoding="UTF-8" ?>
<sqlMap namespace="Admin" xmlns="http://ibatis.apache.org/mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
……
</sqlMap>
2.数据库联接总是有问题,提示BYTEFX.....之类的错误
修改provider.config
用那个数据库,就把哪个的“provider”里的“enable”设置成“true”就行了。
3. nPetShop例子无法运行
如果用access数据库,更改dao.config里面的mdb路径为正确路径