温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

用IBatis.net实现数据读取

发布时间:2020-06-20 04:18:27 来源:网络 阅读:535 作者:seesea125 栏目:编程语言

官方下载地址:http://www.mybatis.org/

IBatis.net作用是把数据库查询与对象的属性间建立映射关系。但它并不是一个实体关系映射工具,仅用于帮助程序人员建立实体和SQL语句或者存储过程间的映射。因此只能叫半自动OR/M工具。

IBatis.net的配置:

一、引用几个DLL,注意在数据层引用即可。

用IBatis.net实现数据读取

单独使用映射的情况下,只需要引用IBatisNet.DataMapper.dll就可以了

其中IBatisNet.Common.dll和Castle.DynamicProxy.dll是必须的

COMMON是我自己的,不用管它

IBatisNet.DataAccess.dll是可选的,如果使用Data Access组件,则还需要添加对IBatisNet.DataAccess.dll的引用。

二、完成对组件的添加后,还需要添加三个XML文档

1 providers.config ----DataMapper根据这个确定是什么类型数据库(放在数据层)

2 SqlMap.xml ----数据映射文档,里面包含了SQL语句(放在数据层)

3 SqlMap.config ----DataMapper的配置文档,它详细描述了工程中SqlMap.XML和providers.config文档的位置,以及其他配置项(必须放在Web跟目录下)。

先看SqlMap.config

 

<?xml version="1.0" encoding="utf-8"?>
<sqlMapConfig xmlns="http://ibatis.apache.org/dataMapper" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <settings>
<setting useStatementNamespaces="true"/>//如果是true,那么写数据查询时,查询语句的名称前要添加它的完整命名空间
<setting cacheModelsEnabled="true"/>//全局化配置DataMapper客户能否启用cache,默认是true
</settings>
<providers embedded="DAL.providers.config, DAL"/> //指定providers.config的位置

<database>
<provider name="sqlServer2.0"/> //如果使用默认的数据提供者,这句可以不要,如果系统中使用多个数据库,需要配置provider的内容
<dataSource name="ConnStr" connectionString="Data Source=.;Database=Test;User ID=sa;Password=123456;Connection Lifetime=3;Min Pool Size=1;Max Pool Size=50;MultipleActiveResultSets=true;"/> //数据库链接字符串,
</database>

<sqlMaps>
<sqlMap embedded="DAL.Maps.UserInfo.xml, DAL"/> //程序的数据映射文件的位置,如果有多个XML,就写多行,如果比较多,也可以当一个单独的XML中去写,比如<sqlMap resource=”Maps/All.XML”/>,然后在ALL.xml再添加数据映射文件,这样就实现了加载一组数据映射文件
</sqlMaps>
</sqlMapConfig>
复制代码

 

具体文档配置可在附件的DEMO中下载查看。

三、创建SqlMapper实例,这个是单例模式,文件名SqlMapper.CS,放在数据层中。

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IBatisNet.Common.Utilities;
using IBatisNet.DataMapper;
using IBatisNet.DataMapper.Configuration;
using IBatisNet.DataMapper.SessionStore;
namespace DAL
{
public class SqlMapper
{
private static volatile ISqlMapper _mapper = null;

protected static void Configure(object obj)
{
_mapper = null;
}

protected static void InitMapper()
{
ConfigureHandler handler = new ConfigureHandler(Configure);
DomSqlMapBuilder builder = new DomSqlMapBuilder();
_mapper = builder.ConfigureAndWatch("SqlMap.config", handler);
_mapper.SessionStore = new HybridWebThreadSessionStore(_mapper.Id);
}

public static ISqlMapper Instance()
{
if (_mapper == null)
{
lock (typeof(SqlMapper))
{
if (_mapper == null) // double-check
{
InitMapper();
}
}
}
return _mapper;
}

public static ISqlMapper Get()
{
return Instance();
}
}
}
复制代码

 

外界调用方法:

IList list = SqlMapper.Instance().QueryForObject<list>("UserInfo.GetCount", param);

因为是单例的,第一次调用时,DomSqlMapBuilder对象会通过查询SqlMap.config来创建一个sqlMapper实例,以后调用就直接从缓存读取了。

DomSqlMapBuilder.ConfigureAndWatch()方法负责监视配置文件的更新情况,如果配置或者映射文件有更新,SqlMapper对象会重新载入并不重启系统

 

整个配置已经结束,具体可参考DEMO:DEMO下载

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI