http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 15339|回复: 3

[讨论] 【苏飞分布式框架】常用方法详解 一个极简单的BLL层使用案例

[复制链接]
发表于 2019-2-16 19:13:29 | 显示全部楼层 |阅读模式
新版分布式框架中  一个极简单的DAL层

我们在使用三层或多层时,都必须要有一个层必须是DAL,在新的分布式框架中

我将直接使用一个封装极简单的DAL层方便大家使用

比如你要对一个表进行,增删改查等操作,或者更复杂的数据库操作,只需要这样写
[C#] 纯文本查看 复制代码
using jjoobb_agentoaModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace jjoobb_agentoaBLL
{
    /// <summary>
    /// 回访跟踪记录 操作类
    /// </summary>
    public class Crm_TrackBLL : BaseDAL<Crm_Track>
    {
        /// <summary>
        /// 数据库链接字符串
        /// </summary>
        protected override string ConnName
        {
            get { return DALConfig.JJoobbAgentOa; }
        }
    }
}



这里就只需要继承一下BaseDAL
就行了,
第二步是配置一个链接字符串,这个与之前的框架相同,主要是为了将每个表分布到不同的服务器上使用
拥有的方法如下
[C#] 纯文本查看 复制代码
#region 程序集 SufeiDAL, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// D:\JJoobb2019\sufeiDAL\SufeiDAL\bin\Debug\SufeiDAL.dll
#endregion

using System.Collections.Generic;
using System.Data;

namespace SufeiDAL.BaseApi
{
    //
    // 摘要:
    //     dal数据库基类
    //
    // 类型参数:
    //   T:
    public abstract class BaseDAL<T>
    {
        protected BaseDAL();

        //
        // 摘要:
        //     配置要链接数据库的字符串
        protected abstract string ConnName { get; }

        //
        // 摘要:
        //     根据sql语句查询条数
        //
        // 参数:
        //   sql:
        //     sql
        //
        //   parameters:
        //     查询参数
        public long Count(string sql, IDictionary<string, object> parameters);
        //
        // 摘要:
        //     根据sql语句查询条数
        //
        // 参数:
        //   sql:
        //     sql
        public long Count(string sql);
        //
        // 摘要:
        //     查询总条数
        public long Count();
        //
        // 摘要:
        //     根据sql语句删除数据
        //
        // 参数:
        //   sql:
        //     sql语句
        //
        //   parameters:
        //     参数字典
        public int Delete(string sql, IDictionary<string, object> parameters = null);
        //
        // 摘要:
        //     执行命令,返回影响行数
        //
        // 参数:
        //   commandType:
        //     命令类型
        //
        //   commandText:
        //     命令文本
        //
        //   parameters:
        //     参数字典
        public int ExecuteNonQuery(CommandType commandType, string commandText, IDictionary<string, object> parameters);
        //
        // 摘要:
        //     执行命令,返回首行首列
        //
        // 参数:
        //   commandType:
        //     命令类型
        //
        //   commandText:
        //     命令文本
        //
        //   parameters:
        //     参数字典
        public object ExecuteScalar(CommandType commandType, string commandText, IDictionary<string, object> parameters);
        //
        // 摘要:
        //     查询所有数据
        public List<T> FindList();
        //
        // 摘要:
        //     根据SQl语句执行存储过程查询数据
        //
        // 参数:
        //   sql:
        //     sql语句
        //
        //   parameters:
        //     参数列表
        //
        //   commandType:
        //     指定执行SQl语句还是存储过程
        public List<T> FindList(string sql, IDictionary<string, object> parameters, CommandType commandType = CommandType.Text);
        //
        // 摘要:
        //     根据sql语句查询数据
        //
        // 参数:
        //   sql:
        //     sql语句
        //
        //   top:
        //     top 默认为全部
        public List<T> FindList(string sql, int top = 0);
        //
        // 摘要:
        //     根据sql语句查询一条数
        //
        // 参数:
        //   sql:
        //     sql语句
        public T FindListOne(string sql);
        //
        // 摘要:
        //     查询分页数据,返回数据列表
        //
        // 参数:
        //   factor:
        //     分页因子,TableName可选
        //
        //   totalCount:
        //     输出参数,总条数
        public List<T> FindListPage(OrmLitePageFactor factor, out long totalCount);
        //
        // 摘要:
        //     根据指定字段查询数据
        //
        // 参数:
        //   conditions:
        //     字段字典
        //
        //   orderBy:
        //     排序
        //
        //   commandType:
        //     指定执行SQl语句还是存储过程
        public List<T> FindListWhere(IDictionary<string, object> conditions, string orderBy = "", CommandType commandType = CommandType.Text);
        //
        // 摘要:
        //     插入实体,返回影响行数或自增列
        //
        // 参数:
        //   obj:
        //
        //   selectIdentity:
        //     是否返回自增列
        public long insert(T obj, bool selectIdentity = false);
        //
        // 摘要:
        //     插入实体,返回影响行数或自增列
        //
        // 参数:
        //   table:
        //     表名
        //
        //   fields:
        //     字段字典
        //
        //   selectIdentity:
        //     是否返回自增列
        public long insert(string table, IDictionary<string, object> fields, bool selectIdentity = false);
        //
        // 摘要:
        //     执行命令,返回实体列表
        //
        // 参数:
        //   commandType:
        //     命令类型
        //
        //   commandText:
        //     命令文本
        //
        //   ps:
        //     参数列表
        public List<T> SelectOriginal(CommandType commandType, string commandText, params IDbDataParameter[] ps);
        //
        // 摘要:
        //     根据主键修改字段
        //
        // 参数:
        //   obj:
        //
        //   updateFields:
        //     要修改的字段
        public int Update(T obj, params string[] updateFields);
        //
        // 摘要:
        //     根据条件修改数据
        //
        // 参数:
        //   updateFields:
        //     被修改的字段字典
        //
        //   conditions:
        //     条件语句
        //
        //   parameters:
        //     参数字段
        public int Update(IDictionary<string, object> updateFields, string conditions, IDictionary<string, object> parameters);
    }
}


使用方法如下
[C#] 纯文本查看 复制代码
  Hr_EmployeeBLL hr_el = new Hr_EmployeeBLL();

            //------------------------------------------------------查询------------------------------------------------------------------------
            //查询所有
            List<Hr_Employee> list = hr_el.FindList();

            //根据条件查询
            list = hr_el.FindList("UserID=232");

            //根据条件查询  支持直接自己写SQl语句 
            list = hr_el.FindList("select top 5 * from Hr_Employee where UserID<>@UserID", DictBuilder.Assign("UserID", 109));

            //根据条件查询 参数必须出现在语句中
            list = hr_el.FindList("UserID=@UserID and Password=@Password", DictBuilder.Assign("UserID", 233).Assign("Password", 12345));

            //根据条件查询 自动组织参数
            list = hr_el.FindListWhere(DictBuilder.Assign("Password", "12345").IN("UserID", new int[] { 233, 1180, 326, 325 }));

            long totalcount = 0;
            //根据条件查询-分页
            list = hr_el.FindListPage(
                new OrmLitePageFactor()
                {
                    Conditions = "UserID<>@UserID",
                    OrderBy = "UserID desc",
                    PageIndex = 2,
                    PageSize = 20,
                    Params = DictBuilder.Assign("UserID", 109)
                },
                out totalcount);

            //------------------------------------------------------写入------------------------------------------------------------------------
            Hr_Employee obj = new Hr_Employee()
            {
                Birthday = string.Empty,
                UserName = "sufei"
            };
            //long userid = hr_el.Insert(obj, true);
            //------------------------------------------------------修改------------------------------------------------------------------------
            int result = 0;

            obj = new Hr_Employee()
            {
                Birthday = string.Empty,
                UserName = "sufei"
            };
             //result = hr_el.Update(obj, new string[] { "Birthday", "UserName", "CheckinDate" });

            //条件修改
            result = hr_el.Update(
               DictBuilder.Assign("UserName", "sufei"), "UserName=@UserName1",
               DictBuilder.Assign("UserName1", "sufei"));
            //------------------------------------------------------删除------------------------------------------------------------------------

            //result = hr_el.Delete("UserName=@UserName", DictBuilder.Assign("UserName", "sufei"));

            //------------------------------------------------------其他------------------------------------------------------------------------
            long countall = hr_el.Count();//总行
            long count = hr_el.Count("UserID<>@UserID", DictBuilder.Assign("UserID", 1516));//sql查询
            count = hr_el.Count("UserID<>1516");//单SQl查询


好了,就是这个简单,对于敏捷开发应该有很大有帮助,
很快会更新,,,,


1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2019-2-19 10:52:08 | 显示全部楼层
强烈支持楼主ing……
发表于 2019-4-9 11:28:36 | 显示全部楼层
强烈支持楼主ing……
发表于 2019-9-6 20:09:43 | 显示全部楼层
更新了吗
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-4-25 16:46

© 2014-2021

快速回复 返回顶部 返回列表