Ibatis.Net 各种配置说明学习(二)

1、各个配置文件的配置说明

providers.config:指定数据库提供者,.Net版本等信息。

xxxxx.xml:映射规则。

SqlMap.config:大部分配置一般都在这里,如数据库连接等等。

2、默认配置文件的存放位置

在Windows应用项目或者类库项目中,需要将配置文件放在项目的bin/Debug/目录下。

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

当然,这也不是强制性的,也可以很灵活地配置。

例如以下代码,就从指定的位置去加载了SqlMap.config。注意,虽然SqlMap.config是从指定的位置,但是要注意里面的resource引入的资源,还是从原来的默认目录开始找

 public IList<PersonModel> GetList()
        {
            //ISqlMapper mapper = Mapper.Instance();
            DomSqlMapBuilder builder = new DomSqlMapBuilder();
            ISqlMapper mapper = builder.Configure(@"D:\SqlMap.config");
            IList<PersonModel> ListPerson = mapper.QueryForList<PersonModel>("SelectAllPerson", null);//这个"SelectAllPerson"就是xml映射文件的Id
            return ListPerson;
        }
View Code
  <providers resource="providers.config"/>

SqlMap.config里面的providers.config这个东西,还是需要在原来默认的位置。

3、SqlMap.config

下面先放上一个在官网直接下载下来的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">

  <properties resource="../../../Files/properties.config"/>
  
  <settings>
    <setting useStatementNamespaces="false"/>
  </settings>
  
  <providers resource="../../../Files/providers.config"/>
  
    <!-- Database connection information -->
    <database>
        <provider name="OleDb2.0"/>
        <dataSource name="Blogs" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=../../../Files/Blogs.mdb"/>
    </database>

    <sqlMaps>
            <sqlMap resource="../../../Files/Maps/Access/Post.xml" />
          <sqlMap resource="../../../Files/Maps/Access/Blog.xml" />
        <sqlMap resource="../../../Files/Maps/Access/Author.xml" />
    </sqlMaps>

</sqlMapConfig>
View Code

properties节点

properties节点通常用于引入在外部定义一些键值对配置文件,以方便在后面统一调用,这样修改的时候,只修改就可以了

它的引入方式有3种:

  •   resource:   通过相对路径来确定文件的位置。
  •   url:     通过绝对路径来确定文件位置。
  •   embedded:通过嵌入资源方式来确定文件位置。

  <sqlMap embedded = "命名空间.文件名.后缀名, 命名空间"/>

例如,你在bin/Debug/目录下添加了一个properties.config文件,里面定义了一个数据库连接

<?xml version="1.0" encoding="utf-8" ?>
<settings>
    <add key="connectionString" value="server=...;uid=...;pwd=...;database=Test"/>
</settings>
View Code

这样在SqlMap.config文件中就可以这样写:

 <properties resource="properties.config"/>
  <!--通常用于引入在外部定义的一些键值对配置文件,以方便统一调用-->

  <!--数据库连接字符串-->
  <database>
    <provider name="sqlServer2.0"/>
    <!--<dataSource name="Test" connectionString="server=...;uid=...;pwd=...;database=Test"/>-->
    <dataSource name="Test" connectionString="${connectionString}"/>
  </database>

settings节点

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

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

示例:

  <settings>
    <setting useStatementNamespaces="false"/> <!--是否使用全局完整命名空间-->
  </settings>

使用全局命名空间时,不同的xml映射文件,id互不影响。如果出现提示"重复的sql Id的情况",大多是没启用。如果启用,则执行sql语句时,要用命名空间.id的方式。

如果为"true",示例:

<?xml version="1.0" encoding="utf-8" ?>
<sqlMap namespace="Person" xmlns="http://ibatis.apache.org/mapping" xmlns:xls="http://www.w3.org/2001/XMLSchema-instance">
  <resultMaps>
    <resultMap id="PersonModel" Class="Person">
      <!--id会被statements节点所用,Class实体类所在位置-->
      <result property="PersonId" column="Id"/>
      <!--property对应实体类的属性名,column对应数据库表的列名-->
      <result property="PersonName" column="Name"/>
    </resultMap>
  </resultMaps>
  <statements>
    <select id="SelectAllPerson" resultMap="PersonModel">
      <!--id在程序中会被SqlMapper实体类所调用,resultMap就是resultMap节点的id-->
      select * from person
    </select>

调用:

IList<PersonModel> ListPerson = mapper.QueryForList<PersonModel>("SelectAllPerson", null);

这里就不能这样调用了,应该为: IList<PersonModel> ListPerson = mapper.QueryForList<PersonModel>("Person.SelectAllPerson", null);

providers节点

  <providers resource="providers.config"/>

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

database节点

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

<database>
    <provider name="sqlServer2.0"/>
    <!--<dataSource name="Test" connectionString="server=106.14.182.241;uid=sa;pwd=van;database=Test"/>-->
    <dataSource name="Test" connectionString="${connectionString}"/>
  </database>

SqlMaps节点

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

 <sqlMaps>
    <sqlMap resource="Person.xml" />
    <!--这个是指定映射文件的位置-->
  </sqlMaps>

4、映射文件

映射文件指定哪个属性名,对于哪个列名。

下面来看看省略的写法(当property与column完全相同时,可以这样写):

<?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">
  <statements>
    <select id="SelectAllPerson" resultClass="IbatisNetModel.PersonModel">
      select * from person
    </select>
  </statements>
</sqlMap>
View Code

下面再来看看全部写全的写法,为了展示不同,我将PersonModel实体的Id和Name的属性名改为PersonId,PersonName:

 public class PersonModel
    {
        public int PersonId { get; set; }

        public string PersonName { get; set; }
    }
View Code
<?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="PersonModel" Class="IbatisNetModel.PersonModel">
      <!--id会被statements节点所用,Class实体类所在位置-->
      <result property="PersonId" column="Id"/>
      <!--property对应实体类的属性名,column对应数据库表的列名-->
      <result property="PersonName" column="Name"/>
    </resultMap>
  </resultMaps>

  <statements>
    <select id="SelectAllPerson" resultMap="PersonModel" >
      <!--id在程序中会被SqlMapper实体类所调用,resultMap就是resultMap节点的id-->
      select * from person
    </select>
  </statements>

</sqlMap>
View Code

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

<?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">

  <alias>
    <typeAlias alias="Person" type="IbatisNetModel.PersonModel,IbatisNetModel"/>
    <!--为类指定一个别名-->
  </alias>

  <resultMaps>
    <resultMap id="PersonModel" Class="Person">
      <!--id会被statements节点所用,Class实体类所在位置-->
      <result property="PersonId" column="Id"/>
      <!--property对应实体类的属性名,column对应数据库表的列名-->
      <result property="PersonName" column="Name"/>
    </resultMap>
  </resultMaps>

  <statements>
    <select id="SelectAllPerson" resultMap="PersonModel" >
      <!--id在程序中会被SqlMapper实体类所调用,resultMap就是resultMap节点的id-->
      select * from person
    </select>
  </statements>

</sqlMap>
View Code

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

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

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

“/”应用程序中的服务器错误。Could not load type from string value 'xxx'

Ibatis.Net有很多默认的别名:

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

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

属性说明:

属性

说明

parameterMap

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

parameterClass

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

resultMap

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

resultClass

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

cacheModel

Statement对应的Cache模块

extends

重复使用SQL子句

extends

extends用于提取一段重复使用的SQL语句而避免重复书写

  <statements>

    <select id="SelectCommon" resultMap="PersonModel">
      select * from person
    </select>

    <select id="SelectAllPerson" resultMap="PersonModel" extends="SelectCommon">
      <!--id在程序中会被SqlMapper实体类所调用,resultMap就是resultMap节点的id-->
      order by Id desc
    </select>
    
  </statements>

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:用于指定最大值

resultMap的属性(后面详细介绍)

它的属性很多是和parameterMap想对应的,但是值得一提的是它下面可以添加一个constructor元素来匹配一个构造函数。当然,这个的前提是Person类中有这样一个构造函数。例如:

<resultMaps>
    <resultMap id="PersonModel" Class="Person">
      <constructor>
        <argument argumentName="personId" column="Id"/>
        <argument argumentName="personName" column="Name"/>          
      </constructor>
      <!--id会被statements节点所用,Class实体类所在位置-->
      <result property="PersonId" column="Id"/>
      <!--property对应实体类的属性名,column对应数据库表的列名-->
      <result property="PersonName" column="Name"/>
    </resultMap>
  </resultMaps>

执行存储过程(后面详细介绍)

这里有一点区别就是,只可以使用parameterMap,而不可以使用parameterClass,其实想一想,您难道还会为每一个存储过程定义个传入的实体类吗?还有一点,就是它的参数完全是按照 parameterMap中的定义自动匹配的。

 <procedure id="demoProcedure" parameterMap="procedureDemo">
        CustOrderHist
        <!--执行存储过程-->
    </procedure>

对SQL片段的引用(后面详细介绍)

在编写SqlMaps的时候,经常需要把一个SQL语句进行拆分,然后在不同的地方引用它,我们可以使用sql和include的组合来完成。

    <sql id="orderby">
        order by Id desc
    </sql>

    <select id="SelectAllPerson" resultMap="PersonModel">
         select * from person
        <include refid="orderby"/>
    </select>

5、$与#的区别

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$',否则会出错,因为它不会自动增加单引号。

参考:http://www.cnblogs.com/caoyc/category/873268.html

原文地址:https://www.cnblogs.com/vanblog/p/8582477.html