直入主题,简单介绍下平台基本使用。
1、下载最新源码
http://openapi.codeplex.com/SourceControl/list/changesets
2、开发工具准备
请参照前面提到的环境需求进行配置。
3、数据库准备
OpenAPI提供了两种平台的数据库:SQL Server 2005、Oracle 9i
4、配置数据库连接参数
平台的数据访问层由NHibernate实现,可以方便进行配置。打开OpenAPI\OpenAPI.Web.Mvc工程下的web.config文件,找到 hibernate-configuration 配置节:
<session-factory name="NhiLINQ">
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="connection.connection_string">
Data Source=liuxiaojun\sqlexpress;Initial Catalog=OpenAPI_v1;Persist Security Info=True;User ID=sa;Password=1qazxsw2
</property>
<property name="adonet.batch_size">10</property>
<property name="show_sql">false</property>
<property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
<property name="use_outer_join">true</property>
<property name="command_timeout">10</property>
<property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
<!--2.1要配置延迟加载的代理 这里配置为Castle -->
<property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
<mapping assembly="OpenAPI.Model"/>
</session-factory>
</hibernate-configuration>
修改以下数据库连接信息:
Data Source=liuxiaojun\sqlexpress;Initial Catalog=OpenAPI_v1;Persist Security Info=True;User ID=sa;Password=1qazxsw2
</property>
以上配置信息是NHibernate必须有的,另外本项目还提供了扩展的配置文件,<appSettings>配置节中:
注意:如果以上配置节的配置文件存在,那么系统运行时会默认按照此配置节的扩展配置文件中的信息进行NHibernate初始化,而不会按照web.config中hibernate-configuration 配置节进行初始化。
以上引用扩展的配置文件的内容如下:
sqlserver_openapi.cfg.xml
<!--
This template was written to work with NhiLINQ.
Copy the template to your NhiLINQ project folder and rename it in hibernate.cfg.xml and change it
for your own use before compile tests in VisualStudio.
-->
<!-- This is the System.Data.OracleClient.dll provider for Oracle from MS -->
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
<session-factory name="NhiLINQ">
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="connection.connection_string">
Data Source=liuxiaojun\sqlexpress;Initial Catalog=OpenAPI_v1;Persist Security Info=True;User ID=sa;Password=1qazxsw2
</property>
<property name="adonet.batch_size">10</property>
<property name="show_sql">false</property>
<property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
<property name="use_outer_join">true</property>
<property name="command_timeout">10</property>
<property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
<!--2.1要配置延迟加载的代理 这里配置为Castle -->
<property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
<mapping assembly="OpenAPI.Model"/>
</session-factory>
</hibernate-configuration>
项目同时提供了Oracle的扩展配置文件:
oracle_openapi.cfg.xml
<!--
This template was written to work with NhiLINQ.
Copy the template to your NhiLINQ project folder and rename it in hibernate.cfg.xml and change it
for your own use before compile tests in VisualStudio.
-->
<!-- This is the System.Data.OracleClient.dll provider for Oracle from MS -->
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
<session-factory name="NhiLINQ">
<property name="connection.driver_class">NHibernate.Driver.OracleClientDriver</property>
<property name="connection.connection_string">
Data Source=crm;User id=openapi;Password=1qazxsw2;
</property>
<property name="adonet.batch_size">10</property>
<property name="show_sql">false</property>
<property name="dialect">NHibernate.Dialect.Oracle9iDialect</property>
<property name="use_outer_join">true</property>
<property name="command_timeout">10</property>
<property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
<!--2.1要配置延迟加载的代理 这里配置为Castle -->
<property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
<mapping assembly="OpenAPI.Model"/>
</session-factory>
</hibernate-configuration>
注意:如果使用Oracle数据还需更改实体类的映射文件信息,如:OpenAPI\OpenAPI.Model下的API\DocumentInfo.hbm.xml映射文件,代码如下:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="OpenAPI.Model" namespace="OpenAPI.Model.API">
<class name="OpenAPI.Model.API.DocumentInfo, OpenAPI.Model" table="T_API_DOCUMENT">
<id name="DOC_CD" column="DOC_CD" type="String" length="50">
<generator class="assigned" />
</id>
<property name="DOC_SUBJECT" column="DOC_SUBJECT" type="String" length="200" not-null ="true"/>
<property name="DOC_DATETIME" column="DOC_DATETIME" type="DateTime" not-null ="true"/>
<property name="DOC_AUTHOR" column="DOC_AUTHOR" type="String" length="50" />
<property name="DOC_TAGS" column="DOC_TAGS" type="String" length="50" />
<property name="DOC_BODY" type="StringClob">
<column name="DOC_BODY" sql-type="NText"></column>
</property>
</class>
</hibernate-mapping>
其中sql-type="NText"需要修改为sql-type="NCLOB"
5、配置Log4net信息
参照appSettings配置节中:
引用的扩展配置文件内容如下:
log4net.cfg.xml
<log4net>
<root>
<!--如果只需要看看Sql设置INFO就够了,如果你要调试可以设置为DEBUG或ALL-->
<priority value="INFO" />
<appender-ref ref="RollingFile_nhi" />
</root>
<logger name="ApplicationInfoLog">
<level value="DEBUG" />
<appender-ref ref="RollingFile_sys" />
</logger>
<appender name="RollingFile_nhi" type="log4net.Appender.RollingFileAppender,log4net" >
<param name="File" value="D:\51API\log\" />
<param name="AppendToFile" value="true" />
<param name="RollingStyle" value="Date" />
<param name="DatePattern" value="yyyyMMdd"_nhi.log"" />
<param name="StaticLogFileName" value="false" />
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] <%X{auth}> - %m%n" />
</layout>
</appender>
<appender name="RollingFile_sys" type="log4net.Appender.RollingFileAppender,log4net" >
<param name="File" value="D:\51API\log\" />
<param name="AppendToFile" value="true" />
<param name="RollingStyle" value="Date" />
<param name="DatePattern" value="yyyyMMdd"_sys.log"" />
<param name="StaticLogFileName" value="false" />
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] <%X{auth}> - %m%n" />
</layout>
</appender>
</log4net>
6、配置后台管理账户
参见appSettings配置节中如下代码:
<add key="MANAGE_USERID" value="xiaojun"/>
<add key="MANAGE_PASSWORD" value="1234"/>
<!-- 管理Account END -->
7、运行
运行OpenAPI.Web.Mvc项目默认使用6000端口,即:http://localhost:6000/ ,IIS设置在此不进行介绍。使用web.config中配置的管理帐号登录(记得勾选“Log on as manager”),登录后转到ManageTab选项,界面如下:
8、初始化
在上面界面中点击Schema Manage--Create/Drop/Init菜单,进行Schema创建、初始化数据、删除操作,截图如下:
9、消费方(Consumer)登录
初始化数据库后,系统中会自动增加一个Consumer,在登录画面中默认填充上了,直接输入验证码登录(不必勾选“Log on as manager”)。消费方可以使用Document、Test两个Tab选项,其中Test为在线测试环境,目前提供了GET/POST两种调用方式,截图如下: