We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
IBaseRepository接口:
//使用其他Model的方法 Task<List<T>> Query<T>(); Task<List<T>> Query<T>(Expression<Func<T, bool>> whereExpression); Task<T> QueryById<T>(object objId); Task<int> Add<T>(T model) where T : class, new(); Task<int> Add<T>(List<T> listEntity) where T : class, new(); Task<bool> Update<T>(T model) where T : class, new(); Task<int> Update<T>(List<T> listEntity) where T : class, new();
BaseRepository类:
private SqlSugarClient _dbBase_Other; private ISqlSugarClient dbOther<T>() { if (_dbBase_Other == null) _dbBase_Other = _dbBase; /* 如果要开启多库支持, * 1、在appsettings.json 中开启MutiDBEnabled节点为true,必填 * 2、设置一个主连接的数据库ID,节点MainDB,对应的连接字符串的Enabled也必须true,必填 */ if (Appsettings.app(new string[] { "MutiDBEnabled" }).ObjToBool()) { if (typeof(T) .GetTypeInfo() .GetCustomAttributes(typeof(SugarTable), true) .FirstOrDefault((x => x.GetType() == typeof(SugarTable))) is SugarTable sugarTable && !string.IsNullOrEmpty(sugarTable.TableDescription)) { _dbBase_Other.ChangeDatabase(sugarTable.TableDescription.ToLower()); } else { _dbBase_Other.ChangeDatabase(MainDb.CurrentDbConnId.ToLower()); } } return _dbBase_Other; } //使用其他Model的方法 /// <summary> /// 查询 /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> public async Task<List<T>> Query<T>() { return await dbOther<T>().Queryable<T>().ToListAsync(); } public async Task<List<T>> Query<T>(Expression<Func<T, bool>> whereExpression) { return await dbOther<T>().Queryable<T>() .WhereIF(whereExpression != null, whereExpression) .ToListAsync(); } public async Task<T> QueryById<T>(object objId) { return await dbOther<T>().Queryable<T>().In(objId).SingleAsync(); } /// <summary> /// 写入实体数据 /// </summary> /// <param name="entity">实体类</param> /// <returns></returns> public async Task<int> Add<T>(T entity) where T : class, new() { var insert = dbOther<T>().Insertable<T>(entity); return await insert.ExecuteReturnIdentityAsync(); } /// <summary> /// 写入实体数据 /// </summary> /// <param name="entity">实体类</param> /// <param name="insertColumns">指定只插入列</param> /// <returns>返回自增量列</returns> public async Task<int> Add<T>(List<T> listEntity) where T : class, new() { if (listEntity != null && listEntity.Count > 0) return await dbOther<T>().Insertable(listEntity.ToArray()).ExecuteCommandAsync(); else return 0; } public async Task<bool> Update<T>(T model) where T : class, new() { return await dbOther<T>().Updateable(model).ExecuteCommandHasChangeAsync(); } /// <summary> /// 更新实体数据 /// </summary> /// <param name="listEntity">实体集合</param> /// <returns></returns> public async Task<int> Update<T>(List<T> listEntity) where T : class, new() { if (listEntity != null && listEntity.Count > 0) return await dbOther<T>().Updateable(listEntity.ToArray()).ExecuteCommandAsync(); else return 0; } /// <summary> /// 查询数据 /// </summary> /// <param name="entity">实体类</param> /// <returns></returns> public ISugarQueryable<T> SqlQueryableByDB<T>(string sql) where T : class, new() { return dbOther<T>().SqlQueryable<T>(sql); }
IBaseServices接口:
Task<List<T>> Query<T>(); Task<List<T>> Query<T>(Expression<Func<T, bool>> whereExpression); Task<T> QueryById<T>(object objId); Task<int> Add<T>(T model) where T : class, new(); Task<int> Add<T>(List<T> listEntity) where T : class, new(); Task<bool> Update<T>(T model) where T : class, new(); Task<int> Update<T>(List<T> listEntity) where T : class, new();
BaseServices类:
public async Task<List<T>> Query<T>() { return await BaseDal.Query<T>(); } public async Task<List<T>> Query<T>(Expression<Func<T, bool>> whereExpression) { return await BaseDal.Query<T>(whereExpression); } public async Task<T> QueryById<T>(object objId) { return await BaseDal.QueryById<T>(objId); } public async Task<int> Add<T>(T model) where T : class, new() { return await BaseDal.Add<T>(model); } public async Task<int> Add<T>(List<T> listEntity) where T : class, new() { return await BaseDal.Add<T>(listEntity); } public async Task<bool> Update<T>(T model) where T : class, new() { return await BaseDal.Update<T>(model); } public async Task<int> Update<T>(List<T> listEntity) where T : class, new() { return await BaseDal.Update<T>(listEntity); }
使用: Model中加分库的连接 可以使用其他Model的Services查询。
主要是懒的写多个Service,我就这样处理了。
The text was updated successfully, but these errors were encountered:
No branches or pull requests
IBaseRepository接口:
BaseRepository类:
IBaseServices接口:
BaseServices类:
使用:
Model中加分库的连接
可以使用其他Model的Services查询。
主要是懒的写多个Service,我就这样处理了。
The text was updated successfully, but these errors were encountered: