iBatis for net 框架使用
簡介:ibatis?一詞來源于“internet”和“abatis”的組合,是一個由Clinton?Begin在2001年發(fā)起的開放源代碼項目,到后面發(fā)展的版本叫MyBatis但都是指的同一個東西。最初側(cè)重于密碼軟件的開發(fā),現(xiàn)在是一個基于Java的持久層框架,漸漸的也把這種模式轉(zhuǎn)移到了net。相對NHibernate等“一站式”O(jiān)RM解決方案而言,ibatis?是一種“半自動化”的ORM實現(xiàn)。
下載地址:http://code.google.com/p/mybatisnet/downloads/list?can=3?
本文開發(fā)Demo環(huán)境:IBatis.DataMapper.1.6.2.bin?+?MsSql?2005?+?Vs?2010
使用步驟:
在使用之前,為大家推薦一款代碼生成器,也是我專為此框架寫的一款代碼生成器,生成iBatis的Map配置文件,這樣一來,用戶不用開任何關(guān)于框架的說明文檔,即可使用此框架。代碼生成器(CodeHelper)下載地址與說明:
http://www.cnblogs.com/stone_w/archive/2011/09/26/2192010.html?
第一步:創(chuàng)建數(shù)據(jù)庫與新建網(wǎng)站項目。
創(chuàng)建數(shù)據(jù)庫與表的T-sql:
/*
*?iBatisDemo?Sql
*?Stone?(QQ:1370569)
*?Date:2011.09.26
*/
create?database?OrmDB
go
use?OrmDB
go
create?table?People
(
id?int?primary?key?identity,
[name]?varchar(250),
age?int
)
go
打開vs開發(fā)工具,新建網(wǎng)站項目。
第二步:為網(wǎng)站添加iBatis框架支持。
為網(wǎng)站添加引用或者直接把下載的iBatis框架中的IBatisNet.Common.dll和IBatisNet.DataMapper.dll復(fù)制到Bin目錄下。
第三步:添加iBatis配置文件。
iBatis的配置文件只用三種,也必須要有這三種:
1.providers.config[文件名不能修改]此文件為數(shù)據(jù)庫開發(fā)商提供的驅(qū)動配置集合,是固定不變的,無需修改與配置,復(fù)制文件到網(wǎng)站的根目錄,代碼如下:
providers.config <?xml version="1.0" encoding="utf-8"?><providers
xmlns="http://ibatis.apache.org/providers"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<clear/>
<provider
name="sqlServer1.0"
description="Microsoft SQL Server, provider V1.0.3300.0 in framework .NET V1.0"
enabled="false"
assemblyName="System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" connectionClass="System.Data.SqlClient.SqlConnection"
commandClass="System.Data.SqlClient.SqlCommand"
parameterClass="System.Data.SqlClient.SqlParameter"
parameterDbTypeClass="System.Data.SqlDbType"
parameterDbTypeProperty="SqlDbType"
dataAdapterClass="System.Data.SqlClient.SqlDataAdapter"
commandBuilderClass="System.Data.SqlClient.SqlCommandBuilder"
usePositionalParameters="false"
useParameterPrefixInSql="true"
useParameterPrefixInParameter="true"
parameterPrefix="@"
allowMARS="false"
/>
<provider
name="sqlServer1.1"
description="Microsoft SQL Server, provider V1.0.5000.0 in framework .NET V1.1"
enabled="false"
assemblyName="System.Data, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
connectionClass="System.Data.SqlClient.SqlConnection"
commandClass="System.Data.SqlClient.SqlCommand"
parameterClass="System.Data.SqlClient.SqlParameter"
parameterDbTypeClass="System.Data.SqlDbType"
parameterDbTypeProperty="SqlDbType"
dataAdapterClass="System.Data.SqlClient.SqlDataAdapter"
commandBuilderClass="System.Data.SqlClient.SqlCommandBuilder"
usePositionalParameters="false"
useParameterPrefixInSql="true"
useParameterPrefixInParameter="true"
parameterPrefix="@"
allowMARS="false"
/>
<provider
name="sqlServer2.0"
enabled="true"
description="Microsoft SQL Server, provider V2.0.0.0 in framework .NET V2.0"
assemblyName="System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
connectionClass="System.Data.SqlClient.SqlConnection"
commandClass="System.Data.SqlClient.SqlCommand"
parameterClass="System.Data.SqlClient.SqlParameter"
parameterDbTypeClass="System.Data.SqlDbType"
parameterDbTypeProperty="SqlDbType"
dataAdapterClass="System.Data.SqlClient.SqlDataAdapter"
commandBuilderClass=" System.Data.SqlClient.SqlCommandBuilder"
usePositionalParameters = "false"
useParameterPrefixInSql = "true"
useParameterPrefixInParameter = "true"
parameterPrefix="@"
allowMARS="false"
/>
<provider
name="sqlServer2005"
enabled="false"
description="Microsoft SQL Server, provider V2.0.0.0 in framework .NET V2.0"
assemblyName="System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
connectionClass="System.Data.SqlClient.SqlConnection"
commandClass="System.Data.SqlClient.SqlCommand"
parameterClass="System.Data.SqlClient.SqlParameter"
parameterDbTypeClass="System.Data.SqlDbType"
parameterDbTypeProperty="SqlDbType"
dataAdapterClass="System.Data.SqlClient.SqlDataAdapter"
commandBuilderClass=" System.Data.SqlClient.SqlCommandBuilder"
usePositionalParameters = "false"
useParameterPrefixInSql = "true"
useParameterPrefixInParameter = "true"
parameterPrefix="@"
allowMARS="true"
/>
<provider name="OleDb1.1"
description="OleDb, provider V1.0.5000.0 in framework .NET V1.1"
enabled="false"
assemblyName="System.Data, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
connectionClass="System.Data.OleDb.OleDbConnection"
commandClass="System.Data.OleDb.OleDbCommand"
parameterClass="System.Data.OleDb.OleDbParameter"
parameterDbTypeClass="System.Data.OleDb.OleDbType"
parameterDbTypeProperty="OleDbType"
dataAdapterClass="System.Data.OleDb.OleDbDataAdapter"
commandBuilderClass="System.Data.OleDb.OleDbCommandBuilder"
usePositionalParameters="true"
useParameterPrefixInSql="false"
useParameterPrefixInParameter="false"
parameterPrefix=""
allowMARS="false"
/>
<provider name="OleDb2.0"
description="OleDb, provider V2.0.0.0 in framework .NET V2"
enabled="false"
assemblyName="System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
connectionClass="System.Data.OleDb.OleDbConnection"
commandClass="System.Data.OleDb.OleDbCommand"
parameterClass="System.Data.OleDb.OleDbParameter"
parameterDbTypeClass="System.Data.OleDb.OleDbType"
parameterDbTypeProperty="OleDbType"
dataAdapterClass="System.Data.OleDb.OleDbDataAdapter"
commandBuilderClass="System.Data.OleDb.OleDbCommandBuilder"
usePositionalParameters="true"
useParameterPrefixInSql="false"
useParameterPrefixInParameter="false"
parameterPrefix=""
allowMARS="false"
/>
<provider
name="Odbc1.1"
description="Odbc, provider V1.0.5000.0 in framework .NET V1.1"
enabled="false"
assemblyName="System.Data, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
connectionClass="System.Data.Odbc.OdbcConnection"
commandClass="System.Data.Odbc.OdbcCommand"
parameterClass="System.Data.Odbc.OdbcParameter"
parameterDbTypeClass="System.Data.Odbc.OdbcType"
parameterDbTypeProperty="OdbcType"
dataAdapterClass="System.Data.Odbc.OdbcDataAdapter"
commandBuilderClass="System.Data.Odbc.OdbcCommandBuilder"
usePositionalParameters="true"
useParameterPrefixInSql="false"
useParameterPrefixInParameter="false"
parameterPrefix="@"
allowMARS="false"
/>
<provider
name="Odbc2.0"
description="Odbc, provider V2.0.0.0 in framework .NET V2"
enabled="false"
assemblyName="System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
connectionClass="System.Data.Odbc.OdbcConnection"
commandClass="System.Data.Odbc.OdbcCommand"
parameterClass="System.Data.Odbc.OdbcParameter"
parameterDbTypeClass="System.Data.Odbc.OdbcType"
parameterDbTypeProperty="OdbcType"
dataAdapterClass="System.Data.Odbc.OdbcDataAdapter"
commandBuilderClass="System.Data.Odbc.OdbcCommandBuilder"
usePositionalParameters="true"
useParameterPrefixInSql="false"
useParameterPrefixInParameter="false"
parameterPrefix="@"
allowMARS="false"
/>
<provider
name="oracle9.2"
description="Oracle, Oracle provider V9.2.0.401"
enabled="false"
assemblyName="Oracle.DataAccess, Version=9.2.0.401, Culture=neutral, PublicKeyToken=89b483f429c47342" connectionClass="Oracle.DataAccess.Client.OracleConnection"
commandClass="Oracle.DataAccess.Client.OracleCommand"
parameterClass="Oracle.DataAccess.Client.OracleParameter"
parameterDbTypeClass="Oracle.DataAccess.Client.OracleDbType"
parameterDbTypeProperty="OracleDbType"
dataAdapterClass="Oracle.DataAccess.Client.OracleDataAdapter"
commandBuilderClass="Oracle.DataAccess.Client.OracleCommandBuilder"
usePositionalParameters="false"
useParameterPrefixInSql="true"
useParameterPrefixInParameter="false"
parameterPrefix=":"
useDeriveParameters="false"
allowMARS="false"
/>
<provider
name="oracle10.1"
description="Oracle, oracle provider V10.1.0.301"
enabled="false"
assemblyName="Oracle.DataAccess, Version=10.1.0.301, Culture=neutral, PublicKeyToken=89b483f429c47342" connectionClass="Oracle.DataAccess.Client.OracleConnection"
commandClass="Oracle.DataAccess.Client.OracleCommand"
parameterClass="Oracle.DataAccess.Client.OracleParameter"
parameterDbTypeClass="Oracle.DataAccess.Client.OracleDbType"
parameterDbTypeProperty="OracleDbType"
dataAdapterClass="Oracle.DataAccess.Client.OracleDataAdapter"
commandBuilderClass="Oracle.DataAccess.Client.OracleCommandBuilder"
usePositionalParameters="true"
useParameterPrefixInSql="true"
useParameterPrefixInParameter="true"
parameterPrefix=":"
useDeriveParameters="false"
allowMARS="false"
/>
<provider
name="oracleClient1.0"
description="Oracle, Microsoft provider V1.0.5000.0"
enabled="false"
assemblyName="System.Data.OracleClient, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" connectionClass="System.Data.OracleClient.OracleConnection"
commandClass="System.Data.OracleClient.OracleCommand"
parameterClass="System.Data.OracleClient.OracleParameter"
parameterDbTypeClass="System.Data.OracleClient.OracleType"
parameterDbTypeProperty="OracleType"
dataAdapterClass="System.Data.OracleClient.OracleDataAdapter"
commandBuilderClass="System.Data.OracleClient.OracleCommandBuilder"
usePositionalParameters="false"
useParameterPrefixInSql="true"
useParameterPrefixInParameter="false"
parameterPrefix=":"
allowMARS="false"
/>
<provider
name="ByteFx"
description="MySQL, ByteFx provider V0.7.6.15073"
enabled="false"
assemblyName="ByteFX.MySqlClient, Version=0.7.6.15073, Culture=neutral, PublicKeyToken=f2fef6fed1732fc1" connectionClass="ByteFX.Data.MySqlClient.MySqlConnection"
commandClass="ByteFX.Data.MySqlClient.MySqlCommand"
parameterClass="ByteFX.Data.MySqlClient.MySqlParameter"
parameterDbTypeClass="ByteFX.Data.MySqlClient.MySqlDbType"
parameterDbTypeProperty="MySqlDbType"
dataAdapterClass="ByteFX.Data.MySqlClient.MySqlDataAdapter"
commandBuilderClass="ByteFX.Data.MySqlClient.MySqlCommandBuilder"
usePositionalParameters="false"
useParameterPrefixInSql="true"
useParameterPrefixInParameter="true"
parameterPrefix="@"
allowMARS="false"
/>
<provider
name="MySql"
description="MySQL, MySQL provider 1.0.7.30072"
enabled="false"
assemblyName="MySql.Data, Version=1.0.7.30072, Culture=neutral, PublicKeyToken=c5687fc88969c44d" connectionClass="MySql.Data.MySqlClient.MySqlConnection"
commandClass="MySql.Data.MySqlClient.MySqlCommand"
parameterClass="MySql.Data.MySqlClient.MySqlParameter"
parameterDbTypeClass="MySql.Data.MySqlClient.MySqlDbType"
parameterDbTypeProperty="MySqlDbType"
dataAdapterClass="MySql.Data.MySqlClient.MySqlDataAdapter"
commandBuilderClass="MySql.Data.MySqlClient.MySqlCommandBuilder"
usePositionalParameters="false"
useParameterPrefixInSql="true"
useParameterPrefixInParameter="true"
parameterPrefix="?"
allowMARS="false"
/>
<provider name="SQLite3 Finisar"
description="SQLite, SQLite.NET provider V0.21.1869.3794"
enabled="false"
assemblyName="SQLite.NET, Version=0.21.1869.3794, Culture=neutral, PublicKeyToken=c273bd375e695f9c"
connectionClass="Finisar.SQLite.SQLiteConnection"
commandClass="Finisar.SQLite.SQLiteCommand"
parameterClass="Finisar.SQLite.SQLiteParameter"
parameterDbTypeClass="System.Data.DbType, System.Data"
parameterDbTypeProperty="DbType"
dataAdapterClass="Finisar.SQLite.SQLiteDataAdapter"
commandBuilderClass="Finisar.SQLite.SQLiteCommandBuilder"
usePositionalParameters="false"
useParameterPrefixInSql="true"
useParameterPrefixInParameter="true"
parameterPrefix="@"
setDbParameterPrecision="false"
setDbParameterScale="false"
allowMARS="false"
/>
<provider name="SQLite3"
description="SQLite, SQLite.NET provider V1.0.43.0"
enabled="false"
assemblyName="System.Data.SQLite, Version=1.0.43.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139"
connectionClass="System.Data.SQLite.SQLiteConnection"
commandClass="System.Data.SQLite.SQLiteCommand"
parameterClass="System.Data.SQLite.SQLiteParameter"
parameterDbTypeClass="System.Data.SQLite.SQLiteType"
parameterDbTypeProperty="DbType"
dataAdapterClass="System.Data.SQLite.SQLiteDataAdapter"
commandBuilderClass="System.Data.SQLite.SQLiteCommandBuilder"
usePositionalParameters="false"
useParameterPrefixInSql="true"
useParameterPrefixInParameter="true"
parameterPrefix="@"
setDbParameterPrecision="false"
setDbParameterScale="false"
allowMARS="false"
/>
<provider
name="Firebird1.7"
description="Firebird, Firebird SQL .NET provider V1.7.0.33200"
enabled="false"
assemblyName="FirebirdSql.Data.Firebird, Version=1.7.0.33200, Culture=neutral, PublicKeyToken=fa843d180294369d" connectionClass="FirebirdSql.Data.Firebird.FbConnection"
commandClass="FirebirdSql.Data.Firebird.FbCommand"
parameterClass="FirebirdSql.Data.Firebird.FbParameter"
parameterDbTypeClass="FirebirdSql.Data.Firebird.FbDbType"
parameterDbTypeProperty="FbDbType"
dataAdapterClass="FirebirdSql.Data.Firebird.FbDataAdapter"
commandBuilderClass="FirebirdSql.Data.Firebird.FbCommandBuilder"
usePositionalParameters="false"
useParameterPrefixInSql="true"
useParameterPrefixInParameter="true"
parameterPrefix="@"
allowMARS="false"
/>
<provider
name="PostgreSql0.99.1.0"
description="PostgreSql, Npgsql provider V0.99.1.0"
enabled="false"
assemblyName="Npgsql, Version=0.99.1.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7"
connectionClass="Npgsql.NpgsqlConnection"
commandClass="Npgsql.NpgsqlCommand"
parameterClass="Npgsql.NpgsqlParameter"
parameterDbTypeClass="NpgsqlTypes.NpgsqlDbType"
parameterDbTypeProperty="NpgsqlDbType"
dataAdapterClass="Npgsql.NpgsqlDataAdapter"
commandBuilderClass="Npgsql.NpgsqlCommandBuilder"
usePositionalParameters="false"
useParameterPrefixInSql="true"
useParameterPrefixInParameter="true"
parameterPrefix=":"
allowMARS="true"
/>
<provider
name="iDb2.10"
description="IBM DB2 Provider, V 10.0"
enabled="false"
assemblyName="IBM.Data.DB2.iSeries, Version=10.0.0.0,Culture=neutral, PublicKeyToken=9cdb2ebfb1f93a26, Custom=null" connectionClass="IBM.Data.DB2.iSeries.iDB2Connection"
commandClass="IBM.Data.DB2.iSeries.iDB2Command"
parameterClass="IBM.Data.DB2.iSeries.iDB2Parameter"
parameterDbTypeClass="IBM.Data.DB2.iSeries.iDB2DbType"
parameterDbTypeProperty="iDB2DbType"
dataAdapterClass="IBM.Data.DB2.iSeries.iDB2DataAdapter"
commandBuilderClass="IBM.Data.DB2.iSeries.iDB2CommandBuilder"
usePositionalParameters="true"
useParameterPrefixInSql="false"
useParameterPrefixInParameter="false"
parameterPrefix=""
allowMARS="false"
/>
<provider
name="Informix"
description="Informix NET Provider, 2.81.0.0"
enabled="false"
assemblyName="IBM.Data.Informix, Version=2.81.0.0, Culture=neutral, PublicKeyToken=7c307b91aa13d208"
connectionClass="IBM.Data.Informix.IfxConnection"
commandClass="IBM.Data.Informix.IfxCommand"
parameterClass="IBM.Data.Informix.IfxParameter"
parameterDbTypeClass="IBM.Data.Informix.IfxType"
parameterDbTypeProperty="IfxType"
dataAdapterClass="IBM.Data.Informix.IfxDataAdapter"
commandBuilderClass="IBM.Data.Informix.IfxCommandBuilder"
usePositionalParameters = "true"
useParameterPrefixInSql = "false"
useParameterPrefixInParameter = "false"
useDeriveParameters="false"
allowMARS="false"
/>
</providers>
2.SQLMap.config[文件名不可修改],配置連接數(shù)據(jù)庫字符串與每個實體類(表)的映射文件配置,復(fù)制文件到網(wǎng)站的根目錄,代碼如下:
<?xml?version="1.0"?encoding="utf-8"?>
<sqlMapConfig
??xmlns="http://ibatis.apache.org/dataMapper"
??xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
????<settings>
????????<!--/是否使用Satement命名空間,這里的命名空間指的是映射文件中sqlMap節(jié)點的namespace屬性,默認是false-->
????????<setting?useStatementNamespaces="false"/>
????????<!--是否啟用DataMapper的緩存機制,針對全部的SqlMap,默認是true-->
????????<setting?cacheModelsEnabled="true"/>
????????<!--是否啟用SqlMapConfig.xsd?schema驗證映射文件,默認是false-->
????????<setting?validateSqlMap="false"/>
????</settings>
????<!--配置數(shù)據(jù)驅(qū)動提供類配置文件的路徑和文件名-->
????<providers?resource="providers.config"/>
????<!--如果在providers.config文件中指定了默認的數(shù)據(jù)庫驅(qū)動,那么provider節(jié)點就不需要設(shè)置了,它的作用是在換數(shù)據(jù)庫驅(qū)動時不需要修改providers.config文件。datasource節(jié)點用于指定ADO.NET?Connection?String.-->
????<database>
????????<provider?name="sqlServer2.0"/>
????????<dataSource?name="mydb"?connectionString="data?source=127.0.0.1;database=MyBatisDB;user?id=sa;password=sa;"/>
????</database>
????<!--指定映射的文件的位置-->
????<sqlMaps>
????????<sqlMap?resource="Maps/PeopleMap.xml"/>
????</sqlMaps>
</sqlMapConfig>
3.每個實體類(表)都有一個單獨的配置文件類,類型為xml。代碼生成器也是生成這個類的,這樣一來,用戶就可以幾秒鐘創(chuàng)建開發(fā)一個iBatis項目了,實體類的配置文件格式如下,在網(wǎng)站的根目錄創(chuàng)建一個Map的文件夾,新建xxxMap.xml格式的配置類,比如:PeopleMap.xml文件,示例代碼如下:
<?xml?version="1.0"?encoding="utf-8"??>
<!--namespace必須用否者就報錯(讀取配置文件報:未將對象引用設(shè)置到對象的實例)-->
<sqlMap?namespace=""?xmlns="http://ibatis.apache.org/mapping"?xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
????<statements>
????????<select?id="GetPeopleById"?parameterClass="string"?resultClass="HashTable">
????????????SELECT?*?FROM?People?WHERE?Id=#Id#
????????</select>
????????<insert?id="Add"?parameterClass="hashtable">
????????????INSERT??dbo.People?(name?,age)
????????????VALUES??(#Name#?,?#Age#)
????????</insert>
????????<delete?id="Delete"?parameterClass="int"?restultClass="int">
????????????DELETE?FROM?dbo.People?WHERE?Id=#Id#
????????</delete>
????????<update?id="Update"?parameterClass="hashtable"?restltClass="int">
????????????UPDATE?People?SET?name=#Name#?WHERE?Id=#Id#
????????</update>
????????<select?id="GetAll"?resultClass="HashTable">
????????????SELECT?*?FROM?People
????????</select>
????</statements>
</sqlMap>
這三類配置文件必須都要有,與其他orm框架不同的是,web.config什么也不用配置,及時練數(shù)據(jù)庫連接字符串也不是在它的文件中配置的,也就是web.config是沒有被用到的。
第四步:iBatis框架語法與使用。
使用之前先來加入一個DB輔助類IBatisHepler.cs,代碼如下:
IBatisHepler.cs using System;using System.Collections.Generic;
using System.Text;
using IBatisNet.DataMapper.Configuration.Statements;
using IBatisNet.DataMapper;
using IBatisNet.DataMapper.MappedStatements;
using IBatisNet.Common;
using IBatisNet.DataMapper.Scope;
using System.Data;
using IBatisNet.DataMapper.Configuration;
using System.Collections;
/// <summary>
/// IBatis 使用工具類
/// </summary>
public class IBatisHepler
{
private static ISqlMapper mapper = null;
static IBatisHepler()
{
DomSqlMapBuilder builder = new DomSqlMapBuilder();//其作用是根據(jù)配置文件創(chuàng)建SqlMap實例。
mapper = builder.Configure("SqlMap.config") as SqlMapper;//SqlMapper是iBatisNet的核心組件,提供數(shù)據(jù)庫操作的基礎(chǔ)平臺。SqlMapper可通過DomSqlMapBuilder創(chuàng)建。
}
/// <summary>
/// 執(zhí)行添加操作
/// </summary>
/// <param name="tag">語句ID</param>
/// <param name="paramObject">語句所需要的參數(shù)</param>
/// <returns>添加的主鍵</returns>
public static object ExcuteInsert(string tag, Hashtable paramObject)
{
return mapper.Insert(tag, paramObject);
}
/// <summary>
/// 執(zhí)行刪除操作
/// </summary>
/// <param name="tag">語句ID</param>
/// <param name="paramObject">語句所需要的參數(shù)</param>
/// <returns>影響行數(shù)</returns>
public static int ExcuteDelete(string tag, object paramObject)
{
return mapper.Delete(tag, paramObject);
}
/// <summary>
/// 執(zhí)行修改操作
/// </summary>
/// <param name="tag">語句ID</param>
/// <param name="paramObject">語句所需要的參數(shù)</param>
/// <returns>影響行數(shù)</returns>
public static int ExcuteUpdate(string tag, Hashtable paramObject)
{
return mapper.Update(tag, paramObject);
}
/// <summary>
/// 查詢單個實體對象
/// </summary>
/// <param name="tag">語句ID</param>
/// <param name="paramObject">語句所需要的參數(shù)</param>
/// <returns>得到的Object</returns>
public static object ExcuteSelectObj(string tag, object paramObject)
{
object obj = mapper.QueryForObject(tag, paramObject);
return obj;
}
/// <summary>
/// 分頁查詢
/// </summary>
/// <param name="tag">語句ID</param>
/// <param name="paramObject">語句所需要的參數(shù)</param>
/// <param name="pageIndex">頁索引</param>
/// <param name="pageSize">每頁顯示的記錄數(shù)</param>
/// <returns>得到的List集合</returns>
public static IList ExcuteSelect(string tag, object paramObject, int pageIndex, int pageSize)
{
return mapper.QueryForList(tag, paramObject, pageIndex, pageSize);
}
/// <summary>
/// 通用的以DataTable的方式得到Select的結(jié)果(xml文件中參數(shù)要使用$標(biāo)記的占位參數(shù))
/// </summary>
/// <param name="tag">語句ID</param>
/// <param name="paramObject">語句所需要的參數(shù)</param>
/// <returns>得到的DataTable</returns>
public static DataTable ExcuteSelect(string tag, object paramObject)
{
DataSet ds = new DataSet();
bool isSessionLocal = false;
ISqlMapSession session = mapper.LocalSession;
if (session == null)
{
session = mapper.CreateSqlMapSession();
session.OpenConnection();
isSessionLocal = true;
}
try
{
IDbCommand cmd = session.CreateCommand(CommandType.Text);
cmd.Connection = session.Connection;
cmd.CommandText = GetSql(tag, paramObject);
IDbDataAdapter adapter = session.CreateDataAdapter(cmd);
adapter.Fill(ds);
}
finally
{
if (isSessionLocal)
{
session.CloseConnection();
}
}
return ds.Tables[0];
}
/// <summary>
/// 得到參數(shù)化后的SQL
/// </summary>
/// <param name="tag">語句ID</param>
/// <param name="paramObject">語句所需要的參數(shù)</param>
/// <returns>sql語句</returns>
protected static string GetSql(string tag, object paramObject)
{
IStatement statement = mapper.GetMappedStatement(tag).Statement;
IMappedStatement mapStatement = mapper.GetMappedStatement(tag);
ISqlMapSession session = mapper.CreateSqlMapSession();
RequestScope request = statement.Sql.GetRequestScope(mapStatement, paramObject, session);
return request.PreparedStatement.PreparedSql;
}
/// <summary>
/// 用于分頁控件使用
/// </summary>
/// <param name="tag">語句ID</param>
/// <param name="paramObject">語句所需要的參數(shù)</param>
/// <param name="PageSize">每頁顯示數(shù)目</param>
/// <param name="curPage">當(dāng)前頁</param>
/// <param name="recCount">記錄總數(shù)</param>
/// <returns>得到的DataTable</returns>
public static DataTable ExcuteSelect(string tag, object paramObject, int PageSize, int curPage, out int recCount)
{
IDataReader dr = null;
bool isSessionLocal = false;
string sql = GetSql(tag, paramObject);
string strCount = "select count(*) " + sql.Substring(sql.ToLower().IndexOf("from"));
IDalSession session = mapper.LocalSession;
DataTable dt = new DataTable();
if (session == null)
{
session = mapper.CreateSqlMapSession();
session.OpenConnection();
isSessionLocal = true;
}
try
{
IDbCommand cmdCount = session.CreateCommand(CommandType.Text);
cmdCount.Connection = session.Connection;
cmdCount.CommandText = strCount;
object count = cmdCount.ExecuteScalar();
recCount = Convert.ToInt32(count);
IDbCommand cmd = session.CreateCommand(CommandType.Text);
cmd.Connection = session.Connection;
cmd.CommandText = sql;
dr = cmd.ExecuteReader();
dt = Paging(dr, PageSize, curPage);
}
finally
{
if (isSessionLocal)
{
session.CloseConnection();
}
}
return dt;
}
/**/
/// <summary>
/// 取回合適數(shù)量的數(shù)據(jù)
/// </summary>
/// <param name="dataReader"></param>
/// <param name="PageSize"></param>
/// <param name="curPage"></param>
/// <returns></returns>
protected static DataTable Paging(IDataReader dataReader, int PageSize, int curPage)
{
DataTable dt;
dt = new DataTable();
int colCount = dataReader.FieldCount;
for (int i = 0; i < colCount; i++)
{
dt.Columns.Add(new DataColumn(dataReader.GetName(i), dataReader.GetFieldType(i)));
}
// 讀取數(shù)據(jù)。將DataReader中的數(shù)據(jù)讀取到DataTable中
object[] vald = new object[colCount];
int iCount = 0; // 臨時記錄變量
while (dataReader.Read())
{
// 當(dāng)前記錄在當(dāng)前頁記錄范圍內(nèi)
if (iCount >= PageSize * (curPage - 1) && iCount < PageSize * curPage)
{
for (int i = 0; i < colCount; i++)
vald[i] = dataReader.GetValue(i);
dt.Rows.Add(vald);
}
else if (iCount > PageSize * curPage)
{
break;
}
iCount++; // 臨時記錄變量遞增
}
if (!dataReader.IsClosed)
{
dataReader.Close();
dataReader.Dispose();
}
return dt;
}
}
使用語法如下:
//?添加引用
using?System.Collections;
using?IBatisNet.DataMapper;
using?IBatisNet.DataMapper.Configuration;
//?1.添加
Hashtable?ht_add?=?new?Hashtable();
ht_add["Name"]?=?"IBatis";??//?[注意]大小寫要和xxxMap.xml里保持一致
ht_add["Age"]?=?2001;
IBatisHepler.ExcuteInsert("Add",?ht_add);
//?2.修改
Hashtable?ht_up?=?new?Hashtable();
ht_up["Id"]?=?"1";
ht_up["Name"]?=?"IBatis";
ht_up["Age"]?=?2001;
IBatisHepler.ExcuteUpdate("Update",?ht_up);
//?3.刪除
IBatisHepler.ExcuteDelete("Delete",?14);
//?4.查詢
Repeater1.DataSource?=?IBatisHepler.ExcuteSelectObj("GetPeopleById",?"1");
Repeater1.DataBind();
我對此框架的評價:配置文件有點多,用戶需要去寫東西比較多,調(diào)用查詢不是很好用,尤其是條件查詢很不方便。
代碼下載:【①.本Demo下載】
Orm框架相關(guān)文章鏈接:
1.NHibernate使用之詳細圖解
2.iBatis for net 框架使用
3.iBatis for Net 代碼生成器(CodeHelper)附下載地址
4.NBear簡介與使用圖解?
5.SubSonic框架使用圖解
6.GentleNet使用之詳細圖解
總結(jié)
以上是生活随笔為你收集整理的iBatis for net 框架使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: springboot发送qq邮件
- 下一篇: Ext.grid.CheckboxSel