Dapper基础知识二

2022-12-09,

在下刚毕业工作,之前实习有用到Dapper?这几天新项目想用上Dapper,在下比较菜鸟,这块只是个人对Dapper的一种总结。

2,如何使用Dapper?

    首先Dapper是支持多种数据库的,当时在学习的时候参考蓝老师的资料https://www.cnblogs.com/lanxiaoke/p/6503022.html。

     Dapper支持多数据库的工厂类,设计模式的工厂模式,Skr·  Skr~。

 public interface IRepository<T> where T : class
{
void Add(T entity);
void AddBatch(IEnumerable<T> entitys);
void Update(T entity);
void Delete(T entity);
void Delete(string Id);
void Delete(int Id);
void Delete(Guid Id);
T Get(string Id);
T Get(Guid Id);
T Get(int Id);
T Get(T entity);
T Get(Expression<Func<T, bool>> func);
IEnumerable<T> GetAll();
IEnumerable<T> GetList(Expression<Func<T, bool>> where = null, Expression<Func<T, bool>> order = null);
Tuple<int, IEnumerable<T>> GetPage(Page page, Expression<Func<T, bool>> where = null, Expression<Func<T, bool>> order = null);
long Count(Expression<Func<T, bool>> where = null);
}
  public class IDapperRepository<T> :IRepository<T> where T:class
{
protected IDbConnection Conn { get; private set; }
public IDapperRepository()
{
Conn = DbConnectionFactory.CreateDbConnection();
}
public void SetDbConnection(IDbConnection conn)
{
Conn = conn;
} public void Add(T entity)
{ Conn.Insert<T>(entity);
} public void AddBatch(IEnumerable<T> entitys)
{
foreach (T entity in entitys)
{
Add(entity);
}
} public void Update(T entity)
{
Conn.Update(entity);
} public void Delete(T entity)
{
Conn.Delete(entity);
} public void Delete(string Id)
{
var entity = Get(Id);
if (entity == null)
return;
Delete(entity);
} public void Delete(int Id)
{
var entity = Get(Id);
if (entity == null) return; Delete(entity);
} public void Delete(Guid Id)
{
var entity = Get(Id);
if (entity == null) return; Delete(entity);
} public T Get(T entity)
{
return Conn.Get<T>(entity);
} public T Get(Guid Id)
{
return Conn.Get<T>(Id);
} public T Get(string Id)
{
return Conn.Get<T>(Id);
} public T Get(int Id)
{
return Conn.Get<T>(Id);
} public T Get(Expression<Func<T, bool>> func)
{
throw new NotImplementedException();
} public IEnumerable<T> GetAll()
{
throw new NotImplementedException();
} public IEnumerable<T> GetList(Expression<Func<T, bool>> where = null, Expression<Func<T, bool>> order = null)
{
throw new NotImplementedException();
} public Tuple<int, IEnumerable<T>> GetPage(Page page, Expression<Func<T, bool>> where = null, Expression<Func<T, bool>> order = null)
{
throw new NotImplementedException();
} public long Count(Expression<Func<T, bool>> where = null)
{
throw new NotImplementedException();
}
}

   

由于Dapper是对Sqlmapper的扩展,所以当引入Dapper或者Dapper的扩展类之后,实例化IDbConnection 就可以使用上面的Dapper已经封装好的方法了。

具体Dapper如何使用可看上一篇的小白的参考资料

具体的方法可以查看引用的对象浏览器

以上就是对Dapper的初级使用了。

Dapper基础知识

Dapper基础知识二

Dapper基础知识二的相关教程结束。

《Dapper基础知识二.doc》

下载本文的Word格式文档,以方便收藏与打印。