由于工作需要,项目里用的是Ibatis.Net。所以就花时间学习学习,做做笔记吧。
1、打开SQLServer 2008,建一个数据库Test,一张表Person,添加入数据如下:
2、打开Vs2015, 新建一个控制台应用程序和两个类库项目如下:
其中,DAO是数据访问层,Model是实体层。然后DAO层需要引入Ibatis.Net的引用,这里使用NuGet添加:
其中PersonDAO的代码如下:
public class PersonDao { public IList<PersonModel> GetList() { ISqlMapper mapper = Mapper.Instance(); IList<PersonModel> ListPerson = mapper.QueryForList<PersonModel>("SelectAllPerson", null);//这个"SelectAllPerson"就是xml映射文件的Id return ListPerson; } }
PersonModel代码如下:
public class PersonModel { public int Id { get; set; } public string Name { get; set; } }
3、配置(包括xml映射文件,SqlMap.config,providers.config)
首先新建SqlMap.config,providers.config,Person.xml 。 在控制台程序中,将它们放到 bin\Debug 目录里。
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="../../../properties.config"/>这个东西是变量用的,这里以最简单的实现说明,因此注释--> <settings> <setting useStatementNamespaces="false"/> </settings> <providers resource="providers.config"/> <!--数据库连接字符串--> <database> <provider name="sqlServer2.0"/> <dataSource name="Test" connectionString="server=...;uid=...;pwd=...;database=Test"/> </database> <sqlMaps> <sqlMap resource="Person.xml" /> <!--这个是指定映射文件的位置--> </sqlMaps> </sqlMapConfig>
映射文件Person.xml:
<?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"> <!--这个Id在程序中会用到,resultClass是实体类所在的程序位置(带命名空间)--> select * from person </select> </statements> </sqlMap>
最后是Providers.config。根据你使用的数据库,将文件中提供版本的enabled="true"就可以了,此处我用“sqlServer2.0”的,可以把其它全部删除(不删除也可以)。
<?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 7.0/2000, 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="@" /> <provider name="sqlServer1.1" description="Microsoft SQL Server 7.0/2000, 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="@" /> <provider name="sqlServer2.0" description="Microsoft SQL Server, provider V2.0.0.0 in framework .NET V2.0" enabled="true" default="true" 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 = "" /> <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 = "@" /> <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" /> <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" /> <provider name="oracleClient1.0" description="Oracle, Microsoft provider V1.0.5000.0" enabled="false" default="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=":" /> <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="@" /> <provider name="MySql" description="MySQL, MySQL provider V1.0.4.20163" enabled="false" assemblyName="MySql.Data, Version=1.0.4.20163, 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="@" /> <provider name="SQLite3" 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 = "true" useParameterPrefixInSql = "false" useParameterPrefixInParameter = "false" parameterPrefix="" setDbParameterPrecision="false" setDbParameterScale="false" setDbParameterSize="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="@" /> <provider name="PostgreSql0.7" description="PostgreSql, Npgsql provider V0.7.0.0" enabled="false" assemblyName="Npgsql, Version=0.7.0.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=":" /> <provider name="iDb2.10" 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 = "" /> </providers>
Program.cs代码如下:
static void Main(string[] args) { PersonDAO dao = new PersonDAO(); IList<PersonModel> listPerson = dao.GetList(); foreach (PersonModel p in listPerson) { Console.WriteLine(p.Id +" - "+ p.Name); } Console.ReadKey(); }
运行程序,显示结果如下:
上面就是Ibatis.Net的一个简单的Demo,下篇会继续学习其它要点
对于上面的程序,将数据库由SQLServer改为Oracle,上面不用改代码,只需要将providers.config文件name="oracle10.1"的enabled="true",SqlMap.cofig里的database节点改为对应Oracle的连接字符串就行
参考:http://www.cnblogs.com/caoyc/category/873268.html