在下刚毕业工作,之前实习有用到Dapper?这几天新项目想用上Dapper,在下比较菜鸟,这块只是个人对Dapper的一种总结。
2,如何使用Dapper?
首先Dapper是支持多种数据库的,当时在学习的时候参考蓝老师的资料https://www.cnblogs.com/lanxiaoke/p/6503022.html。
Dapper支持多数据库的工厂类,设计模式的工厂模式,Skr· Skr~。
public interface IRepositorywhere T : class { void Add(T entity); void AddBatch(IEnumerable 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); IEnumerable GetAll(); IEnumerable GetList(Expression > where = null, Expression > order = null); Tuple > GetPage(Page page, Expression > where = null, Expression > order = null); long Count(Expression > where = null); }
public class IDapperRepository:IRepository 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 (entity); } public void AddBatch(IEnumerable 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 (entity); } public T Get(Guid Id) { return Conn.Get (Id); } public T Get(string Id) { return Conn.Get (Id); } public T Get(int Id) { return Conn.Get (Id); } public T Get(Expression > func) { throw new NotImplementedException(); } public IEnumerable GetAll() { throw new NotImplementedException(); } public IEnumerable GetList(Expression > where = null, Expression > order = null) { throw new NotImplementedException(); } public Tuple > GetPage(Page page, Expression > where = null, Expression > order = null) { throw new NotImplementedException(); } public long Count(Expression > where = null) { throw new NotImplementedException(); } }
由于Dapper是对Sqlmapper的扩展,所以当引入Dapper或者Dapper的扩展类之后,实例化IDbConnection 就可以使用上面的Dapper已经封装好的方法了。
具体Dapper如何使用可看上一篇的小白的参考资料
具体的方法可以查看引用的对象浏览器
以上就是对Dapper的初级使用了。