首页 > 代码库 > SqlSugar简单工模式数据访问简单Demo

SqlSugar简单工模式数据访问简单Demo

源代码地址:http://git.oschina.net/tiama3798/BootstrapBack_Demo

1.Model层

技术分享

2.抽象层实例:

基础接口

技术分享
    /// <summary>
    /// 基础接口
    /// </summary>
    public interface IBaseAbstract<T> where T : class
    {
        #region 基础访问
        /// <summary>
        /// 获取表达式树
        /// </summary>
        /// <returns></returns>
        Queryable<T> GetQueryable();
        /// <summary>
        /// 获取对象
        /// </summary>
        /// <param name="ID">主键ID</param>
        /// <returns></returns>
        T GetModel(object ID);
        /// <summary>
        /// 添加对象
        /// </summary>
        /// <param name="model">对象</param>
        /// <returns></returns>
        object Add(T model);
        /// <summary>
        /// 修改对象
        /// </summary>
        /// <param name="model">对象</param>
        /// <returns></returns>
        bool Update(T model);
        /// <summary>
        /// 删除对象
        /// </summary>
        /// <param name="model">对象</param>
        /// <returns></returns>
        bool Delete(T model);
        /// <summary>
        /// 删除对象---主键删除的公共方法,只支持int类型
        /// </summary>
        /// <param name="ID">主键ID</param>
        /// <returns></returns>
        bool Delete(int ID);
        #endregion
    }
View Code
技术分享
    /// <summary>
    /// 学生接口定义
    /// </summary>
    public interface IStudent : IBaseAbstract<student>
    {
        #region 获取分页
        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <param name="info">分页信息</param>
        /// <returns></returns>
        List<student> GetPage(PageInfo info,string search);

        #endregion

        #region 获取数量
        /// <summary>
        /// 获取总数
        /// </summary>
        /// <returns></returns>
        int GetCount();
        #endregion
    }
View Code

3.实现成

基础类:

技术分享
    internal class BaseOperate<T> where T : class, new()
    {
        protected SqlSugarClient db = null;
        public BaseOperate()
        {
            db = SugarContext.GetInstance();
        }

        #region 基础访问
        /// <summary>
        /// 获取表达式树
        /// </summary>
        /// <returns></returns>
        public Queryable<T> GetQueryable()
        {
            return db.Queryable<T>();
        }
        /// <summary>
        /// 获取对象
        /// </summary>
        /// <param name="ID">主键ID</param>
        /// <returns></returns>
        public T GetModel(object ID)
        {
            return GetQueryable().InSingle<T>(ID);
        }
        /// <summary>
        /// 添加对象
        /// </summary>
        /// <param name="model">对象</param>
        /// <returns></returns>
        public object Add(T model)
        {
            try
            {
                return db.Insert<T>(model);
            }
            catch (Exception ex)
            {
                throw new Exception("添加对象失败,当前类型:"+(typeof(T).Name)+ex.Message,ex);
            }
        }
        /// <summary>
        /// 修改对象
        /// </summary>
        /// <param name="model">对象</param>
        /// <returns></returns>
        public bool Update(T model)
        {
            try
            {
                return db.Update<T>(model);
            }
            catch (Exception ex)
            {
                throw new Exception("修改对象失败,当前类型:" + (typeof(T).Name), ex);
            }
        }
        /// <summary>
        /// 删除对象
        /// </summary>
        /// <param name="model">对象</param>
        /// <returns></returns>
        public bool Delete(T model)
        {
            try
            {
                return db.Delete<T>(model);
            }
            catch (Exception ex)
            {
                throw new Exception("删除对象失败,当前类型:" + (typeof(T).Name), ex);
            }
        }
        /// <summary>
        /// 删除对象,默认只支持int类型
        /// </summary>
        /// <param name="ID">主键ID</param>
        /// <returns></returns>
        public bool Delete(int ID)
        {
            try
            {
                return db.Delete<T, int>(ID);
            }
            catch (Exception ex)
            {
                throw new Exception("删除对象失败,当前类型:" + (typeof(T).Name), ex);
            }
        }
        #endregion
    }
View Code

实现类:

技术分享
    /// <summary>
    /// 学生数据访问操作
    /// </summary>
    internal class StudentOperate : BaseOperate<student>, IStudent
    {
        #region 获取分页
        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <param name="info">分页信息</param>
        /// <returns></returns>
        public List<student> GetPage(PageInfo info, string search)
        {
            //排序
            var query = GetQueryable();
            if (string.IsNullOrEmpty(search) == false)
            {
                query = query.Where(q => q.sno.Contains(search) || q.sname.Contains(search));
            }
            query = query.OrderBy(q => q.sbirthday);
            info.RecordCount = query.Count();
            //分页
            return query.ToPageList(info.PageIndex, info.PageSize);
        }
        #endregion

        #region 获取数量
        /// <summary>
        /// 获取总数
        /// </summary>
        /// <returns></returns>
        public int GetCount()
        {
            return GetQueryable().Count();
        }
        #endregion
    }
View Code

4.工厂类:

技术分享
    /// <summary>
    /// 获取数据操作接口
    /// </summary>
    public class SugarFactory
    {
        public static IStudent GetStudent()
        {
            return new StudentOperate();
        }

    }
View Code

 

 

 

更多参考:

SqlSugar-事务操作

SqlSugar常用增删改操作

SqlSugar常用查询实例-拉姆达表达式

SqlSugar框架T4模板的使用

.Net开源SqlServer ORM框架SqlSugar整理

 

SqlSugar简单工模式数据访问简单Demo