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 content="width=device-width" /> <title>Index</title> </head> <body> <div> <h1>IBatis 学习Demo</h1> @if (ViewData["List"] != null) { <table> <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)" ids="@newInfo.Id">详细</a></td> <td><a href="/home/delUserInfo?Id=@newInfo.Id">删除</a></td> <td><a href="/home/getUserInfo?Id=@newInfo.Id">修改</a></td> </tr> } </table> } else { <span>暂无数据</span> } <a href="/home/UserInsert">添加</a> </div> </body> </html>
编辑和添加的模板 直接在添加视图的时候生成就可以了,源码里面都有,这儿就不贴出来了
下面是运行效果: