1、最简单的使用

public class DatabaseService
{
private static readonly Lazy<SqlSugarClient> _db = new Lazy<SqlSugarClient>(() =>
{
var db = new SqlSugarClient(new ConnectionConfig
{
ConnectionString = ConnectionDbConfig.ConnectionString,
DbType = DbType.SqlServer,
IsAutoCloseConnection = true,
InitKeyType = InitKeyType.Attribute
});
return db;
});
}

2、使用自带的SimpleClient

 public class DatabaseService<T> : SimpleClient<T> where T : class, new()
{
private static readonly Lazy<SqlSugarClient> _db = new Lazy<SqlSugarClient>(() =>
{
var db = new SqlSugarClient(new ConnectionConfig
{
ConnectionString = ConnectionDbConfig.ConnectionString,
DbType = DbType.SqlServer,
IsAutoCloseConnection = true,
InitKeyType = InitKeyType.Attribute
});
return db;
});
}

3、在第一种基础上自定义部分方法

 public class DataBaseProvider<TEntity> where TEntity : class, new()
{
private SqlSugarClient _db; public DataBaseProvider()
{
_db = new SqlSugarClient(new ConnectionConfig()
{ ConnectionString = ConnectionDbConfig.ConnectionString,
DbType = DbType.SqlServer,
IsAutoCloseConnection = true,
InitKeyType = InitKeyType.Attribute });
} public ISugarQueryable<TEntity> Queryable
{
get { return _db.Queryable<TEntity>(); }
} public ISugarQueryable<TEntity> QueryableList()
{
return _db.Queryable<TEntity>();
} public void Insert(TEntity entity)
{
_db.Insertable(entity).ExecuteCommand();
} public void Update(TEntity entity)
{
_db.Updateable(entity).ExecuteCommand();
} public void Delete(TEntity entity)
{
_db.Deleteable(entity).ExecuteCommand();
} public void SaveData(TEntity entity)
{
//_db.Saveable(entity).ExecuteReturnEntity(); 过时
_db.Storageable(entity).ExecuteCommand();
}
public void DeleteById(object id)
{
_db.Deleteable<TEntity>(id).ExecuteCommand();
}
}

4、复杂业务需要自定义泛型接口,(包括但不仅限于输入输出DTO),此时子类方法需要用到AutoMapper或者Maspter

 public interface IBaseRepository<TEntity> where TEntity : class, new()
{
Task<bool> CreateAsync(TEntity entity);
Task<bool> DeleteAsync(int id);
Task<bool> EditAsync(TEntity entity);
Task<TEntity> FindAsync(int id);
Task<TEntity> FindAsync(Expression<Func<TEntity, bool>> func);
/// <summary>
/// 查询全部的数据
/// </summary>
/// <returns></returns>
Task<List<TEntity>> QueryAsync();
/// <summary>
/// 自定义条件查询
/// </summary>
/// <param name="func"></param>
/// <returns></returns>
Task<List<TEntity>> QueryAsync(Expression<Func<TEntity, bool>> func);
/// <summary>
/// 分页查询
/// </summary>
/// <param name="page"></param>
/// <param name="size"></param>
/// <param name="total"></param>
/// <returns></returns>
Task<List<TEntity>> QueryAsync(int page, int size, RefAsync<int> total);
/// <summary>
/// 自定义条件分页查询
/// </summary>
/// <param name="func"></param>
/// <param name="page"></param>
/// <param name="size"></param>
/// <param name="total"></param>
/// <returns></returns>
Task<List<TEntity>> QueryAsync(Expression<Func<TEntity, bool>> func, int page, int size, RefAsync<int> total);
}
public class BaseRepository<TEntity> : SimpleClient<TEntity>, IBaseRepository<TEntity> where TEntity : class, new()
{
public BaseRepository(ISqlSugarClient context = null) : base(context)
{
base.Context = xxx; }
public async Task<bool> CreateAsync(TEntity entity)
{
return await base.InsertAsync(entity);
}
public async Task<bool> DeleteAsync(int id)
{
return await base.DeleteByIdAsync(id);
} public async Task<bool> EditAsync(TEntity entity)
{
return await base.UpdateAsync(entity);
}
//导航查询
public virtual async Task<TEntity> FindAsync(int id)
{
return await base.GetByIdAsync(id);
} public async Task<TEntity> FindAsync(Expression<Func<TEntity, bool>> func)
{
return await base.GetSingleAsync(func);
} public virtual async Task<List<TEntity>> QueryAsync()
{
return await base.GetListAsync();
} public virtual async Task<List<TEntity>> QueryAsync(Expression<Func<TEntity, bool>> func)
{
return await base.GetListAsync(func);
} public virtual async Task<List<TEntity>> QueryAsync(int page, int size, RefAsync<int> total)
{
return await base.Context.Queryable<TEntity>()
.ToPageListAsync(page, size, total);
} public virtual async Task<List<TEntity>> QueryAsync(Expression<Func<TEntity, bool>> func, int page, int size, RefAsync<int> total)
{
return await base.Context.Queryable<TEntity>()
.Where(func)
.ToPageListAsync(page, size, total);
}
}

SqlSugar的几种连接方式的更多相关文章

  1. linux学习之centos(二):虚拟网络三种连接方式和SecureCRT的使用

    ---操作环境--- 虚拟机版本:VMware Workstation_10.0.3 Linux系统版本:CentOS_6.5(64位) 物理机系统版本:win10  一.虚拟网络三种连接方式 当在V ...

  2. Java使用SFTP和FTP两种连接方式实现对服务器的上传下载 【我改】

    []如何区分是需要使用SFTP还是FTP? []我觉得: 1.看是否已知私钥. SFTP 和 FTP 最主要的区别就是 SFTP 有私钥,也就是在创建连接对象时,SFTP 除了用户名和密码外还需要知道 ...

  3. oracle Hash Join及三种连接方式

    在Oracle中,确定连接操作类型是执行计划生成的重要方面.各种连接操作类型代表着不同的连接操作算法,不同的连接操作类型也适应于不同的数据量和数据分布情况. 无论是Nest Loop Join(嵌套循 ...

  4. [转]Apache HTTP Server 与 Tomcat 的三种连接方式介绍

    首先我们先介绍一下为什么要让 Apache 与 Tomcat 之间进行连接.事实上 Tomcat 本身已经提供了 HTTP 服务,该服务默认的端口是 8080,装好 tomcat 后通过 8080 端 ...

  5. Linux基石【第二篇】虚拟网络三种连接方式(转载)

    在虚拟机上安装完Centos系统后,开始配置静态IP,以方便在本宿主机上可以访问虚拟机,在曲折的配置中,了解到虚拟机还有三种连接方式:Bridged,NAT和Host-only,于是,我又一轮新的各种 ...

  6. Java连接Oracle数据库的三种连接方式

    背景: 这两天在学习Oracle数据库,这里就总结下自己上课所学的知识,同时记录下来,方便整理当天所学下的知识,也同时方便日后自己查询. SQL语句的话,这里我就不多讲了,感觉和其他的数据库(MySQ ...

  7. iSCSI存储的3种连接方式

    我们分析了iSCSI存储的系统结构,下面来看iSCSI是如何与服务器.工作站等主机设备来连接的,也就是我们如何建立一个iSCSI网络存储系统. iSCSI设备的主机接口一般默认都是IP接口,可以直接与 ...

  8. Hash Join是Oracle CBO时代经常出现的一种连接方式

    Hash Join是Oracle CBO时代经常出现的一种连接方式,对海量数据处理时经常出现在执行计划里.本篇的上篇(http://space.itpub.net/17203031/viewspace ...

  9. 项目案例模板之jdbc两种连接方式

    项目案例模板之jdbc两种连接方式 第一种连接方式 JDBCUtils.java package jdbc; ​ import org.junit.jupiter.api.Test; ​ import ...

  10. mysql三种连接方式

    sql四种连接方式demo: 表a 表b a.id与b.parent_id有关系 1.内连接:SELECT a.*,b.* from a INNER JOIN b ON a.id=b.parent_i ...

随机推荐

  1. Kafka-常用命令行命令(Kafak3.4.0最新命令)

    第一章 Kafka常用命令 1. Topic(主题) 1.1. 创建Topic bin/kafka-topics.sh --create --bootstrap-server hadoop01:909 ...

  2. wmctf的题解&blindless&exit_hook

    0x00 好久不见 2023.8.25 夜里 wmctf2023也是一个收获很大的比赛.只做了一个blindless,但是体会到了无泄露做出题来的奥妙.踩过的坑(学到的东西)包括但不限于 调试要用do ...

  3. 《Boosting Document-Level Relation Extraction by Mining and Injecting Logical Rules》论文阅读笔记

    代码 原文地址 摘要 文档级关系抽取(DocRE)旨在从文档中抽取出所有实体对的关系.DocRE 面临的一个主要难题是实体对关系之间的复杂依赖性.与大部分隐式地学习强大表示的现有方法不同,最新的 Lo ...

  4. 扯淡的DevOps,我们开发根本不想做运维!

    引言 最初考虑引用" DevOps 已死,平台工程才是未来"作为标题,但这样的表达可能太过于绝对.最终,决定用了"扯淡的"这个词来描述 DevOps,但这并不是 ...

  5. NC53074 Forsaken喜欢独一无二的树

    题目链接 题目 题目描述 ​ 众所周知,最小生成树是指使图中所有节点连通且边权和最小时的边权子集. ​ 不过最小生成树太简单了,我们现在来思考一个稍微复杂一点的问题. ​ 现在给定一个 \(n\) 个 ...

  6. Linux查看系统版本的方法

    记录几种查看当前Linux系统的版本的方法 一.使用命令:cat /proc/version 查看 linux版本号:Linux version 5.4.0-99-generic (buildd@lg ...

  7. sensitive-word v0.13 特性版本发布 支持英文单词全词匹配

    拓展阅读 sensitive-word-admin v1.3.0 发布 如何支持分布式部署? sensitive-word-admin 敏感词控台 v1.2.0 版本开源 sensitive-word ...

  8. 【Android】使用MediaExtractor获取关键帧的时间戳

    1 前言 使用MediaExtractor.MediaMuxer去掉视频文件中的音频数据 中介绍了 MediaExtractor 类的主要方法,本文主要将使用其 advance() 和 seekTo( ...

  9. 配置主机访问virtualbox中redhat7.3虚拟机网络(其他系统配置也类似)

    为什么默认无法访问? virtualbox默认分配一个NAT网络,这个是给虚拟机操作系统访问互联网用的,默认主机通过这个ip段无法直接访问虚拟机.[网卡1] 需要添加一块网卡 在虚拟机关闭状态下,点[ ...

  10. Oracle SQL 注入攻击

    All about Security - SQL Injection 最近做个一个有关ORACLE数据库安全的网上研讨.我们有1300多位参与者,反馈相当丰富. I recently did a we ...