基于IBatisNet(MyBatis.Net)的Repository

说明什么的就不写了,直接上代码,,如果不了解Repository,那就先去百度或者谷歌下……

IRepository接口约束

  using IBatisNet.DataAccess.Interfaces; public interface IRepository<T> : IDaowhere T : class{/// <summary>/// 新增/// </summary>/// <param name="entity"></param>/// <returns></returns>bool Insert(T entity);/// <summary>/// 编辑/// </summary>/// <param name="entity"></param>/// <returns></returns>bool Update(T entity);/// <summary>/// 删除/// </summary>/// <param name="entity"></param>/// <returns></returns>bool Delete(object entity);/// <summary>/// 单个查询/// </summary>/// <param name="entity"></param>/// <returns></returns>T QueryByKey(object entity);/// <summary>/// 查找数据集合/// </summary>/// <param name="searchEntity"></param>/// <param name="totalCount"></param>/// <returns></returns>IList<T> FindAll<T1>(T1 searchEntity, out int totalCount)where T1 : BaseQuery;}

BaseQuery查询实体

[Serializable]public class BaseQuery{public int StartIndex{get{if (this.PageSize <= 0 || this.PageIndex < 0){return 0;}return this.PageSize * this.PageIndex;}}public int EndIndex{get{if (this.PageSize <= 0 || this.PageIndex < 0){return 0;}return this.PageSize * this.PageIndex + this.PageSize;}}public int PageSize { get; set; }/// <summary>/// 页码,从0开始计算/// </summary>public int PageIndex { get; set; }/// <summary>/// 修正页码,如果传入的页码索引大于最大页码数,将页码修正为最后一页/// </summary>/// <param name="totalCount"></param>public void FixPageIndex(int totalCount){if (this.PageIndex < 0 || this.PageSize <= 0 || totalCount <= 0){this.PageIndex = 0;return;}if (this.StartIndex >= totalCount){this.PageIndex = totalCount / this.PageSize;if (totalCount % this.PageSize > 0){this.PageIndex++;}this.PageIndex–;}}}Repository抽象基类,所有定义的抽象属性均为SqlMap中statements节点sql对应的id,需在子类中实际指定 using IBatisNet.DataMapper;using System.Linq.Expressions;public abstract class Repository<T> : IRepository<T>where T : class{protected virtual ISqlMapper Mapper{get { return SqlMapperHelper.GetMapper(); }}/// <summary>/// 新增/// </summary>protected abstract string InsertStatement { get; }/// <summary>/// 编辑/// </summary>protected abstract string UpdateStatement { get; }/// <summary>/// 删除/// </summary>protected abstract string DeleteStatement { get; }/// <summary>/// 单查/// </summary>protected abstract string QueryObjectStatement { get; }/// <summary>/// Count/// </summary>protected abstract string QueryCountStatement { get; }/// <summary>/// 分页查询/// </summary>protected abstract string QueryPagedListStatement { get; }/// <summary>/// Full Query/// </summary>protected abstract string QueryAllStatement { get; }public virtual bool Insert(T entity){return this.Mapper.Insert(this.InsertStatement, entity) != null;}public virtual bool Update(T entity){return this.Mapper.Update(this.UpdateStatement, entity) > 0;}public virtual bool Delete(object entity){return this.Mapper.Delete(this.DeleteStatement, entity) > 0;}public virtual T QueryByKey(object entity){return this.Mapper.QueryForObject<T>(this.QueryObjectStatement, entity);}public virtual IList<T> FindAll<T1>(T1 searchEntity, out int totalCount) where T1 : BaseQuery{return this.FindAll<T, T1>(searchEntity, this.QueryCountStatement, this.QueryPagedListStatement,this.QueryAllStatement, out totalCount);}/// <summary>/// 分页查询(如果searchEntity没有指定或者指定Size,Index不正确,则查询所有数据)/// </summary>/// <typeparam name="T1"></typeparam>/// <typeparam name="T2"></typeparam>/// <param name="searchEntity"></param>/// <param name="queryCountStatement"></param>/// <param name="queryPagedListStatement"></param>/// <param name="queryAllStatement"></param>/// <param name="totalCount"></param>/// <returns></returns>protected IList<T1> FindAll<T1, T2>(T2 searchEntity,string queryCountStatement, string queryPagedListStatement, string queryAllStatement,out int totalCount) where T2 : BaseQuery{IList<T1> list = null;if (searchEntity != null && searchEntity.PageIndex >= 0 && searchEntity.PageSize > 0){//分页totalCount = this.Mapper.QueryForObject<int>(queryCountStatement, searchEntity);searchEntity.FixPageIndex(totalCount);//修正页码if (totalCount > 0){list = this.Mapper.QueryForList<T1>(queryPagedListStatement, searchEntity);}}else{//获取所有数据list = this.Mapper.QueryForList<T1>(queryAllStatement, searchEntity);totalCount = list.Count;}return list;}/// <summary>/// 单数据库事务/// </summary>/// <param name="fun"></param>/// <returns></returns>protected bool Transaction(Func<bool> fun){this.Mapper.BeginTransaction();try{bool result = fun();if (result){this.Mapper.CommitTransaction();}else{this.Mapper.RollBackTransaction();}return result;}catch{this.Mapper.RollBackTransaction();//TODO:logthrow;}finally{}}}SqlMapperHelper帮助类,此处暂时随便写的,如果多数据库,此部分可以与Repository部分联合调整

思念是对昨天悠长的沉淀和对未来美好的向往。

基于IBatisNet(MyBatis.Net)的Repository

相关文章:

你感兴趣的文章:

标签云: