Click here to Skip to main content
14,637,391 members
Articles » Database » Database » Data Access
Article
Posted 6 Apr 2015

Stats

67.8K views
3.9K downloads
16 bookmarked

MyBatis.NET

Rate this:
4.00 (3 votes)
Please Sign up or sign in to vote.
4.00 (3 votes)
21 Dec 2016CPOL
MyBatis.NET as DataMapper

Introduction

In this article I am going to show you a simple DEMO application in C# for using MyBatis.NET as a datamapper for accessing SQL SERVER. Before diving into coding , let us see what MyBatis.NET is and why we should use it.

What is MyBatis.NET

MyBatis is a data mapping tool. It maps columns of a database query including stored procedure to properties of a business object. One definition of mapper is “an object that sets up communication between two independent objects. A Data Mapper is a "layer of mappers that moves data between objects and a database while keeping them independent of each other and the mapper itself.

MyBatis is a good choice when your application is layered and business layer can be separated from UI layer. You provide the database and the objects; MyBatis provides the mapping layer that goes between the two. 

Why MyBatis.NET

.NET platform already provides a capable library for accessing databases, whether through SQL statements or stored procedures but several things are still hard to do well when using ADO.NET, including:

  • Separating SQL code from programming code
  • Passing input parameters to the library classes and extracting the output
  • Separating data access classes from business logic classes
  • Caching often-used data until it changes
  •  Managing transactions and threading

iBATIS DataMapper solves these problems -- and many more -- by using XML documents to create a mapping between a plain-old object and a SQL statement or a stored procedure. The "plain-old object" can be a IDictionary or property object.

Background

You might have seen everywhere the names ‘MyBatis’ and ‘iBatis’ are interchangeably used because MyBatis was developed with Apache Foundation under the name ‘iBatis’ until 19 May 2010 and then ‘iBatis’ project moved to Google Code with a new project name ‘MyBatis’. Originally MyBatis was developer for Java and recently the .NET version is called MyBatis.NET

Using the code

This is a simple DEMO application and it does not cover everything that MyBatis.NET can offer. It is a good starting point for someone who wants to use MyBatis.NET in their application as data mapper between a database and business objects. I am using SQL SERVER in this demo but you can use any other popular database that MyBatis supports.

 The following diagram shows MyBatis.NET datamapper workflow.

Image 1

 

First thing you need for the datamapper work is data map definition file (sqlMap.config). I have create the data map definition file as follows

<span style="color: rgb(17, 17, 17); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px;"><?xml version="1.0" encoding="utf-8" ?></span>

<sqlMapConfig xmlns="http://ibatis.apache.org/dataMapper"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

  <settings>
    <setting useStatementNamespaces="false" />
    <setting cacheModelsEnabled="true" />
    <setting validateSqlMap="true" />
  </settings>

  <database>
    <provider name="sqlServer2.0" />
    <dataSource name="MtBatisSQL" connectionString="Data Source=MachineName\SQL2008R2;Initial Catalog=MyBatisNet;Integrated Security=True"/>
  </database>

  <sqlMaps>
    <sqlMap embedded="sqlFile.xml, MyBatisDataMapper" />
  </sqlMaps>
</sqlMapConfig>

In the first section it sets parameters and then sets the provider and datasource , finally the sql mapping. In this case all the mappings are stored in a seperate file sqlFile.xml which is given below. 

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

  <statements>
    <statement id="FindDepartment" parameterClass="System.Int32" resultClass="System.String" >
      SELECT Name
      FROM Department
      WHERE Id =  #value#
    </statement>
    <procedure id="GetEmployees" parameterMap="get-employees-params"  resultMap="get-employee-result">
      GetEmployees
    </procedure>
  </statements>
  <parameterMaps>
  <parameterMap id="get-employees-params">
    <parameter property="Id" column="DepartmentId" />
  </parameterMap>
  </parameterMaps>
  <resultMaps>
    <resultMap id="get-employee-result" class="model.Employee">
      <result property="Id" column="Id" dbType="Int"/>
      <result property="Name" column="Name" dbType="Varchar"/>
      <result property="Age" column="Age" dbType="Varchar"/>
      <result property="DepartmentId" column="DepartmentId" dbType="Int"/>
    </resultMap>
  </resultMaps>
</sqlMap>

As you can see, I have created one simple FindDepartment statement using SELECT query that takes department id as int and returns department name as a string.

Also there is a stored procedure GetEmployees that uses a parameterMap and resultsMap. A parameter map is used to map parameter property (id in this case) to the column parameter in the stored procedure (DepartmentId in this case) before executing the stored procedure. The results map on the another hand maps the results (columns in the results set) from stored procedure to the object (properties in the Employee object).

Before we start coding we need more more config file. Remember in the sqlMap.config file , in the database element , it requires provider along with datasource. The provider is nothing but the driver information to connect to the specified database. In the above config we have specified 

<provider name="sqlServer2.0" />

we can have as many providers as you want and change the provider according to the enviroment delpoyed. The following is the provide we are going to use.

<?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="sqlServer2.0"
    enabled="true"
    default="false" 
    description="Microsoft SQL Server, provider V2.0.0.0 in framework .NET V2.0" 
    assemblyName="System.Data, Version=2.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"
    />

  <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>

 

Now let us start coding in C#. To access any mapped sql statements or stored procedures , we need SqlMapper instance from the configuration we have provided. The following code shows how to get SqlMapper instance

public static ISqlMapper EntityMapper
        {
            get
            {
                try
                {
                    ISqlMapper mapper = Mapper.Instance();
                    return mapper;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        } 

The ISqlMapper is available from the referenced library IBatisNet.DataMapper.dll.  Now the instance is available so we can execure the statement and storeprocedure as below

 

public static List<model.Employee> getEmployees(model.Department department)
{

    ISqlMapper mapper = EntityMapper;

    List<model.Employee> employees = mapper.QueryForList<model.Employee>("GetEmployees", department).ToList();
    return employees;

}

public static string FindDepartment(int deptId){

    ISqlMapper mapper = EntityMapper;
    string str = mapper.QueryForObject<string>("FindDepartment", deptId);
    return str;

}
//Find Department
string deptName = FindDepartment(1);
Console.WriteLine(deptName);
Console.Read();

// Get Employees
model.Department dep = new model.Department();
dep.Id = 1;
List<model.Employee> emps = getEmployees(dep);
Console.ReadLine();

 

Either it is a statment or SP , it does not differentiate in terms of executing the code. The only difference is if the resulting type is a list then we have to use QueryForList rather than QueryForObject. This is just to show how MyBatis.NET can be used as a data mapper between database and business objects. MyBatis.NET can offer a lot more than what we have seen in this demo. It supports transactions including distributed transactions.

It can be clearly seen that the data access code is very clean and neat , and there is no SQL statements or store procedures sprinkled across the code to access database data. It is purely using objects and its methods.

Further Reading

https://mybatis.github.io/mybatis-3/

 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

John C Rayan
Engineer
United Kingdom United Kingdom
Areas of Expertise:

.NET, MVC, SQL Server, XML , SOA and WEB solutions.

* Microsoft certified in C# Programming.
* Microsoft certified Techology Specialist (ASP.NET)
* IBM certified in XML Technology
* Sun certified Java Programmer
* Sun certified Web Solution Developer

Comments and Discussions

 
QuestionWell done Pin
wshcdr11-Oct-18 1:04
Memberwshcdr11-Oct-18 1:04 
QuestionMyBatis.Net 's compatibility with UWP App Pin
Member 1341054323-Dec-17 15:16
MemberMember 1341054323-Dec-17 15:16 
AnswerRe: MyBatis.Net 's compatibility with UWP App Pin
John C Rayan28-Dec-17 2:34
professionalJohn C Rayan28-Dec-17 2:34 
QuestionMyBastis.NET is dead? Pin
Won Lee31-Oct-17 11:50
MemberWon Lee31-Oct-17 11:50 
AnswerRe: MyBastis.NET is dead? Pin
John C Rayan2-Nov-17 7:32
professionalJohn C Rayan2-Nov-17 7:32 
QuestionGlad to see I'm not the only one! Pin
Member 1306084315-Mar-17 3:49
MemberMember 1306084315-Mar-17 3:49 
AnswerRe: Glad to see I'm not the only one! Pin
John C Rayan15-Mar-17 23:08
professionalJohn C Rayan15-Mar-17 23:08 
QuestionWhat is the provider(name) for SQL Server 2014 Express Pin
Member 1262440010-Jul-16 11:35
MemberMember 1262440010-Jul-16 11:35 
AnswerRe: What is the provider(name) for SQL Server 2014 Express Pin
John C Rayan11-Jul-16 0:44
professionalJohn C Rayan11-Jul-16 0:44 
GeneralRe: What is the provider(name) for SQL Server 2014 Express Pin
Member 1262440011-Jul-16 15:44
MemberMember 1262440011-Jul-16 15:44 
GeneralRe: What is the provider(name) for SQL Server 2014 Express Pin
John C Rayan11-Jul-16 21:54
professionalJohn C Rayan11-Jul-16 21:54 
QuestionWhat is difference between IBatisNet and MyBatis? Pin
Mukund Thakker8-May-16 23:59
professionalMukund Thakker8-May-16 23:59 
AnswerRe: What is difference between IBatisNet and MyBatis? Pin
John C Rayan9-May-16 23:03
professionalJohn C Rayan9-May-16 23:03 
GeneralRe: What is difference between IBatisNet and MyBatis? Pin
Mukund Thakker12-May-16 3:13
professionalMukund Thakker12-May-16 3:13 
GeneralRe: What is difference between IBatisNet and MyBatis? Pin
John C Rayan12-May-16 4:23
professionalJohn C Rayan12-May-16 4:23 
QuestionSqlException Pin
Mukund Thakker6-May-16 2:39
professionalMukund Thakker6-May-16 2:39 
AnswerRe: SqlException Pin
John C Rayan6-May-16 3:45
professionalJohn C Rayan6-May-16 3:45 
GeneralRe: SqlException Pin
Mukund Thakker8-May-16 23:35
professionalMukund Thakker8-May-16 23:35 
GeneralRe: SqlException Pin
John C Rayan9-May-16 23:06
professionalJohn C Rayan9-May-16 23:06 
GeneralThank you Pin
Francis S Michael7-Apr-15 15:32
professionalFrancis S Michael7-Apr-15 15:32 
GeneralRe: Thank you Pin
John C Rayan8-Apr-15 1:05
professionalJohn C Rayan8-Apr-15 1:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.