Dapper 工具类 电脑版发表于:2022/3/4 17:18 tn>安装Dapper ```bash Install-Package Dapper -Version 2.0.123 ``` tn>DapperHelper类 ```csharp public class DapperHelper { private IDbConnection _connection { get; set; } private IDbTransaction _transaction { get; set; } public DapperHelper(IDbConnection dbConnection) { _connection = dbConnection; } public void Open() { if (_connection.State != ConnectionState.Open) { _connection.Open(); } } public void Close() { if (_connection.State != ConnectionState.Closed) { _connection.Close(); } } public void BeginTransaction() { _transaction = _connection.BeginTransaction(); } public void Commit() { _transaction.Commit(); } public void RollBack() { _transaction.Rollback(); } public async Task<List<T>> QueryAll<T>(string sql,T t) { var query = await _connection.QueryAsync<T>(sql, t); return query.AsList(); } public async Task<T> QueryOne<T>(string sql, T t) { var query = await _connection.QueryFirstOrDefaultAsync<T>(sql, t); return query; } public async Task<List<T>> QueryAll<T>(string sql, object t) { var query = await _connection.QueryAsync<T>(sql, t); return query.AsList(); } public async Task<T> QueryOne<T>(string sql, object t) { var query = await _connection.QueryFirstOrDefaultAsync<T>(sql, t); return query; } public async Task<int> Execute<T>(string sql, T t) { return await _connection.ExecuteAsync(sql, t); } } ```