1、MyBatis.net介绍

MyBatis..net是一个简单,但是完整的ORM框架,它使你的实体对象与sql语句或者存储过程之间的映射变得很简单,并提供数据访问。包括两个主要框架

DataAccess FrameWork和DataMapper FrameWork

DataAccessObject Framework and DataMapper Framework are completely separate and are not dependent on each other in any way.Please feel  free to use either one separately,or both together.

2、MyBatis.net配置

主要的配置文件

providers.config:用来定义框架中用到的数据库引擎.

SqlMap.config:数据库连接信息,数据映射xml文件信息,设置信息

Default locations for the sqlMap.config and providers.config files

Windows, Library, or Test projects (using NUnit or    
equivalent)

This would be the binary folder (such as /bin/debug)
with the assembly (.dll) files and the App.config file

Web projects

In the application root, where the Web.config file is
located.

当providers.config在默认的位置的时候,SqlMap.config中可以不用指定它的位置。

主要的dll文件

    IBatisNet.Common.dll,IBatisNet.DataAccess.dll,在project或websit中添加引用即可。

3、MyBatis.net实战

 1、providers.config(eg.sqlserver2008)

<?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="sqlServer4.0"
enabled="true"
default="true"
description="Microsoft SQL Server, provider V4.0.0.0 in framework .NET V4.0"
assemblyName="System.Data, Version=4.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"
/>
</providers>

2、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="true"/>
<setting cacheModelsEnabled="true"/>
</settings> <providers resource="providers.config"/> <!-- Database connection information -->
<database>
<provider name="${provider}"/>
<dataSource name="MyDb" connectionString="${connectionString}"/>
</database> <sqlMaps>
<sqlMap resource="Maps/Account.xml" />
</sqlMaps> </sqlMapConfig>

3、properties.config

<?xml version="1.0" encoding="utf-8"?>
<settings>
<!-- User application and configured property settings go here.-->
<!-- Example: <add key="settingName" value="settingValue"/> --> <add key="provider" value="sqlServer4.0" />
<!--Data Source=10.8.8.18;Initial Catalog=Ets_JinBaiWan;User ID=sa;Password=sa;Application Name=jituanyitaodian_web" />--> <!--<add key="connectionString" value="Data Source=59.151.43.221;Initial Catalog=FoodkingdomCN_MianAiMian20140429;User ID=etaoshi_master;Password=Tr1iV8Q7sY005" />-->
<add key="connectionString" value="Server=.; User ID=sa;Password=sqltest;Database=TestDB;Persist Security Info=True" />
<!--<add key="connectionString" value="Data Source=59.151.43.221;Initial Catalog=FoodkingdomCN_Yunhaiyao;User ID=etaoshi_master;Password=Tr1iV8Q7sY005" />--> <add key="root" value="../" />
</settings>

4、sqlserver2008中创建表

5、创建实体

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace MyBatisDemo.Model
{
public class Accounts
{
public int Id { get; set; } public string Name { get; set; } public float Money { get; set; } public DateTime CreateDate { get; set; }
}
}

6、创建数据访问类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IBatisNet.Common.Utilities;
using IBatisNet.DataMapper;
using IBatisNet.DataMapper.Configuration;
using MyBatisDemo.Model; namespace MyBatisDemo.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;
}
}
} public class AccountService
{
public int TestInsertOne(Accounts account)
{
Object obj = Mapper.GetMaper.Insert("Account.sql_InsertOne", account);
return (int)obj;
} public Accounts GetAccount(int id)
{
return (Accounts)Mapper.GetMaper.QueryForObject("Account.sql_selectByid", id);
} public IList<Accounts> GetAccountList()
{
return Mapper.GetMaper.QueryForList<Accounts>("Account.sql_selectAll", null);
}
}
}

7、配置O/R Maping映射xml(Account.xml)

<?xml version="1.0" encoding="utf-8" ?>
<sqlMap namespace="Account" xmlns="http://ibatis.apache.org/mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<alias>
<!-- alias:取别名
assembly:表示类所在的文件
type:表示该类的完整的名称
-->
<typeAlias alias="Account" assembly="MyBatisDemo.Model.dll" type="MyBatisDemo.Model.Accounts" />
</alias> <resultMaps>
<resultMap id="Account-result" class="Account">
<result property="Id" column="id"/>
<result property="Name" column="name"/>
<result property="Money" column="money"/>
<result property="CreateDate" column="createdate"/> </resultMap>
</resultMaps> <statements>
<select id="sql_selectByid" resultMap="Account-result">
select * from Account
<dynamic prepend="where">
<isParameterPresent property="id" prepend="">
[id] = #id#
</isParameterPresent>
</dynamic>
</select> <select id="sql_selectAll" resultMap="Account-result">
select * from Account
</select> <insert id="sql_InsertOne" parameterClass="Account">
insert into Account (name,money,createdate)
values
(#Name#,
#Money#,
#CreateDate#
)
<selectKey type="post" resultClass="int" property="Id">
SELECT CAST(@@IDENTITY as int) as Id
</selectKey>
</insert>
</statements>
</sqlMap>

  

8、程序调用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyBatisDemo.Dao;
using MyBatisDemo.Model; namespace MyBatisDemo
{
public partial class Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
try
{
Accounts account = new Accounts();
account.Id = -1;
account.Name = "qqp";
account.Money = 100000;
account.CreateDate = DateTime.Now; AccountService service = new AccountService();
service.TestInsertOne(account);
Response.Write("<script>alert('success')</script>");
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
}
}

  

初学MyBatis.net的更多相关文章

  1. SSM框架-初学Mybatis框架

    SSM(Spring+SpringMVC+Mybatis)是目前项目开发比较流行的一套组合框架,而Mybatis是负责数据库操作的那部分框架,具体 我也说不上来 传统的JDBC操作比较冗长而繁琐,而用 ...

  2. 初学mybatis和mysql碰到的问题

    今天学习了下使用mybatis操作数据库,期间也是各种问题出现,幸好现在网络发达,网络上很多都可以解决,现在总结一下: Exception in thread "main" org ...

  3. 初学Mybatis

    首先配置mybatis配置文件 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" &qu ...

  4. 初学MyBatis(踩坑)Error querying database. Cause: java.sql.SQLException: java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long

    最近在学习Mybatis,代码全部根据教程写好了,一运行结果报了一个错误,主要错误内容: Caused by: org.apache.ibatis.exceptions.PersistenceExce ...

  5. 每日一记-mybatis碰到的疑惑:String类型可以传入多个参数吗

    碰到一个觉得很疑惑的问题,Mybatis的parameterType为String类型的时候,能够接收多个参数的吗? 背景 初学Mybatis的时候,看的教程和书籍上都是在说基本的数据类型如:int. ...

  6. mybatis generator配置生成代码的问题

    接触第二种orm两天下来,一脸懵逼.mybatis是大多数公司所推崇的,相比于hibernate性能较为好的,操作更为方便的轻量级工具,所以小富就搞起这个orm.好吧,都说mybatis有个配置可以自 ...

  7. MyBatis源码解读(3)——MapperMethod

    在前面两篇的MyBatis源码解读中,我们一路跟踪到了MapperProxy,知道了尽管是使用了动态代理技术使得我们能直接使用接口方法.为巩固加深动态代理,我们不妨再来回忆一遍何为动态代理. 我相信在 ...

  8. Mybatis学习日志

    在Mybatis深入学习的一周中,总感觉跟着师傅的视屏讲解什么都能懂,但实际自己操作的时候才发现自己一脸懵逼,不知道从何入手.但还好自己做了点笔记.在此记录一下自己浅度学习Mybatis遇到几个小问题 ...

  9. 一看就懂的Mybatis框架入门笔记

    本篇为初学Mybatis框架时的入门笔记,整理发出 Spring集成Mybatis https://www.cnblogs.com/yueshutong/p/9381590.html SpringBo ...

随机推荐

  1. JS一个根据时区输出时区时间的函数

    做项目遇到的坑爹问题,需要根据时区获取时区中轴线的时间.为此搜了好久网上都没什么JS的代码描述到这一方面,最后自己翻了下高中地理才写了个函数出来. 此图可以看出来,全球分为了0时区,东西1-11区,第 ...

  2. 16.2.13 asp.net 学习随笔

    using System.Data.SqlClient;//连接数据库必须的 using System.Configuration; CommandType所在的命名空间 system.data; P ...

  3. aac格式解析

    AAC格式有以下两种: ADIF:Audio Data Interchange Format 音频数据交换格式.这种格式的特征是可以确定的找到这个音频数据的开始,不需进行在音频数据流中间开始的解码,即 ...

  4. c#知识总结1

    一.C#程序结构 一个c#程序主要包括以下部分 ①命名空间声明 ②一个class ③class方法 ④class属性 ⑤一个main方法 ⑥语句 和 表达式 以及 注释 简单的“Helloworld” ...

  5. Ubuntu 安装Redis体验

      背景:由于之前一直没有试过Linux的环境,今天加了内存之后,虚拟机开了3G,速度大大提高,对照博客试一下安装Redis的过程.   体验: 下载源码,解压,编译 $ wget http://do ...

  6. MVC ViewEngineResult实际上是一种设计

    概述 MVC中, IView代表一个视图,最后是要表现为HTML或者其他的HttpResponse的应答流的: IViewEngine提供了类似工厂的作用或者提供器的作用,以返回一个视图. OO的视觉 ...

  7. 【BZOJ】1606: [Usaco2008 Dec]Hay For Sale(背包)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1606 越来越水了T_T 这题两种做法,一个正规01背包,价值就是体积 还有一种是非正规背包,即 if ...

  8. HTTP请求中的User-Agent 判断浏览器类型的各种方法 网络爬虫的请求标示

    我们知道,当用户发送一个http请求的时候,浏览的的版本信息也包含在了http请求信息中: 如上图所示,请求 google plus 请求头就包含了用户的浏览器信息: User-Agent:Mozil ...

  9. java不用jni,也可以获得当前系统性能信息

    最近做个项目,就是要取得cpu占有率等等的系统信息,一开始以为要用动态链接库了,但后来发现可以像下面这样做,不去调用jni,这样省去了很多看新技术的时间o(∩_∩)o... 在Java中,可以获得总的 ...

  10. javamail发送邮件的简单实例

    今天学习了一下JavaMail,javamail发送邮件确实是一个比较麻烦的问题.为了以后使用方便,自己写了段代码,打成jar包,以方便以后使用.呵呵 以下三段代码是我的全部代码,朋友们如果想用,直接 ...