namespace CodeUin.Dapper
{
public class RepositoryBase<T> : IRepositoryBase<T>
{
public async Task Delete(int Id, string deleteSql)
{
using (IDbConnection conn = DataBaseConfig.GetMySqlConnection())
{
await conn.ExecuteAsync(deleteSql, new { Id });
}
}
public async Task<T> Detail(int Id, string detailSql)
{
using (IDbConnection conn = DataBaseConfig.GetMySqlConnection())
{
return await conn.QueryFirstOrDefaultAsync<T>(detailSql, new { Id });
}
}
public async Task<List<T>> ExecQuerySP(string SPName)
{
using (IDbConnection conn = DataBaseConfig.GetMySqlConnection())
{
return await Task.Run(() => conn.Query<T>(SPName, null, null, true, null, CommandType.StoredProcedure).ToList());
}
}
public async Task<int> Insert(T entity, string insertSql)
{
using (IDbConnection conn = DataBaseConfig.GetMySqlConnection())
{
return await conn.ExecuteAsync(insertSql, entity);
}
}
public async Task<List<T>> Select(string selectSql)
{
using (IDbConnection conn = DataBaseConfig.GetMySqlConnection())
{
return await Task.Run(() => conn.Query<T>(selectSql).ToList());
}
}
public async Task Update(T entity, string updateSql)
{
using (IDbConnection conn = DataBaseConfig.GetMySqlConnection())
{
await conn.ExecuteAsync(updateSql, entity);
}
}
}
}
```
好了,基础类基本已经定义完成。下面将新建一个Users类,并定义几个常用的接口。
#### 创建Users实体类
该类存放在Entities文件夹中,该类继承BaseModel。
```c#
namespace CodeUin.Dapper.Entities
{
/// <summary>
/// 用户表
/// </summary>
public class Users : BaseModel
{
/// <summary>
/// 用户名
/// </summary>
public string UserName { get; set; }
/// <summary>
/// 密码
/// </summary>
public string Password { get; set; }
/// <summary>
/// 盐
/// </summary>
public string Salt { get; set; }
/// <summary>
/// 邮箱
/// </summary>
public string Email { get; set; }
/// <summary>
/// 手机号
/// </summary>
public string Mobile { get; set; }
/// <summary>
/// 性别
/// </summary>
public int Gender { get; set; }
/// <summary>
/// 年龄
/// </summary>
public int Age { get; set; }
/// <summary>
/// 头像
/// </summary>
public string Avatar { get; set; }
/// <summary>
/// 是否删除
/// </summary>
public int IsDelete { get; set; }
}
}
```
#### 创建IUserRepository类
该类存放在IRepository文件夹中,继承IRepositoryBase<Users>,并定义了额外的接口。
```c#
using CodeUin.Dapper.Entities;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace CodeUin.Dapper.IRepository
{
public interface IUserRepository : IRepositoryBase<Users>
{
Task<List<Users>> GetUsers();
Task<int> AddUser(Users entity);
Task DeleteUser(int d);
Task<Users> GetUserDetail(int id);
Task<Users> GetUserDetailByEmail(string email);
}
}
```
#### 创建UserRepository类
该类存放在Repository文件夹中,继承RepositoryBase<Users>, IUserRepository ,是IUserRepository类的具体实现。
```c#
using CodeUin.Dapper.Entities;
using CodeUin.Dapper.IRepository;
using Dapper;
using System.Collections.Generic;
using System.Data;
using System.Threading.Tasks;
namespace CodeUin.Dapper.Repository
{
public class UserRepository : RepositoryBase<Users>, IUserRepository
{
public async Task DeleteUser(int id)
{
string deleteSql = "DELETE FROM [dbo].[Users] WHERE Id=@Id";
await Delete(id, deleteSql);
}