Spring.NET 中的 ADO.NET 数据访问的示例
2014-02-27 13:10
423 查看
Spring.NET 1.3.1 中提供了一个使用 AdoTemplate 的完整示例,包括使用泛型和使用非泛型技术进行数据访问,这个示例位于下载的压缩包中\Spring.NET-1.3.1\Spring.NET\examples\Spring\Spring.DataQuickStart。Spring.NET 的下载地址:http://www.springframework.net/download.html。示例中的数据来源于微软的示例数据库 Northwind,这个数据库可以从微软的网站下载,下载地址:http://www.microsoft.com/downloads/en/details.aspx?FamilyId=06616212-0356-46A0-8DA2-EEBC53A68034&displaylang=en
Spring.NET 提供了两种风格的 ADO.NET 访问,第一种 template 风格,通过一个单例的 AdoTemplate 应用在所有的数据访问实现中,AdoTemplate 提供了通过一个方法调用来完成数据访问的机制,这个机制类似于微软的 DAAB。这个类位于命名空间 Spring.Data.Core,在程序集Spring.Data 中。另外一种方式更加面向对象一些,比如通过 AdoQuery 来完成查询任务,而 AdoNonQuery 完成创建、更新、删除任务,存储过程则通过 StoredProcedure 来完成。这几个类都定义在命名空间 Spring.Data.Objects 中,位于程序集 Spring.Data。
在 Spring.NET 中,为了方便使用 AdoTemplate,使用 AdoDaoSupport 封装了 AdoTemplate。AdoDaoSupport 定义在命名空间Spring.Data.Core 下,具体的定义如下:
通常情况下,我们从 AdoDaoSupport 派生一个用于数据访问的类,这样就可以直接通过 AdoDaoSupport 来使用 AdoTemplate 了。例如,示例中的 QueryForObjectDao 的代码。
测试程序中,首先初始化 Spring 容器。这里的配置文件是嵌入在程序集中的。
在代码中,就可以通过容器直接取得 QueryForObjectDao 对象使用了。
Spring.NET 提供了两种风格的 ADO.NET 访问,第一种 template 风格,通过一个单例的 AdoTemplate 应用在所有的数据访问实现中,AdoTemplate 提供了通过一个方法调用来完成数据访问的机制,这个机制类似于微软的 DAAB。这个类位于命名空间 Spring.Data.Core,在程序集Spring.Data 中。另外一种方式更加面向对象一些,比如通过 AdoQuery 来完成查询任务,而 AdoNonQuery 完成创建、更新、删除任务,存储过程则通过 StoredProcedure 来完成。这几个类都定义在命名空间 Spring.Data.Objects 中,位于程序集 Spring.Data。
在 Spring.NET 中,为了方便使用 AdoTemplate,使用 AdoDaoSupport 封装了 AdoTemplate。AdoDaoSupport 定义在命名空间Spring.Data.Core 下,具体的定义如下:
<!-- 连接串定义 --> <db:provider id="dbProvider" provider="System.Data.SqlClient" connectionString="Data Source=.\SQL2005;Initial Catalog=Northwind;Persist Security Info=True;User ID=springqa;Password=springqa;Trusted_Connection=False"/> <!-- AdoTemplate 定义 --> <object id="adoTemplate" type="Spring.Data.Core.AdoTemplate, Spring.Data"> <!-- 注入连接 --> <property name="DbProvider" ref="dbProvider"/> <property name="DataReaderWrapperType" value="Spring.Data.Support.NullMappingDataReader, Spring.Data"/> </object> <!-- 查询定义 --> <object id="queryForObjectDao" type="Spring.DataQuickStart.Dao.Template.QueryforObjectDao, Spring.DataQuickStart"> <!-- 注入 AdoTemplate --> <property name="AdoTemplate" ref="adoTemplate"/> </object>
测试程序中,首先初始化 Spring 容器。这里的配置文件是嵌入在程序集中的。
[SetUp] publicvoid InitContext() { // Configure Spring programmatically NamespaceParserRegistry.RegisterParser(typeof(DatabaseNamespaceParser)); ctx =new XmlApplicationContext( "assembly://Spring.DataQuickStart.Tests/Spring.DataQuickStart.Template/ExampleTests.xml"); adoTemplate = ctx["adoTemplate"] as AdoTemplate; }
在代码中,就可以通过容器直接取得 QueryForObjectDao 对象使用了。
[Test] publicvoid QueryForObjectDaoTest() { QueryForObjectDao dao = ctx["queryForObjectDao"] as QueryForObjectDao; Customer customer = dao.GetCustomer("Hanna Moos"); Assert.AreEqual(customer.ContactName, "Hanna Moos"); }
相关文章推荐
- Spring.NET 中的 ADO.NET 数据访问的示例
- ADO和ADO.NET在数据访问上的区别
- ADO.NET数据访问模板整理
- ADO.NET数据访问模式
- MOSS 2010:Visual Studio 2010开发体验(20)——使用ADO.NET Data Service快速访问SharePoint列表数据
- Ado.net批量添加和更新数据简单示例
- ADO与ADO.NET两种数据访问方式区别
- ADO.NET中的数据访问--强类型
- ADO.NET数据访问方式:DataSet
- ADO 与ADO.NET两种数据访问方式区别。
- ADO.NET的数据访问对象
- 【Asp.Net】——ADO.NET为访问数据插上了翅膀
- 数据访问:使用 ADO.NET 的最佳实践(ADO.NET 技术文档)
- 数据访问:使用 ADO.NET 的最佳实践(ADO.NET 技术文档)
- ADO.net实体框架访问不到数据库最新数据问题
- VB2005(1、使用ADO.NET访问数据库--Windows窗体中的数据体系结构)
- ADO 与ADO.NET两种数据访问方式区别?
- 数据访问技术系列课程 笔记(2) ADO.NET 连接方式进行数据访问
- 数据访问:使用 ADO.NET 的最佳实践
- ADO.NET数据访问方式:强类型DataSet