少英's profile妹妹的乖宝宝PhotosBlogListsMore Tools Help
    November 21

    简化IBatisNet的配置方法 类(对初学者非常有帮助)

    刚接触IBatisNet,对于它的配置文件不容易很快的上手,原因就在于IBatisNet的配置信息过于复杂,其实了解其过程就会发现IBatisNet的配置信息分为以下几个步骤:

    1,配置DataAccess的dao.config

    2,配置DataMapper的sqlmap.config

    3,创建xml的数据操作map文件(例如:company.xml),实体类,操作类的借口和操作类(注意,操作类必须继承IDao和操作类借口)

    3,程序第一次加载的时候要生成 IBatisNet 的 domanager。

    接下来就可以正常使用了,但是,初学者往往会在第一次生成的时候遇到诸多的问题,由于错误提示信息不够清晰,所以导致查找起来非常的困难,其实大家需要注意的是以下几点:

    1,注意区分大小写,IBatisNet的config对大小学非常的敏感,注意在写入配置信息的过程中避免出错,我一般都是写好一个专用程序自动来生成的,这样可以避免手工录入的时候出现不必要的失误。

    2,配置文件的调用路径一定要绝对的路径,IBatisNet不会因为你指对了dao.config配置文件的位置而相对的找到sqlmap.config的路径,所以sqlmap的路径一定要正确,尽量不要采用偷懒的默认路径,同样xml的实际操作类的map路径也要正确。

    我这里写好了一个自动修改配置文件的路径的基础类,大家可以借鉴一下,希望能够对初学者有所帮助。

    using System;
    using System.IO;
    using System.Text;
    using System.Configuration;
    using System.Text.RegularExpressions;

    using IBatisNet.Common;
    using IBatisNet.Common.Utilities;
    using IBatisNet.DataAccess;
    using IBatisNet.DataAccess.Configuration;
    using IBatisNet.DataAccess.Interfaces;
    using IBatisNet.DataAccess.Scope;

    using System.Xml;
    using System.Xml.XPath;

    namespace Commons.DataAccessGlobal
    {
        public static class DataAccessGlobal
        {
            private static bool initSign = false;
            private static IDaoManager daoManager;

            #region Config和xml初始化

            private const string CONFIG_DAO_FILE_NAME = "DataAccess.config";
            private const string CONFIG_PROVIDERS_FILE_NAME = "DataProviders.config";
            private const string CONFIG_SQLMAP_FILE_NAME = "DataMapper.config";
            private const string CONFIG_IBATISNET_FILE_NAME = "IBatisNet.config";

            private static Configuration _IBatisNet_Config;
            private static Configuration IBatisNet_Config
            {
                get
                {
                    if (_IBatisNet_Config == null)
                    {
                        ExeConfigurationFileMap ecf = new ExeConfigurationFileMap();
                        ecf.ExeConfigFilename = CONFIG_IBATISNET_FILE_NAME;
                        _IBatisNet_Config = ConfigurationManager.OpenMappedExeConfiguration(ecf, ConfigurationUserLevel.None);

                        _IBatisNet_Config.Save();
                    }
                    return _IBatisNet_Config;
                }
            }

            private static string CONFIG_DIR
            {
                get
                {
                    if (IBatisNet_Config.AppSettings.Settings["DAO_CONFIG_DIR"] == null)
                    {
                        IBatisNet_Config.AppSettings.Settings.Add("DAO_CONFIG_DIR", "");
                        IBatisNet_Config.Save();

                        throw new Exception("配置信息中的 DAO_CONFIG_DIR 未定义。");
                    }
                    return IBatisNet_Config.AppSettings.Settings["DAO_CONFIG_DIR"].Value;
                }
            }

            private static string CONFIG_XML_SQLMAPPER_DIR
            {
                get
                {
                    if (IBatisNet_Config.AppSettings.Settings["XML_SQLMAPPER_DIR"] == null)
                    {
                        IBatisNet_Config.AppSettings.Settings.Add("XML_SQLMAPPER_DIR", "");
                        IBatisNet_Config.Save();

                        throw new Exception("配置信息中的 XML_SQLMAPPER_DIR 未定义。");
                    }
                    return IBatisNet_Config.AppSettings.Settings["XML_SQLMAPPER_DIR"].Value;
                }
            }

            private static void InitDataAccessConfig()
            {
                XmlDocument document = new XmlDocument();

                document.Load(CONFIG_DIR + CONFIG_DAO_FILE_NAME);

                XmlNamespaceManager xnm = new XmlNamespaceManager(document.NameTable);
                xnm.AddNamespace("dao", "http://ibatis.apache.org/dataAccess");
                xnm.AddNamespace("provider", "http://ibatis.apache.org/providers");

                XmlNode node = document.SelectSingleNode("/dao:daoConfig/dao:providers", xnm);
                node.Attributes["resource"].Value = CONFIG_DIR + CONFIG_PROVIDERS_FILE_NAME;

                node = document.SelectSingleNode("/dao:daoConfig/dao:context/dao:daoSessionHandler/dao:property", xnm);
                node.Attributes["value"].Value = CONFIG_DIR + CONFIG_SQLMAP_FILE_NAME;

                document.Save(CONFIG_DIR + CONFIG_DAO_FILE_NAME);
            }

            private static void InitSqlMapperConfig()
            {
                XmlDocument document = new XmlDocument();

                document.Load(CONFIG_DIR + CONFIG_SQLMAP_FILE_NAME);

                XmlNamespaceManager xnm = new XmlNamespaceManager(document.NameTable);
                xnm.AddNamespace("mapper", "http://ibatis.apache.org/dataMapper");
                xnm.AddNamespace("provider", "http://ibatis.apache.org/providers");
                xnm.AddNamespace("mapping", "http://ibatis.apache.org/mapping");

                XmlNode node = document.SelectSingleNode("/mapper:sqlMapConfig/mapper:sqlMaps", xnm);

                foreach (XmlNode xn in node)
                {
                    xn.Attributes["resource"].Value = CONFIG_XML_SQLMAPPER_DIR + Regex.Match(xn.Attributes["resource"].Value, @"[^\\]+.xml", RegexOptions.IgnoreCase).Value;
                }

                document.Save(CONFIG_DIR + CONFIG_SQLMAP_FILE_NAME);
            }

            #endregion

            public static void InitDao()
            {
                if (!initSign)
                {
                    InitDataAccessConfig();
                    InitSqlMapperConfig();

                    DomDaoManagerBuilder builder = new DomDaoManagerBuilder();
                    builder.Configure(CONFIG_DIR + CONFIG_DAO_FILE_NAME);
                    daoManager = DaoManager.GetInstance("DaoContext") as IDaoManager;
                    initSign = true;
                }
            }

            public static IDao GetDaoManager(Type daoInterface)
            {
                InitDao();

                if (daoManager == null)
                {
                    return null;
                }
                else
                {
                    return daoManager[daoInterface];
                }
            }
        }
    }

     

    相对应的配置文件:

    IBatisNet.config //程序级配置信息

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
        <appSettings>
            <add key="DAO_CONFIG_DIR" value="D:\Temp\" />
            <add key="XML_SQLMAPPER_DIR" value="D:\Temp\" />
        </appSettings>
    </configuration>

    DataAccess.config //dao.config文件,我这里没有用默认的dao.config文件名称,我不太喜欢默认的东西:)

    <?xml version="1.0" encoding="utf-8"?>
    <daoConfig xmlns="http://ibatis.apache.org/dataAccess" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <providers resource="D:\Temp\DataProviders.config" />
      <context id="DaoContext" default="true">
        <database>
          <provider name="sqlServer2.0" />
          <dataSource name="SiteDB" connectionString="Server=vm-ssc-1; Database=HomeWorld;User ID=sa; Password=******" />
        </database>
        <daoSessionHandler id="SqlMap">
          <property name="resource" value="D:\Temp\DataMapper.config" />
        </daoSessionHandler>
        <daoFactory>
          <dao interface="TaoKe.Tools.Courses.Dao.CoursesInterface,TaoKe.Tools.Courses.Dao" implementation="TaoKe.Tools.Courses.Dao.CoursesDao,TaoKe.Tools.Courses.Dao" />
        </daoFactory>
      </context>
    </daoConfig>

    DataMapper.config //sqlmap.config sqlmapper的配置文件信息

    <?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="false" />
      </settings>
      <sqlMaps>
        <sqlMap resource="D:\Temp\CoursesMap.xml" />
      </sqlMaps>
    </sqlMapConfig>

    CoursesMap.xml //数据操作map的xml文件例子

    <?xml version="1.0" encoding="utf-8" ?>
    <sqlMap namespace="Courses" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://ibatis.apache.org/mapping">

    <alias>
       <typeAlias alias="Courses" type="TaoKe.Tools.Courses.Dao.CoursesModel, TaoKe.Tools.Courses.Dao" />
    </alias>

    <resultMaps>
       <resultMap id="Courses_ResultMap" class="Courses">
          <result property="id" column="id" />
          <result property="site" column="site" />
          <result property="url" column="url" />
          <result property="name" column="name" />
          <result property="pubTime" column="pubTime" />
          <result property="city" column="city" />
          <result property="price" column="price" />
          <result property="body" column="body" />
       </resultMap>
    </resultMaps>

    <parameterMaps>
        <parameterMap id="Courses_Parameter_Insert" class="Courses">
            <parameter property="site" column="site" />
            <parameter property="url" column="url" />
            <parameter property="name" column="name" />
            <parameter property="pubTime" column="pubTime" />
            <parameter property="city" column="city" />
            <parameter property="price" column="price" />
            <parameter property="body" column="body" />
        </parameterMap>
    </parameterMaps>

    <statements>
    <insert id="Courses_Insert" parameterMap="Courses_Parameter_Insert" parameterClass="Courses">
       <generate table="Courses"/>
    </insert>

    <select id="Courses_GetObject" resultMap="Courses_ResultMap" resultClass="Courses">
       select site,url,name,pubTime,city,price,body from Courses
    </select>

    <select id="Courses_GetList" resultMap="Courses_ResultMap" resultClass="Courses">
       select site,url,name,pubTime,city,price,body from Courses
    </select>
    </statements>

    </sqlMap>

    DataProviders.config //就是providers.config,用IBatisNet提供的即可,文件名我也改过了,学的朋友要注意,这里就不列出该配置文件的内容了:)

    November 07

    奔跑

         静谧的非洲大草原上,夕阳西下,这时,一头狮子在沉思,明天当太阳升起,我要奔跑,以追上跑得最快的羚羊;此时,一只羚羊也在沉思,明天当太阳升起,我要奔跑,以逃脱跑得最快的狮子。
         那么,无论你是狮子或是羚羊,当太阳升起,你要做的,就是奔跑。