
IBatis.net使用说明
发布日期:2021-05-14 04:37:13
浏览次数:18
分类:博客文章
本文共 6776 字,大约阅读时间需要 22 分钟。
IBatis.net是移植于java版本,所以在.net平台上的操作和java平台上的操作基本没有什么太大的差别。严格来说,ibatis.net并不算的上是一种orm,因为它的sql语句均写到了配置文件中,没有自己专门的sql映射语句。但是相比Nhibernate来说,不仅简易,而且方便易学。学习成本还是很低的。
回到正规话题上来,实例IBatis.net需要两个类库:IBatisNet.DataMapper.dll和IBatisNet.Common.dll,新建项目,我的项目结构如下:
然后在使用ibatis.net框架的时候,需要配置三个config文件,第一个为database.config文件,里面则是配置数据库连接等信息的记录节点,如下图所示:
第二个配置文件为providers.config,则记录了数据驱动的详细信息,可以通过在database.config中的provider节点来设置使用哪一个数据驱动。
第三个配置文件为sqlmap.config,这个文件则是用来进行一些数据集方面的配置。
需要注意的是,在项目中,所有的xml文件和.config文件,都需要设置为嵌入的资源才行。
下面开始参看代码:
首先是SQLHelper.cs的代码:
View Code
using IBatisNet.DataMapper; using System.Reflection; using System.IO; using IBatisNet.DataMapper.Configuration; using IBatisNet.DataMapper.Configuration.Statements; using IBatisNet.DataMapper.MappedStatements; using IBatisNet.DataMapper.Scope; using System.Data; using IBatisNet.Common; using System; namespace SQLMaps{ public class SQLHelper { public static ISqlMapper SqlMap; private static readonly object syncObj = new object (); /// <summary> /// ISqlMapper对象生成 /// </summary> static SQLHelper() { if (SqlMap == null ) { lock (syncObj) { if (SqlMap == null ) { Assembly asembly = Assembly.Load( " SQLMaps " ); Stream stream = asembly.GetManifestResourceStream( " SQLMaps.sqlmap.config " ); // 得到xml数据流 DomSqlMapBuilder builder = new DomSqlMapBuilder(); SqlMap = builder.Configure(stream); } } } } /// <summary> /// 返回DataSet数据集 /// </summary> public static DataSet QueryForDataSet( string tag, object paramObject) { DataSet ds = new DataSet(); IDbCommand command = GetDbCommand(tag,paramObject); SqlMap.LocalSession.CreateDataAdapter(command).Fill(ds); return ds; } /// <summary> /// 返回DataTable数据集 /// </summary> public static DataTable QueryForDataTable( string tag, object paramObject) { return QueryForDataSet(tag, paramObject).Tables[ 0 ]; } /// <summary> /// 用于分页的DataTable数据集 /// </summary> public static DataTable QueryForDataTable( string tag, object paramObject, int PageSize, int curPage, out int recCount) { IDataReader dr = null ; bool isSessionLocal = false ; string sql = QueryForSql(tag,paramObject); string strCount = " select count(*) " + sql.Substring(sql.ToLower().IndexOf( " from " )); IDalSession session = SqlMap.LocalSession; DataTable dt = new DataTable(); if (session == null ) { session = new SqlMapSession(SqlMap); session.OpenConnection(); isSessionLocal = true ; } try { IDbCommand cmdCount = GetDbCommand(tag, paramObject); cmdCount.Connection = session.Connection; cmdCount.CommandText = strCount; object count = cmdCount.ExecuteScalar(); recCount = Convert.ToInt32(count); IDbCommand cmd = GetDbCommand(tag, paramObject); cmd.Connection = session.Connection; dr = cmd.ExecuteReader(); dt = QueryForPaging(dr, PageSize, curPage); } finally { if (isSessionLocal) { session.CloseConnection(); } } return dt; } /// <summary> /// 获取数据 /// </summary> protected static DataTable QueryForPaging(IDataReader dataReader, int PageSize, int curPage) { DataTable 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))); } // 读取数据,将DataReader中的数据读取到DataTable中去 object [] vald = new object [colCount]; int iCount = 0 ; // 临时记录变量 while (dataReader.Read()) { // 当前记录在当前页记录范围内 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; } }}
具体的model类我就不说了,至于BLL类,则是根据配置文件来进行操作的,具体的如下:
View Code
public IList < Person > GetAllPerson() { return SqlMap.QueryForList < Person > ( " SelectAllPerson " , null ); } public DataTable GetAllPerson2() { int recCount = 0 ; DataTable dt = SQLHelper.QueryForDataTable( " SelectAllPerson2 " , new Person { FirstName = " kilo " }, 10 , 1 , out recCount); return dt; } public DataTable GetAllPerson3() { return SQLHelper.QueryForDataTable( " SelectAllPerson2 " , new Person { FirstName = " kilo " }); } public Person GetPerson( int id) { return SqlMap.QueryForObject < Person > ( "" ,id); } public int Create(Person person) { int Id = ( int )SqlMap.Insert( " InsertPerson " ,person); SqlMap.CommitTransaction(); return Id; } public void Update(Person person) { SqlMap.Update( " UpdatePerson " ,person); } public void Delete(Person person) { SqlMap.Delete( " DeletePerson " ,person); }
而这些操作是根据Person.xml的sql语句来的,具体内容如下:
View Code
<? xml version="1.0" encoding="utf-8" ?> < sqlMap namespace ="Person" xmlns ="http://ibatis.apache.org/mapping" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" > < alias > < typeAlias alias ="Person" type ="Model.Person,Model" /> </ alias > < resultMaps > < resultMap id ="SelectAllResult" class ="Person" > < result property ="Id" column ="PER_ID" /> < result property ="FirstName" column ="PER_FIRST_NAME" /> < result property ="LastName" column ="PER_LAST_NAME" /> < result property ="BirthDate" column ="PER_BIRTH_DATE" /> < result property ="WeightInKilograms" column ="PER_WEIGHT_KG" /> < result property ="HeightInMeters" column ="PER_HEIGHT_M" /> </ resultMap > </ resultMaps > < statements > < select id ="SelectAllPerson" resultMap ="SelectAllResult" > select PER_ID, PER_FIRST_NAME, PER_LAST_NAME, PER_BIRTH_DATE, PER_WEIGHT_KG, PER_HEIGHT_M from PERSON </ select > < select id ="SelectAllPerson2" resultMap ="SelectAllResult" > select PER_ID AS Id, PER_FIRST_NAME AS FirstName, PER_LAST_NAME AS LastName, PER_BIRTH_DATE AS BirthDate, PER_WEIGHT_KG AS WeightInKilograms, PER_HEIGHT_M AS HeightInMeters from PERSON < dynamic prepend ="WHERE" > < isParameterPresent > PER_FIRST_NAME LIKE #FirstName#+'%' </ isParameterPresent > </ dynamic > </ select > < select id ="SelectByPersonId" resultClass ="Person" parameterClass ="int" > select PER_ID, PER_FIRST_NAME, PER_LAST_NAME, PER_BIRTH_DATE, PER_WEIGHT_KG, PER_HEIGHT_M from PERSON where PER_ID = #value# </ select > < insert id ="InsertPerson" parameterclass ="Person" > < selectKey property ="Id" type ="post" resultClass ="int" > ${selectKey} </ selectKey > insert into Person ( PER_FIRST_NAME, PER_LAST_NAME, PER_BIRTH_DATE, PER_WEIGHT_KG, PER_HEIGHT_M) values (#FirstName#,#LastName#,#BirthDate#, #WeightInKilograms#, #HeightInMeters#) </ insert > < update id ="UpdatePerson" parameterclass ="Person" > <![CDATA[ update Person set PER_FIRST_NAME =#FirstName#, PER_LAST_NAME =#LastName#, PER_BIRTH_DATE =#BirthDate#, PER_WEIGHT_KG=#WeightInKilograms#, PER_HEIGHT_M=#HeightInMeters# where PER_ID = #Id# ]]> </ update > < delete id ="DeletePerson" parameterclass ="Person" > delete from Person where PER_ID = #Id# </ delete > </ statements > </ sqlMap >
下面的文章将会整合log4net开发,并且更加详细。谢谢
发表评论
最新留言
留言是一种美德,欢迎回访!
[***.207.175.100]2025年04月08日 22时54分53秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
linux--练习001-基础类型
2019-03-11
python内存地址和编译字节码
2019-03-11
Flask--简介
2019-03-11
Flask模板--过滤器与测试器
2019-03-11
16 python基础-恺撒密码
2019-03-11
06.1 python基础--结构控制
2019-03-11
Frame--Api框架
2019-03-11
Frame--WEB框架
2019-03-11
idea 在Debug 模式中运行语句中函数的方法
2019-03-11
springboot2.1.1开启druid数据库连接池并开启监控
2019-03-11
《朝花夕拾》金句摘抄(五)
2019-03-11
《朝花夕拾》金句摘抄(六)
2019-03-11
《金色梦乡》金句摘抄(六)
2019-03-11
Boostrap技能点整理之【网格系统】
2019-03-11
新闻发布项目——业务逻辑层(UserService)
2019-03-11
hibernate正向生成数据库表以及配置——hibernate.cfg.xml
2019-03-11
javaWeb服务详解(含源代码,测试通过,注释) ——Emp的Dao层
2019-03-11