.NET Core简易FreeSql分表查、增、删、改操作例子

简易FreeSql分表查、增、删、改操作例子

参考:
1.FreeSql官方文档
2.FreeSql分表分库

1.创建连接

        private readonly IFreeSql _freeSql;

        public FreeSqlService(IConfiguration configuration)
        {
            var _configuration = configuration["ConnectionStrings:Default"];
            _freeSql = new FreeSql.FreeSqlBuilder().UseConnectionString(FreeSql.DataType.MySql, _configuration).Build();
        }

2.创建各方法

说明

我这里分表是根据时间来分
这里参数根据实际情况来创建,我这里是为了方便 用于拼接分表的表名
一般来说是要根据传入的时间来判定所需要查询的分表范围,而不是直接传入分表时间参数。。。

        /// <summary>
        /// 分表查询
        /// </summary>
        /// <param name="yearmonth">表年月</param>
        /// <returns></returns>
        public List<DataForecastModel> Get(List<string> yearmonth)
        {
            ISelect<DataForecastModel> data = _freeSql.Select<DataForecastModel>();
            foreach (var item in yearmonth)
            {
                 data.AsTable((type, x) => $"{x}_{item}");
            }
            return data.ToList();
        }

        /// <summary>
        /// 批量分表插入数据
        /// </summary>
        /// <param name="datas"></param>
        /// <param name="yearmonth"></param>
        /// <returns></returns>
        public bool Insert(List<DataForecastModel> datas,string yearmonth)
        {
            try
            {
                var items = new List<DataForecastModel>();
                foreach (var item in datas)
                {
                    items.Add(new DataForecastModel
                    {
                        Time = item.Time,
                        Weather = item.Weather
                    });
                }
                var res = _freeSql.Insert(items).AsTable((x) => $"{x}_{yearmonth}").ExecuteAffrows();
                return res > 0 ? true : false;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        /// <summary>
        /// 批量分表更新数据
        /// </summary>
        /// <param name="datas"></param>
        /// <param name="yearmonth"></param>
        /// <returns></returns>
        public bool Update(List<DataForecastModel> datas, string yearmonth)
        {
            try
            {
                var res = _freeSql.Update<DataForecastModel>().SetSource(datas).AsTable((x) => $"{x}_{yearmonth}").ExecuteAffrows();
                return res > 0 ? true : false;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        /// <summary>
        /// 批量分表删除数据
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="yearmonth"></param>
        /// <returns></returns>
        public bool Delete(List<string> Id, string yearmonth)
        {
            try
            {
                var res = _freeSql.Delete<DataForecastModel>(Id).AsTable((x) => $"{x}_{yearmonth}").ExecuteAffrows();
                return res > 0 ? true : false;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

3.调用

依赖注入后调用即可

		private readonly IFreeSqlService _freeSqlService;

        public WeatherForecastController(IFreeSqlService freeSqlService)
        {
            _freeSqlService = freeSqlService;
        }

        [HttpPost("DataPost")]
        public List<DataForecastModel> DataPost(List<string> yearmonth)
        {
            var data = _freeSqlService.Get(yearmonth);
            return data;
        }

        [HttpPost("PostInsert")]
        public bool PostInsert(string yearmonth)
        {
            var datas = _freeSqlService.Get(new List<string> { yearmonth });
            var res = _freeSqlService.Insert(datas,yearmonth);
            return res;
        }

        [HttpPost("PostUpdate")]
        public bool PostUpdate(List<DataForecastModel> data, string yearmonth)
        {
            var res = _freeSqlService.Update(data, yearmonth);
            return res;
        }

        [HttpPost("PostDelete")]
        public bool PostDelete(List<string> id, string yearmonth)
        {
            var res = _freeSqlService.Delete(id, yearmonth);
            return res;
        }

4.例子

数据库
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
参数:[“202204”,“202205”]
返回

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值