C#版本的MyBatis( C#解析XML文件并且获取SQL语句)

来源:互联网 发布:门户网站源码下载 编辑:程序博客网 时间:2024/06/01 14:43

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace Common
{
    /// <summary>
    /// 读取SQLConfig文件中的SQL
    /// </summary>
    public  class SqlConfig
    {
        public SqlConfig()
        {
        }

        #region 读取指定SQL
        /// ========================================================================================
        /// 类名:GetSql
        /// <summary>
        /// 读取指定SQL
        /// </summary>
        /// <param name="key">sql名称</param>
        /// <returns>sql字符串</returns>
        /// ========================================================================================
        ///    更新记录
        ///    序号     更新日期         担当者    更新内容
        ///    0001    2014/04/24     zhou        初次作成
        ///    ========================================================================================
        public static string GetSql(string key)
        {
            string sql = string.Empty;
            try
            {
                string[] sqlLayers = key.Split('.');
                XmlNode xmlNode = ReadMapping(sqlLayers[0]);
                XmlNodeList list = xmlNode.SelectNodes("Sql");
                foreach (XmlNode node in list)
                {
                    if (node.Attributes["key"].Value.Equals(sqlLayers[1]))
                    {
                        return node.InnerText;
                    }
                }
                sql = xmlNode.SelectNodes("SqlMap[@key='" + sqlLayers[1] + "']")[0].InnerText;
            }
            catch (Exception ex)
            {
            }
            return sql;
        } 
        #endregion

        #region 读取配置文件中的Mapping节点
        /// ========================================================================================
        /// 类名:ReadMapping
        /// <summary>
        /// 读取配置文件中的Mapping节点
        /// </summary>
        /// <param name="key">mapping节点名称</param>
        /// <returns>Mapping节点下的sqlMap</returns>
        /// ========================================================================================
        ///    更新记录
        ///    序号     更新日期         担当者    更新内容
        ///    0001    2014/04/24     zhou        初次作成
        ///    ========================================================================================
        private static XmlNode ReadMapping(string key)
        {
            XmlDocument Xdoc;
            string fileName = AppDomain.CurrentDomain.BaseDirectory + (@"\SqlMapping.config");
            Xdoc = new XmlDocument();
            Xdoc.Load(fileName);
            XmlNodeList list = Xdoc.SelectNodes("SqlMapping/SqlMaps");
            foreach (XmlNode node in list)
            {
                if (node.Attributes["key"].Value.Equals(key))
                {
                    return node;
                }
            }
            return null;
        } 
        #endregion
    }


}

--------------------------------------------------------------------------

2、XML文件的格式如下所示,文件名为【SqlMapping.config】

<?xml version="1.0" encoding="utf-8" ?>
<SqlMapping>
  <!--数据维护模块-->
  <SqlMaps key="DataManage">
    <Sql key="GetNavigation">
      <![CDATA[
                 select * form Navigation
    ]]>
    </Sql>
    <Sql key="GetUserInfo">
        <![CDATA[
                 select * form users
             ]]>
      </Sql>
    </SqlMaps>
</SqlMapping>


3、访问方式为如下所示:

class Program
    {
        static void Main(string[] args)
        {
           string sql = SqlConfig.GetSql("DataManage.GetNavigation");
           Console.WriteLine(sql.Trim());
           Console.ReadKey();
        }
    }

0 0
原创粉丝点击