• Ibatis学习2 ---配置文件学习


    1、Ibatis.Net 主要用到三个配置SqlMap.config Providers.config  XXXX.xml 

          SqlMap.config  主要用于配置数据库连接、缓存控制类等信息。

          providers.config   主要用于指定数据库

          xxxxx.xml   主要用于设置映射规则

    2、不指定配置文件的位置 配置文件应该放置在默认的位置

       Windows应用项目或者类库项目,需要放在项目的bin/debug目录下

       在Web应用程序中,需要放在应用程序根目录

       在初始化数据库连接的时候 可以通过DomSqlMapBuilder 来指定具体的SqlMap文件放置的位置

       首先,需要初始化一个类 var builder=new DomSqlMapBuilder(); 

       通过DomSqlMapBuilder的Configure指定具体的SqlMap.config 放置的路径 。 虽然我们指定了读取SqlMap.config的路径 其中配置的xxx.xml 及数据库的支持文件Providers.config还是要在默认的位置来找

           

    using System.Collections.Generic;
    using IBatisNet.DataMapper;
    using IBatisNet.DataMapper.Configuration;
    using StudyDemo.Model;
    
    namespace StudyDemo.Dao
    {
        public class BlogContentDao
        {
            public IList<BlogContent> GetList()
            {
                ISqlMapper mapper = Mapper.Instance();
                IList<BlogContent> listBlogContent = mapper.QueryForList<BlogContent>("SelectAllBlogContenty", null);
                return listBlogContent;
            }
    
            public IList<BlogContent> GetBlogContents()
            {
                DomSqlMapBuilder builder = new DomSqlMapBuilder();
                ISqlMapper mapper = builder.Configure(@"../../Config/SqlMap.config");
                IList<BlogContent> listBlgContent = mapper.QueryForList<BlogContent>("SelectAllBlogContenty", null);
                return listBlgContent;
            }
    
            public IList<BlogContent> GetBlogContentsTest()
            {
                DomSqlMapBuilder builder = new DomSqlMapBuilder();
                ISqlMapper mapper = builder.Configure(@"../../Config/test/SqlMap.config");
                IList<BlogContent> listBlgContent = mapper.QueryForList<BlogContent>("SelectAllBlogContenty", null);
                return listBlgContent;
            }
        }
    }
    View Code

    3、SqlMap.config配置文件中的节点学习

     创建一个新的属性键值对的配置文件properties.config内容如下

      

    <?xml version="1.0" encoding="utf-8" ?>
    <settiing>
      <add  key="datasource" value="server=GSHC-LIWC8;uid=sa; pwd=111111;database=MVC5"/>
    </settiing>

        首先我们看一个完整的SqlMap.config文件  我们看上面的这个文件中的第一个Properties节点,可以引用外部的定义的键值对数据的内容供后面统一使用。

        上面的这些键值对配置文件的引入可以通过三种方式来引入

          resource :相对路径引入      

          url:通过绝对路径来确定文件位置    

          embedded: 通过嵌入资源方式来确定文件的位置

    <?xml version="1.0" encoding="utf-8"?>
    <sqlMapConfig xmlns="http://ibatis.apache.org/dataMapper" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <properties resource="../../config/properties.config"/>
      <settings>
        <setting useStatementNamespaces="false"/>
      </settings>
      <providers  resource="../../config/providers.config"/>
      <database>
        <provider name="sqlServer2008"/>
        <dataSource name="test" connectionString="${datasource}"/>
      </database>
      <sqlMaps>
        <sqlMap  resource="../../config/BlogContent.xml"/>
      </sqlMaps>
    </sqlMapConfig>

       Settings节点

       Settings节点里,可以配置以下5个信息

    • useStatementNameSpaces 默认False 是否使用全局完整的命名空间
    • cacheModelsEnabled 默认true 是否启用缓存
    • validateSqlMap:默认false,使用启用SqlMapConfig.xsd来验证映射XML文件。
    • useReflectionOptimizer:默认true,是否使用反射机制访问C#中对象的属性。
    • useEmbedStatementParams 是否使用嵌入的方式声明可变参数

      

       Providers节点

      示例:

    <providers resource="providers.config"/>

      这里使用引入外部配置文件的方式实现。

        alias节点

      alias节点用于为类指定一个别名,通常用于为一些很长的类名指定一个别名,这样可以减少一些代码。

    <typeAlias alias="Person" type="iBatis.Domain.Person,iBatisSample"/>

      以上代码的意思是,为iBatis.Domain.Person类指定一个别名Person。Person就代表iBatis.Domain.Person这个类。

      起初我以为别名只是起了缩短配置类的作用,但后来我发现别名是还有其他作用的,它还指明了IBatis.net应该到哪个程序集去找实体类。

      如果程序偶尔报如下错误,那么你就要考虑加上别名了。

      “/”应用程序中的服务器错误。

      Could not load type from string value 'xxx'

    <alias>
      <typeAlias alias="Field" type="Nx.Domain.Common.Field, Nx.Domain" />
    </alias>

     database节点

      指定一个你选择使用的数据库,和数据库连接。

      示例:

      <database>
        <provider name="sqlServer2008"/>
        <dataSource name="Test" connectionString="server=KISSDODOG-PC;uid=sa;pwd=123;database=Test"/>
      </database>

      SqlMaps节点

      SqlMaps节点,用于配置映射信息。通常在映射信息写在外部,在这个节点引入。

      

    映射文件的学习

       Model下定义一个新的实体类Blog

       

    namespace StudyDemo.Model
    {
        public class Blog
        {
            public int Id { get; set; }
            public string No { get; set; }
            public string Title { get; set; }
            public string Content { get; set; }
        }
    }

    新建一个BlogContent.xml的映射文件 内容如下 

      

    <?xml version="1.0" encoding="utf-8" ?>
    <sqlMap namespace="Ibatis" xmlns="http://ibatis.apache.org/mapping" xmlns:xls="http://www.w3.org/2001/XMLSchema-instance">
      <resultMaps>
        <resultMap id="blogcontent" Class="StudyDemo.Model.Blog">
          <result property="Id" column="Id" />
          <result property="No" column="TestNo"/>
          <result property="Title" column="TestTitle"/>
          <result property="Content" column="TestContent"/>
        </resultMap>
      </resultMaps>
      <statements>
        <select id="selectAllBlog" resultMap="blogcontent">
          select * from blogcontent
        </select>
      </statements>
    </sqlMap> 

          resultMaps部分:定义了数据库字段名与实体类属性名之间的关系。当然,如果你数据库字段名与实体类属性名完全一样,那么resultMaps部分是可以省略的。另外要注意一点,ResultMap的列比你查询的列不能少,也不能多。它不会说,ResultMap里映射的列多了,该属性就自动将select返回的列自动置null,而是直接所有列都不映射赋值。也就是说,Person表有Id,Name,Age3列,如果你只想要SELECT两个列(Id,Name),那么ResultMap里面的3列映射没用了,你必须另外搞一个ResulpMap只映射两列的。不爽。

      statements部分:用于定义你需要执行的语句,在程序中通过select的id调用。

    加入以下方法,执行查看结果正常情况是可以正常显示

            public IList<Blog> GetBlogContentsTest()
            {
                DomSqlMapBuilder builder = new DomSqlMapBuilder();
                ISqlMapper mapper = builder.Configure(@"../../Config/test/SqlMap.config");
                IList<Blog> listBlgContent = mapper.QueryForList<Blog>("selectAllBlog", null);
                return listBlgContent;
            }



    属性

    说明

    parameterMap

    参数映射,需结合parameterMap节点对映射关系加以定义,对于存储过程之外的statement而言,建议使用parameterClass作为参数配置方式,一方面避免了参数映射配置工作,另一方面其性能表现更加出色

    parameterClass

    参数类。指定了参数类型的完整类名(包括命名空间),可以通过别名避免每次书写冗长的类名

    resultMap

    结果映射,需结合resultMap节点对映射关系加以定义

    resultClass

    结果类。指定了结果类型的完整类名(包括命名空间),可以通过别名避免每次书写冗长的类名

    cacheModel

    Statement对应的Cache模块

    extends

    重复使用SQL子句

    映射文件中extends使用

    修改映射文件如下

    <?xml version="1.0" encoding="utf-8" ?>
    <sqlMap namespace="Ibatis" xmlns="http://ibatis.apache.org/mapping" xmlns:xls="http://www.w3.org/2001/XMLSchema-instance">
      <resultMaps>
        <resultMap id="blogcontent" Class="StudyDemo.Model.Blog">
          <result property="Id" column="Id" />
          <result property="No" column="TestNo"/>
          <result property="Title" column="TestTitle"/>
          <result property="Content" column="TestContent"/>
        </resultMap>
      </resultMaps>
      <statements>
        <select id="selectAllBlog" resultMap="blogcontent">
          select * from blogcontent
        </select>
        <select id="selectAllBlogOrderbyNodesc" resultMap="blogcontent" extends="selectAllBlog">
          order by TestNo desc
        </select>
      </statements>
    </sqlMap>

    增加新的方法:

        

            public IList<Blog> GetBlogContentsOrderbyNoDesc()
            {
                DomSqlMapBuilder builder = new DomSqlMapBuilder();
                ISqlMapper mapper = builder.Configure(@"../../Config/test/SqlMap.config");
                IList<Blog> listBlgContent = mapper.QueryForList<Blog>("selectAllBlogOrderbyNodesc", null);
                return listBlgContent;
            }

      执行代码调用方法测试,正常情况将会根据TestNo进行倒排。

       

    parameterClass  使用示例

    修改映射文件为

    <?xml version="1.0" encoding="utf-8" ?>
    <sqlMap namespace="Ibatis" xmlns="http://ibatis.apache.org/mapping" xmlns:xls="http://www.w3.org/2001/XMLSchema-instance">
      <resultMaps>
        <resultMap id="blogcontent" Class="StudyDemo.Model.Blog">
          <result property="Id" column="Id" />
          <result property="No" column="TestNo"/>
          <result property="Title" column="TestTitle"/>
          <result property="Content" column="TestContent"/>
        </resultMap>
      </resultMaps>
      <statements>
        <select id="selectAllBlog" resultMap="blogcontent">
          select * from blogcontent
        </select>
        <select id="selectAllBlogOrderbyNodesc" resultMap="blogcontent" parameterClass="StudyDemo.Model.Blog" extends="selectAllBlog">
         where TestNo =#No# order by TestNo desc
        </select>
      </statements>
    </sqlMap>

    增加新方法 

       

            public IList<Blog> GetBlogContentsOrderbyNoDesc()
            {
                DomSqlMapBuilder builder = new DomSqlMapBuilder();
                ISqlMapper mapper = builder.Configure(@"../../Config/test/SqlMap.config");
                Blog t=new Blog(){No = "01"};
                IList<Blog> listBlgContent = mapper.QueryForList<Blog>("selectAllBlogOrderbyNodesc",t);
                return listBlgContent;
            }

    执行的时候,正常结果如下 

    查询的时候根据传的参数 Blog t=new Blog(){No = "01"};进行了过滤

     

    parameterMap的属性

      它可以接受三个属性,id/class/extends,其中是有id是必须的,class用于声明使用的实体类名称,可以是别名,也可以是全名,extends,可想而知,不解释
    在它下一级节点中应该包含若干个parameter元素,来指定对象属性与当前变量的映射规则,parameter有如下常用属性:

      1. property:指定类中的一个属性
      2. columu:定义的参数名称
      3. direction:用于声明存储过程的参数方向(input,output,inputoutput)
      4. dbType:用于指定property映射到数据库中的数据类型
      5. type:用于为参数的对象指定CLR类型
      6. nullValue:指定在property为何值时,将会在存储数据时候,替换为null,这是经常会被用到的
      7. size:用于指定最大值

     

    这个指定的映射信息中Property column 是不起作用的,参数必须与需要的参数顺序一致

     

     

    $与#的区别

    • SELECT * FROM TABLE WHERE Id = #id# 其中如果字段id为字符串类型,那么#id#表示的就是'id',也就是说会自动加引号。如果id为整型,那么#id#就是整型;
    • SELECT * FROM TABLE WHERE Id = $id$ ,如果字段id为整型,Sql语句就不会出错,但是如果字段id为字符串类型,那么Sql语句应该写成 SELECT * FROM TABLE WHERE Id = '$id$',否则会出错,因为它不会自动增加单引号。
  • 相关阅读:
    LeetCode 48 Anagrams
    大数据实时处理:百分点实时计算架构和算法
    Kafka操作
    Kafka
    批量扫描互联网无线路由设备telnet,并获取WIFI密码
    WMI
    openvas
    原始套接字
    Zabbix
    MySQL exist
  • 原文地址:https://www.cnblogs.com/liwenchaoCode/p/5810241.html
Copyright © 2020-2023  润新知