MyBatis.NET.SqlMapper 1.5.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package MyBatis.NET.SqlMapper --version 1.5.0
                    
NuGet\Install-Package MyBatis.NET.SqlMapper -Version 1.5.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="MyBatis.NET.SqlMapper" Version="1.5.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MyBatis.NET.SqlMapper" Version="1.5.0" />
                    
Directory.Packages.props
<PackageReference Include="MyBatis.NET.SqlMapper" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add MyBatis.NET.SqlMapper --version 1.5.0
                    
#r "nuget: MyBatis.NET.SqlMapper, 1.5.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package MyBatis.NET.SqlMapper@1.5.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=MyBatis.NET.SqlMapper&version=1.5.0
                    
Install as a Cake Addin
#tool nuget:?package=MyBatis.NET.SqlMapper&version=1.5.0
                    
Install as a Cake Tool

MyBatis.NET

NuGet Version NuGet Downloads

A lightweight MyBatis port for .NET, providing XML-based SQL mapping, runtime proxy generation, and transaction support.

Features

  • XML Mappers: Define SQL statements in XML files
  • Runtime Proxy: Automatically generate mapper implementations using dynamic proxies
  • Transaction Support: Built-in transaction management
  • Result Mapping: Automatic mapping of query results to .NET objects
  • ADO.NET Integration: Uses Microsoft.Data.SqlClient for database connectivity

Installation

Install via NuGet:

dotnet add package MyBatis.NET.SqlMapper

Or using Package Manager:

Install-Package MyBatis.NET.SqlMapper

Demo Project

Check out the MyBatis.Demo repository for complete working examples including:

  • Basic CRUD operations
  • Custom mapper configurations
  • DDD architecture with multiple libraries
  • Async operations
  • Transaction management

Quick Start

1. Define Your Entity

public class User
{
    public int Id { get; set; }
    public string UserName { get; set; } = "";
    public string Email { get; set; } = "";
}

2. Create Mapper Interface

public interface IUserMapper
{
    List<User> GetAll();
    User GetById(int id);
    int InsertUser(User user);
}

3. Create XML Mapper

Create a file UserMapper.xml in the Mappers directory:

<mapper namespace="IUserMapper">
  <select id="GetAll" resultType="User">
    SELECT Id, UserName, Email FROM Users
  </select>

  <select id="GetById" parameterType="int" resultType="User">
    SELECT Id, UserName, Email FROM Users WHERE Id = @Id
  </select>

  <insert id="InsertUser" parameterType="User">
    INSERT INTO Users (UserName, Email) VALUES (@UserName, @Email)
  </insert>
</mapper>

4. Use in Your Code

using MyBatis.NET.Mapper;
using MyBatis.NET.Core;

// Auto-load all XML mappers from Mappers directory
MapperAutoLoader.AutoLoad();

// Create session with connection string
var connStr = "Server=your-server;Database=your-db;User Id=your-user;Password=your-password;";
using var session = new SqlSession(connStr);

// Get mapper instance
var mapper = session.GetMapper<IUserMapper>();

// Use mapper methods
var users = mapper.GetAll();
var user = mapper.GetById(1);
var rowsAffected = mapper.InsertUser(new User { UserName = "John", Email = "john@example.com" });

Custom Mapper Folders

For projects with multiple libraries (e.g., DDD architecture), you can load mappers from multiple directories:

// Load from multiple directories
MapperAutoLoader.AutoLoad("Mappers", "../Domain/Mappers", "../Infrastructure/Mappers");

// Or load from embedded resources in assemblies (useful for library projects)
MapperAutoLoader.AutoLoadFromAssemblies(typeof(MyClass).Assembly, typeof(OtherClass).Assembly);

To embed XML files as resources in your library:

  1. Add XML files to your project
  2. Set "Build Action" to "Embedded Resource" in file properties
  3. Use AutoLoadFromAssemblies() to load them

Transactions

using var session = new SqlSession(connStr);
session.BeginTransaction();

try
{
    var mapper = session.GetMapper<IUserMapper>();
    mapper.InsertUser(new User { UserName = "Jane", Email = "jane@example.com" });
    session.Commit();
}
catch
{
    session.Rollback();
    throw;
}

Configuration

Connection String

MyBatis.NET uses standard ADO.NET connection strings. Ensure your database supports the operations defined in your mappers.

Mapper Files

  • Place XML mapper files in a Mappers directory
  • Use MapperAutoLoader.AutoLoad() to load all mappers automatically
  • Or load specific files using XmlMapperLoader.LoadFromFile(path)

Supported SQL Operations

  • SELECT (returns List<T> or single T)
  • INSERT, UPDATE, DELETE (returns affected row count)

Requirements

  • .NET 8.0 or later
  • Microsoft.Data.SqlClient (included as dependency)

Contributing

Contributions are welcome! Please feel free to submit issues and pull requests.

License

MIT License - see LICENSE file for details.

Version 1.5.0

  • Cleaned package by removing demo files (User.cs, IUserMapper.cs, Program.cs, and Demo folder) from compilation.

Author

Hammond

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.0.0 181 11/6/2025
1.5.0 176 10/29/2025