IbatisNet

之前用过Ibatis架构,感觉还不错,不想干活就回忆一下之前的操作,写下笔记,以备后用

首先要在项目中引用几个dll,先看下结构图

之后要在编写添加providers.config该文件可以在下载,并且内容不需要修改

然后看下BaseService.cs内容

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using IBatisNet.DataMapper;
using IBatisNet.DataMapper.Configuration;
using IBatisNet.DataMapper.SessionStore;

namespace GraduationDesign.Service
{
public class BaseService
{
private static ISqlMapper sqlMap;

public ISqlMapper SqlMap
{
get { return sqlMap; }
}

public BaseService()
{
Assembly assembly = Assembly.Load("GraduationDesign.Service");//此处要改成自己的服务类

Stream stream = assembly.GetManifestResourceStream("GraduationDesign.Service.sqlmap.config");//此处是服务类中的sqlmap配置文件
DomSqlMapBuilder builder = new DomSqlMapBuilder();
sqlMap = builder.Configure(stream);
sqlMap.SessionStore = new HybridWebThreadSessionStore(sqlMap.Id);//可用于多线程调用
}
}
}

看下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" >

<!-- Rem : If used via a DataAccess context, properties tag will be ignored -->
<properties resource="database.config"/>//此处是你的数据库配置文件
<settings>
<!--
<setting useStatementNamespaces="${useStatementNamespaces}"/>
-->
<setting cacheModelsEnabled="true"/>
<setting validateSqlMap="false"/>
</settings>

<!-- Optional if resource -->
<!--<providers resource="../VodIC.Service/providers.config"/> -->

<providers embedded="GraduationDesign.Service.providers.config"/>//此处是providers配置文件
<!-- ==== SqlClient configuration ========= -->
<!-- Rem : If used via a DataAccess context, database tag will be ignored -->
<database>
<provider name="sqlServer1.1"/>
<dataSource name="Single_Server_GraduationDesign" connectionString="user id=${username};password=${password};data source=${datasource};database=${database};Integrated Security=${sspi};Connect Timeout=30;"/>
</database>
<sqlMaps>
<!-- user via embedded-->//该处是你的资源文件,也就是xml文件,你添加的资源文件必须在此配置
<sqlMap embedded="GraduationDesign.Service.map.SqlClient.BlogArticle.xml,GraduationDesign.Service"/>
<sqlMap embedded="GraduationDesign.Service.map.SqlClient.Event.xml,GraduationDesign.Service"/>
<sqlMap embedded="GraduationDesign.Service.map.SqlClient.Friend.xml,GraduationDesign.Service"/>
<sqlMap embedded="GraduationDesign.Service.map.SqlClient.Message.xml,GraduationDesign.Service"/>
<sqlMap embedded="GraduationDesign.Service.map.SqlClient.Profile.xml,GraduationDesign.Service"/>
<sqlMap embedded="GraduationDesign.Service.map.SqlClient.UserInfo.xml,GraduationDesign.Service"></sqlMap>
</sqlMaps>
</sqlMapConfig>

看个资源文件

<?xml version="1.0" encoding="utf-8" ?>
<sqlMap
namespace="Post"
xmlns="http://ibatis.apache.org/mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<alias>
<typeAlias alias="Event" type="GraduationDesign.Domain.Event,GraduationDesign.Domain"/>//该处是你的实体类
</alias>
<resultMaps>
<resultMap id="Event-Result" class="Event">
<result property="EventId" column="EventId"/>
<result property="CreatUserId" column="CreatUserId"/>
<result property="VisitId" column="VisitId"/>
<result property="VisitTime" column="VisitTime"/>
<result property="Type" column="Type"/>
<result property="Body" column="Body"/>
</resultMap>
</resultMaps>
<statements>
<!--根据事件承受者和事件类型获取事件-->
<select id="GetEventInfoByVisitIdAndType" resultMap="Event-Result" parameterClass="hashtable">//其中id是该查询语句的唯一标志,在服务方法中可以调用,resultMap是结果集的映射,此处选择Event-Result,那么结果的列就要和该映射中一致, parameterClass是参数类型可以是哈希表也可以是类也可以是整形字符串
select distinct * from Event where VisitId=#VisitId# and Type=#Type# and CreatUserId !=#VisitId# order by VisitTime desc;
</select>
<!--添加事件-->
<insert id="InsertEvent" parameterClass="Event">
INSERT
INTO [dbo].[Event](CreatUserId,VisitId,VisitTime,Type,Body)
VALUES(#CreatUserId#,#VisitId#,#VisitTime#,#Type#,#Body#)
<selectKey type="post" resultClass="int" property="EventId" >
select @@IDENTITY as value
</selectKey>
</insert>
<!--删除事件-->
<delete id="DeleteEventByEventId" parameterClass="int">
DELETE
FROM [dbo].[Event]
WHERE EventId=#EventId#
</delete>
</statements>
</sqlMap>

例外这些资源要把属性设置成内嵌,sqlmap,providers资源文件都要设置成嵌入

看个服务类是如何调用这些资源的

/// <summary>
/// 根据被访问ID和事件类型获得事件
/// </summary>
/// <param name="blogId"></param>
/// <returns></returns>
public IList<Event> GetEventInfoByVisitIdAndType(int blogId, string type)
{
IList<Event> events = new List<Event>();
Hashtable table = new Hashtable();
table.Add("VisitId", blogId);
table.Add("Type", type);
try
{
events = SqlMap.QueryForList<Event>("GetEventInfoByVisitIdAndType", table);
return events;
}
catch
{
throw;
}
}

原文地址:https://www.cnblogs.com/GreenGrass/p/2784592.html