需要引入包:
Dapper
Dapper.Contrib
MySql.Data
接口:
public interface IMySqlDapperContext : IDisposable
{
///
/// MySql连接对象
///
IDbConnection MySqlConnection { get; }
}
///
/// 数据库CRUD操作
///
///
public interface IMySqlRepository where T : class
{
///
/// 连接
///
IDbConnection Connection { get; }
///
/// 上下文
///
IMySqlDapperContext Context { get; }
///
/// 根据主键获取数据
///
///
///
///
///
///
T GetByKey(object key, IDbTransaction transaction = null, int? commandTimeout = null) where T : class;
///
/// 添加数据
///
///
///
///
///
long Add(T entity, IDbTransaction transaction = null, int? commandTimeout = null);
///
///
///
///
///
///
void Update(T entity, IDbTransaction transaction = null, int? commandTimeout = null);
///
/// 删除单条数据
///
///
///
///
void Remove(T entity, IDbTransaction transaction = null, int? commandTimeout = null);
///
/// 根据主键获取数据
///
///
///
///
///
T GetByKey(object key, IDbTransaction transaction = null, int? commandTimeout = null);
///
/// 获取所有数据
///
///
///
///
IEnumerable GetAll(IDbTransaction transaction = null, int? commandTimeout = null);
///
/// 根据条件获取数据列表
///
///
///
///
///
///
IEnumerable GetBy(object where = null, object order = null, IDbTransaction transaction = null, int? commandTimeout = null);
IEnumerable GetBy(object sql = null, object param = null, IDbTransaction transaction = null, int? commandTimeout = null) where T : class;
T QuerySingleOrDefault(string sql = null, object param = null, IDbTransaction transaction = null, int? commandTimeout = null) where T : class;
///
/// 查询数据列表
///
///
///
///
///
IEnumerable Query(string sql, IDbTransaction transaction = null, int? commandTimeout = null);
///
/// 查询数据列表
///
///
///
///