zoukankan      html  css  js  c++  java
  • SqlSugar简单工模式数据访问简单Demo

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

    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整理

  • 相关阅读:
    Oracle通过Rman的"copy datafile"转移数据文件后不要使用sqlplus来重命名文件位置和文件名
    Oracle使用errorstack跟踪客户端的ORA报错
    Oracle OEM 13C表空间报警延迟问题
    CH5 用神经网络解决线性问题
    CH4 简化神经网络模型
    CH3 初识 TensorFlow
    Python 语言和 TensorFlow 框架环境准备
    创建型模式之单例模式与工厂模式(一)
    Node.js Koa框架学习笔记
    国庆七天假 不如来学学Vue-Router
  • 原文地址:https://www.cnblogs.com/tianma3798/p/6094771.html
Copyright © 2011-2022 走看看