zoukankan      html  css  js  c++  java
  • IBatis.net使用一用IBatis.net实现数据读取

    查看文章索引请通过http://www.cnblogs.com/seesea125/archive/2012/04/17/2453256.html

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

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

    IBatis.net的配置:

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

    image

    单独使用映射的情况下,只需要引用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下载

  • 相关阅读:
    10.13 新版本go on~
    9.30 总结一下九月呗
    9.25 学习下日期加减
    9.22 迎难而上不要怂!
    9.22 Sans-serif VS Serif
    9.22 keep studying
    【LeetCode刷题】最长同值路径:妙解
    【LeetCode刷题】机器人走路最大距离:妙解
    【LeetCode刷题】不使用+-的加减法:妙解
    【LeetCode刷题】NIM游戏:妙解
  • 原文地址:https://www.cnblogs.com/seesea125/p/2377609.html
Copyright © 2011-2022 走看看