IBatis.net 是2001年发起的开源项目,它是一个轻量级的ORM框架,现在IBatisNET已经是属于Apache下的一个子项目了,最新版本是1.6.2.

官方网站:http://www.mybatis.org/

.net项目下载地址:http://code.google.com/p/mybatisnet/

DataMapper:通过配置映射关系的xml业务对象与SQL语句和存储过程进行映射.

DataAcces:简单的说就是IBatis的数据访问层.

这里通过一个简单的增删改查案例 进行学习 Ibatis.net的配置和使用

一、首先需要下载Ibatis.net 的dll.上面的官网估计下载不下来,所以这儿我自己上传了一份

下载地址:

IBatis.net1.9.2&1.6.2最新版本

本项目的 Demo:

asp.net MVC和IBatis.net整合demo程序

本项目的数据库:

asp.net MVC和IBatis.net整合demo数据库部分

二、使用VS 2013新建一个解决方案。

首先使用sqlserver2014 建立数据库表

数据库:UserDemoDb

并建立相关的架构  如图所示

IBatisDemo.Dao 提供一个统一的Mapper访问接口,

IBatisDemo.Model 数据实体

IBatisDemo.Service 数据操作

因为是做Demo没有对整体架构做过多的细节设置.

三、IBatis.net配置

web层拷贝的配置文件,这些文件在 Ibatis.net1.9.2的程序中 解压就有

providers.config 这个直接拷贝到根目录,该文件定义各种数据库的驱动,包括SqlServer, Oracle, MySQL, PostgreSQL, DB2 and OLEDB, ODBC 等。

sqlmap.config 就是非常核心的一个配置文件,主要配置了数据库访问字符串,settings设置,以及配置实体类和数据库表相关xml。

还有一个database.config 文件,它是配置一些在sqlmap中用到得参数.

添加对Ibatis dll的引用

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">
<settings>
<!--启用命名空间,对于多个表的时候,进行区分-->
<setting useStatementNamespaces="true"/>
</settings>
<!--连接数据库驱动文件-->
<providers resource="providers.config"/> <!-- Database connection information -->
<database>
<!--配置数据库连接字符串-->
<provider name="sqlServer2.0"></provider>
<dataSource name="IBatisNet" connectionString="server=WWW;database=UserDemoDb;user id=sa;password=DDD;connection reset=false;"/>
</database>
<sqlMaps>
<!--引用数据库表实体xml文件-->
<sqlMap resource="Maps/UserInfo.xml" />
</sqlMaps> </sqlMapConfig>

  

先配置网站根目录下的Maps/UserInfo.xml如下:

<?xml version="1.0" encoding="utf-8" ?>

<sqlMap namespace="UserInfo" xmlns="http://ibatis.apache.org/mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >

  <alias>
<!-- alias:取别名
assembly:表示类所在的文件
type:表示该类的完整的名称
-->
<typeAlias alias="UserInfo" assembly="IBatisDemo.Model" type="IBatisDemo.Model.UserInfo" />
</alias> <statements> <select id="select_UserInfoAll" resultMap="UserInfo-result"> select Id,UserName,Age
from UserInfo
</select>
<insert id="insert_UserInfoOne" parameterClass="UserInfo">
INSERT INTO UserInfo(
[UserName],[Age]
)VALUES(
#UserName#,#Age#
)
<selectKey type="post" resultClass="int" property="Id">
SELECT CAST(@@IDENTITY as int) as Id
</selectKey> </insert> <delete id="del_UserInfoOne" parameterClass="UserInfo">
<![CDATA[
DELETE UserInfo
]]>
<dynamic prepend="WHERE">
Id = #Id#
</dynamic>
</delete> <select id="select_UserInfoOne" resultMap="UserInfo-result">
select * from UserInfo
<dynamic prepend="where">
<isParameterPresent property="id" prepend="WHERE">
[Id] = #Id#
</isParameterPresent>
</dynamic>
</select> <update id="update_UserInfoOne" parameterClass="UserInfo">
<![CDATA[
UPDATE UserInfo
SET
UserName = #UserName#,
Age = #Age#
]]>
<dynamic prepend="WHERE">
Id = #Id#
</dynamic>
</update> </statements> <resultMaps > <resultMap id="UserInfo-result" class="UserInfo"> <result property="Id" column="Id" /> <result property="UserName" column="UserName" /> <result property="Age" column="Age" /> </resultMap> </resultMaps> </sqlMap>

  

说明:

statements 节点:

在这些容器标签中有一些常用的属性如下所示

resultMap和resultclass对比:

1、resultMap属于直接映射,可以把结果集中的数据库字段与实体类中的属性一一对应,这样通过select语句得到的结果就会准确的对上号

2、resultclass属于隐身映射,虽然你指定resultclass=“”,具体某一个类,但是select语句得到的结果是一条实力记录,但如果数据库字段与类的属性名字不一致,这个时候就会出现映射错误,有一种方式可以解决就是在写select语句时,给每个字段用as运算符取名字与属性一样:例如:select realname as name...其中realname是字段列名,name是属性字段名

3、resultmap比resultclass性能要高。尽量使用resultmap

insert标签下的selectKey  是表示返回刚插入数据的主键id,具体说明如下

<!-- 为了使insert操作能够返回插入记录的id,必须为insert写一个selectKey 

建立数据库实体类:UserInfo.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace IBatisDemo.Model
{
public class UserInfo
{
public int Id { get; set; }
public string UserName { get; set; }
public int Age { get; set; }
}
}

  Mapper.cs 获取Mapper的对象类:

using IBatisNet.Common.Utilities;
using IBatisNet.DataMapper;
using IBatisNet.DataMapper.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace IBatisDemo.Dao
{
public class Mapper
{
private static volatile ISqlMapper _mapper = null; protected static void Configure(object obj)
{
_mapper = null;
} protected static void InitMapper()
{
ConfigureHandler handler = new ConfigureHandler(Configure);
DomSqlMapBuilder builder = new DomSqlMapBuilder();
_mapper = builder.ConfigureAndWatch(handler);
} public static ISqlMapper Instance()
{
if (_mapper == null)
{
lock (typeof(SqlMapper))
{
if (_mapper == null) // double-check
{
InitMapper();
}
}
}
return _mapper;
} public static ISqlMapper Get()
{
return Instance();
} /// <summary>
/// RealMarket Mapper
/// </summary>
public static ISqlMapper GetMaper
{
get
{
if (_mapper == null)
{
lock (typeof(ISqlMapper))
{
if (_mapper == null)
{
ConfigureHandler hander = new ConfigureHandler(Configure);
DomSqlMapBuilder builder = new DomSqlMapBuilder();
_mapper = builder.ConfigureAndWatch("SqlMap.config", hander);
}
}
}
return _mapper;
}
}
}
}

  然后再Service里面建立UserInfoService.cs 数据访问

using IBatisDemo.Dao;
using IBatisDemo.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Data.SqlClient; namespace IBatisDemo.Service
{
public class UserInfoService
{
public int UserInfoInsertOne(UserInfo userInfo)
{
Object obj = Mapper.GetMaper.Insert("UserInfo.insert_UserInfoOne", userInfo);
return (int)obj;
} public UserInfo GetUserInfo(int id)
{
return (UserInfo)Mapper.GetMaper.QueryForObject("UserInfo.select_UserInfoOne", id);
} public IList<UserInfo> GetUserInfoList()
{
//xml里面配置的格式
return Mapper.GetMaper.QueryForList<UserInfo>("UserInfo.select_UserInfoAll", null);
} public int DelUserInfoOne(int id)
{
Object obj = Mapper.GetMaper.Delete("UserInfo.del_UserInfoOne", id);
return (int)obj;
}
public int UpdateUserInfo(UserInfo userInfo)
{
Object obj = Mapper.GetMaper.Update("UserInfo.update_UserInfoOne", userInfo);
return (int)obj;
}
}
}

  最后在web层 controller文件夹下建立 HomeController.cs   控制器

using IBatisDemo.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using IBatisDemo.Model; namespace IBatisDemo.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
UserInfoService service = new UserInfoService();
#region 显示员工 public ActionResult Index()
{ IList<UserInfo> userInfos = service.GetUserInfoList();
ViewData["list"] = userInfos;
return View();
} #endregion #region 添加员工 [HttpGet]
public ActionResult UserInsert()
{
return View();
}
[HttpPost]
public ActionResult UserInsert(UserInfo userInfo)
{
userInfo.UserName = Request["UserName"];
userInfo.Age = int.Parse(Request["Age"]);
if (service.UserInfoInsertOne(userInfo) > 0)
{
return Redirect("Index");
}
else
{
return Content("添加失败");
} } #endregion #region 删除员工 public ActionResult delUserInfo(int id)
{
id = int.Parse(Request["Id"]);
if (service.DelUserInfoOne(id) > 0)
{
return Redirect("Index"); }
else
{
return Content("删除失败");
}
}
#endregion #region 编辑员工资料
[HttpGet]
public ActionResult getUserInfo(int Id)
{
Id = int.Parse(Request["Id"]);
UserInfo userInfos = service.GetUserInfo(Id);
//ViewData["user"] = userInfos;
return View(userInfos);
}
[HttpPost]
public ActionResult getUserInfo(UserInfo userInfo)
{
userInfo.Id = int.Parse(Request["Id"]);
userInfo.UserName = Request["UserName"];
userInfo.Age = int.Parse(Request["Age"]);
if (service.UpdateUserInfo(userInfo) > 0)
{
return Redirect("Index");
}
else
{
return Content("修改失败");
}
} #endregion }
}

  View层  Index.cshtml

@{
Layout = null;
}
@using IBatisDemo.Model
<!DOCTYPE html> <html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div>
<h1>IBatis 学习Demo</h1>
@if (ViewData["List"] != null)
{
<table style="width:100%;text-align:center;" id="tabs">
<tr><th>编号</th><th>姓名</th><th>年龄</th><th>详细</th><th>删除</th><th>修改</th></tr>
@foreach (var newInfo in (IList<UserInfo>)ViewData["List"])
{
<tr>
<td>@newInfo.Id</td>
<td>@newInfo.UserName</td>
<td>@newInfo.Age</td>
<td><a href="javascript:void(0)" class="details" ids="@newInfo.Id">详细</a></td>
<td><a href="/home/delUserInfo?Id=@newInfo.Id" class="deletes">删除</a></td>
<td><a href="/home/getUserInfo?Id=@newInfo.Id" class="edits">修改</a></td>
</tr>
}
</table>
}
else
{
<span>暂无数据</span>
}
<a href="/home/UserInsert">添加</a>
</div>
</body>
</html>

  编辑和添加的模板 直接在添加视图的时候生成就可以了,源码里面都有,这儿就不贴出来了

下面是运行效果:


IBatis.net在asp.net MVC下的使用的更多相关文章

  1. ASP.NET MVC下的四种验证编程方式[续篇]

    在<ASP.NET MVC下的四种验证编程方式>一文中我们介绍了ASP.NET MVC支持的四种服务端验证的编程方式("手工验证"."标注Validation ...

  2. ASP.NET MVC下的四种验证编程方式

    ASP.NET MVC采用Model绑定为目标Action生成了相应的参数列表,但是在真正执行目标Action方法之前,还需要对绑定的参数实施验证以确保其有效性,我们将针对参数的验证成为Model绑定 ...

  3. Response.End()在Webform和ASP.NET MVC下的表现差异

    前几天在博问中看到一个问题--Response.End()后,是否停止执行?MVC与WebForm不一致.看到LZ的描述后,虽然奇怪于为何用Response.End()而不用return方式去控制流程 ...

  4. ASP.NET MVC下的四种验证编程方式[续篇]【转】

    在<ASP.NET MVC下的四种验证编程方式> 一文中我们介绍了ASP.NET MVC支持的四种服务端验证的编程方式(“手工验证”.“标注ValidationAttribute特性”.“ ...

  5. ASP.NET MVC下的四种验证编程方式【转】

    ASP.NET MVC采用Model绑定为目标Action生成了相应的参数列表,但是在真正执行目标Action方法之前,还需要对绑定的参数实施验证以确保其有效 性,我们将针对参数的验证成为Model绑 ...

  6. ASP.NET MVC下使用AngularJs语言(六):获取下拉列表的value和Text

    前面Insus.NET有在Angularjs实现DropDownList的下拉列表的功能.但是没有实现怎样获取下拉列表的value和text功能. 下面分别使用ng-click和ng-change来实 ...

  7. ASP.NET MVC下使用AngularJs语言(五):ng-selected

    这次学习ng-selected语法,这个是为DropDownList下拉列表显示默认选项. 演示从下面步骤开始 1,新建一个model: 上面#14行代码的property,数据类型为bool.即是存 ...

  8. ASP.NET MVC下使用AngularJs语言(二):ng-click事件

    程序用户交互,用户使用mouse点击,这是一个普通的功能. 在angularjs的铵钮点击命令是ng-click. 创建Angularjs的app使用前一篇<ASP.NET MVC下使用Angu ...

  9. ASP.NET MVC下使用AngularJs语言(一):Hello your name

    新春节后,分享第一个教程. 是教一位新朋友全新学习ASP.NET MVC下使用AngularJs语言. 一,新建一个空的Web项目.使用NuGet下载AngularJs和jQuery.二,配置Bund ...

随机推荐

  1. 分享一个字数限制和统计的UITextView分类方法

    - (NSUInteger)letterCountWithLimits:(NSInteger)limits { NSString *toBeString = self.text; NSUInteger ...

  2. Android_AsyncTask_DownloadImg

    layout.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xml ...

  3. chosen 下拉框

    $("#teams").trigger("liszt:updated");//更新重新绑定                            $(" ...

  4. mapping 详解5(dynamic mapping)

    概述 在使用 ES 的时,我们不需要事先定义好映射设置就可以直接向索引中导入文档.ES 可以自动实现每个字段的类型检测,并进行 mapping 设置,这个过程就叫动态映射(dynamic mappin ...

  5. 关于Eclipse插件开发(一)

    plugin.xml是插件和Eclipse内核的接口,Eclipse就像一所大宅子.它的外墙(plugin.xml)有很多门(扩展点), 要熟练进入这座大宅子,就得先搞清楚它有那些门(扩展点). 插件 ...

  6. 关于JFace中的向导式对话框(WizardDialog类)

    向导式对话框是一种非常友好的界面,它能够引导用户一步步的输入信息.Eclipse的"新建项目",就是这样的向导式对话框. 在Eclipse中向导式对话框的开发是很简单的,它由Wiz ...

  7. matlab中num2str的应用

    在求导数,积分,方程的过程中,难免会遇到一些参数要随着情况有点变化,这时,你就需要能够动态的表示出你的表达式,Num2str函数是一个相当有用的函数,一般配合[]连接符使用,下面将我接触到的一些用法写 ...

  8. Microsoft SQL Server Product Samples:Database

    从SQL Server 2005 之后示例数据都为AdventureWorks,需要的通过codeplex网站下载.这样设计的目的应该在于是生产库行不必要的用户以及权限分配. 从以下网址访问http: ...

  9. openlayers wfs获取要素

    var wfsProtocol = new OpenLayers.Protocol.WFS.v1_1_0({ url: mapServerUrl + "/wfs", feature ...

  10. HW--漂亮度

    描述 给出一个名字,该名字有26个字符串组成,定义这个字符串的“漂亮度”是其所有字母“漂亮度”的总和.每个字母都有一个“漂亮度”,范围在1到26之间.没有任何两个字母拥有相同的“漂亮度”.字母忽略大小 ...