SmartPersistenceLayer 3.0 之基础设置篇

 (转http://magazine.cnblogs.com/200502/index.html
 

       现在我要讲一下,SPL在系统中应该如何应用,通过本篇,你将会学会如何配置SPL到你的系统中。

SPL运行原理

       SPL作为一个数据访问平台,最终以DLL的方式体现,那么系统只要引用SPLDLL就可以开始应用了。

       SPL将依赖于两个XML配置文件:数据源配置文件(以下我称之为DatabaseMap.xml)和实体映射文件(以下称之为ClassMap.xml)。这两个配置文件在整个系统运行时,将一次性载入到内存,这样可以提高SPL的运行效率,所以如果这两个配置文件修改了,要IISReset或是重编译才能见到效果。

       DatabaseMap.xml

databaseMap.xml是数据源配置文件,在这里配置系统运行的数据库连接,目前支持三种数据源(SQL Server,Oracle,Access),可以在这个文件中配置多个多种数据源,下面举个例子:
<?
xml
version="1.0" encoding="utf-8"?><map>

<!-- SQL Server服务器配置  -->

  <database name="SQL" type="MsSqlServer">

    <parameter name="Provider" value="SQLOLEDB.1" />

    <parameter name="Password" value="" />

    <parameter name="Initial Catalog" value="northwind" />

    <parameter name="User ID" value="sa" />

    <parameter name="Data Source" value="localhost" />

    <classMapFile path="ClassMap1.xml" />

</database>

<!-- SQL Server服务器配置  -->

  <database name="ACCESS" type="MsAccess">

    <parameter name="Provider" value=" Microsoft.Jet.OLEDB.4.0" />

    <parameter name="Data Source" value=" D:\pl\Test\test.mdb" />

    <classMapFile path="ClassMap2.xml" />

</database>

<!-- SQL Server服务器配置  -->

<database name="ORACLE" type="Oracle">

      <parameter name="Provider" value="MSDAORA.1" />

      <parameter name="Password" value="password" />

      <parameter name="User ID" value="user" />

    <parameter name="Data Source" value="host.domain.com" />

    <classMapFile path="ClassMap3.xml" />

</database>

</map>

以上列举了三种数据库的配置信息,其中:

database name是指在SPL中要使用的数据源名,这个值要能唯一标识一个连接,这样在SPL中可以直接使用name就可以执行操作了.

databasetype有三种:MsSqlServer是指SQL数据库

                    MsAccess是指Access数据库

                  Oracle是指ORACLE数据库

parameter是连接中要用到的各个参数,这根据不同的数据源类型会有不同的值,具体的参照上面的写法就可以了.

    如果要扩展参数,可以直接添加parameter,因为SPL中从parameter中读取[值对]组装成连接字符串的.

    每个连接源上都有一个  <classMapFile path="ClassMap.xml" />是指定这个数据源对应的表与实体的映射信息,可以采用与databaseMap的相对路径指定,也可以指定为绝对路径,下面解释一下ClassMap.xml中的内容.

ClassMap.xml

ClassMap.xml是实体与表的映射关系,我以一个典型的例子要做解释,比如目前有个表为学生表Student,有字段:

        Id      序号      Int          pk   自动增长

        No     学号      Varchar(40)   pk

        Name    姓名     Varchar(40)

        Birthday  出生日期 Datatime

        Grade     年级     Int

        Score    入学成绩  decimal

 那么对应的映射信息为:

<class name="StudentEntity" table="Student" database="MyDB">

    <attribute name="Id" column="Id" type="Integer" increment="true" key="primary" />

    <attribute name="No" column="No" type="String" key="primary" />

    <attribute name="Name" column="Name" type="String" />

    <attribute name="Birthday" column="Birthday" type="Date" />

    <attribute name="Grade" column="Grade" type="Integer" />

    <attribute name="Score" column="Score" type="Decimal" />

</class>

从以上的配置我们很容易理解:

  class name是指实体名,这要与实体类的名称相同

table是指映射的数据库是的表名;

database是指定实体默认的数据库源,对应databaseMap.xml的配置;

IsSaveToMemory为可选配置,如果此表为维护性数据,具有字段少,记录少,修改频率低,使用频率高,就可以设置IsSaveToMemory="true",这样,这个表的数据将会自动在内存存取,具体将在后面讲解.

 attribute是每一个对应的实体属性与表字段名映射,

name为实体的属性名,要与实体类中的属性相同;

column是指表的字段名,这与表中的字段要相同;

type指类型,如整数型Integer,字符型String,日期型Date,数字型Decimal.

increment是指字段是否为自动增长,是则标识为true,这样SPL将会从数据库中自动生成;

key值为primary说明此字段是主键,SPL中每个表必须要有一个主键,这作为表的唯一标识;

   关于type的对应关系:

    

SQL Server

Oracle

XML中的type

.NET内置类型

bigint

Integer

BigInt

System.Int64

binary

 

Binary

System.Byte[]

bit

 

Boolean

System.Boolean

char

Char

String

System.String

datetime

Date

Date

System.DateTime

decimal

Decimal

Decimal

System.Decimal

float

Float

Double

System.Double

image

 

Binary

System.Byte[]

int

Integer

Integer

System.Int32

money

 

Decimal

System.Decimal

nchar

 

String

System.String

ntext

 

String

System.String

numeric

 

Decimal

System.Decimal

nvarchar

 

String

System.String

real

 

Single

System.Single

smalldatetime

 

Date

System.DateTime

smallint

Integer

SmallInt

System.Int16

smallmoney

 

Decimal

System.Decimal

sql_variant

 

Object

System.Object

text

 

String

System.String

timestamp

 

Binary

System.Byte[]

tinyint

 

TinyInt

System.Byte

uniqueidentifier

 

Guid

System.Guid

varbinary

 

Binary

System.Byte[]

varchar

Varchar2

String

System.String

posted @ 2005-02-28 12:39  沉默天蝎的学习汇集  阅读(515)  评论(0)    收藏  举报