昨天是寫着寫着發現,時間不早了,已經養成了晚上下班抽時間看看能寫點兒啥的習慣(貌似),今天實在是不想讓昨天沒作完的事情影響,因此又坐下,沉下心(週末了),開始把數據庫這塊兒的補充完整。mysql
昨天已經介紹過大部分的東西,包括方法封裝也是基本的展現了下,實際上應該先介紹這一篇,從怎麼用來引導封裝類庫,可是既然寫都寫出來了就不想再調整了,今天就主要說下怎麼實際使用方法吧,須要查看具體使用的類庫可查看net core Webapi基礎工程搭建(六)——數據庫操做_Part 1。(懶的不能行)web
閒話很少說,Service層搞起,新建兩個文件夾,一個Interfaces,一個Implements,另外順帶新建個Common的文件夾,把上一篇的三個類放進去(純粹是看的,歸類,放不放不影響使用)。sql
上一篇咱們建立了一個StudentEntity這個對象,忘了的朋友能夠去看下。
新建一個IStudentService接口,繼承IBaseService數據庫
public interface IStudentService : IBaseService<StudentEntity> { }
新建一個StudentService實現類,繼承BaseService,實現IStudentServicejson
public class StudentService : BaseService<StudentEntity>, IStudentService { }
好了,完了,回家睡覺吧。api
若是你看到上面的直接走了,對不起,娛樂一下,作開發千萬不敢像烏鴉學蓋房子,沒聽過自行度娘,我也不會講故事。cookie
咱們須要在StartUp這裏將接口與實現關聯,有些博客會介紹自動關聯用於解耦,須要也能夠自行百度。app
補充說明,昨天好像忘記在WebApi工程引入Service與Entity兩個工程了,見諒見諒,特此補充。ide
這裏我在Service項目下Common下新建Depends文件夾,新建一個類ServiceInjection,在這統一放接口與實現的對應關係。
public class ServiceInjection { public static void ConfigureRepository(IServiceCollection services) { services.AddSingleton<IStudentService, StudentService>(); } }
對於依賴注入,這裏簡短穿插幾句,後續有新的感覺會再補充。
方法 | 說明 |
---|---|
Transient | 每一次調用都會建立一個新的實例 |
Scoped | 一個做用域中只實例化一個 |
Singleton | 整個應用程序生命週期之內只建立一個實例 |
而後咱們在StartUp的ConfigureServices最前面加上這句話,這裏在構造函數加上這句,用途就是由AprilConfig來統一接管配置信息。
public Startup(IConfiguration configuration) { ...以前的東西 AprilConfig.InitConfig(configuration); } public void ConfigureServices(IServiceCollection services) { ServiceInjection.ConfigureRepository(services); ...以前的東西 }
固然對應要有接收的方法。
public class AprilConfig { public static IServiceProvider ServiceProvider; public static IConfiguration Configuration; public static void InitConfig(IConfiguration _configuration) { Configuration = _configuration; } }
好了,全部的都寫好後,咱們繼續拿萬惡的Values控制器(多好的開刀對象)實驗。
在這以前,肯定好你的數據庫是哪類,SqlServer請按1,MySql請按0,其餘請自行查看文檔。
爲了方便項目統一管理,咱們要好好利用appsettings,就像咱們當時使用web.config同樣,鏈接串本身根據實際狀況修改。
//新加一個 "DefaultSqlConnectionString": { "MySql": "server=127.0.0.1;userid=root;password=root;database=test;" }
在咱們Util層的AprilConfig,來獲取參數。
private static string _MySqlConnectionString = string.Empty; /// <summary> /// MySql默認鏈接串 /// </summary> public static string MySqlConnectionString { get { if (string.IsNullOrEmpty(_MySqlConnectionString)) { _MySqlConnectionString = Configuration["DefaultSqlConnectionString:MySql"]; } return _MySqlConnectionString; } }
而後咱們來修改BaseService當時留的鏈接串信息。
建立表結構,這裏說明下,不是說非要建立,畢竟SqlSugar有CodeFirst(固然也有DbFirst),須要的朋友可去文檔查看,也比較簡單,在程序啓動的時候來判斷是否有表,或者專門作個接口作初始化操做也能夠,下圖作用法簡介,具體仍是查看文檔吧,畢竟還有備份啊改列名什麼的。
萬事具有,以前Values這個挨千刀的東風,構造函數來獲取IStudentService這個接口。
public class ValuesController : ControllerBase { private readonly IStudentService _service; public ValuesController(IStudentService service) { _service = service; } }
[HttpGet] public ActionResult<IEnumerable<string>> Get() { StudentEntity entity = new StudentEntity(); entity.Name = "小明"; entity.Age = 18; entity.Number = "007"; entity.Sex = 0; entity.Address = "大洛陽"; _service.Insert(entity); return new string[] { "value1", "value2" }; }
SqlFilterEntity擴展方法
//...以前的實體對象 /// <summary> /// 添加查詢條件 /// </summary> /// <param name="filter">條件</param> /// <param name="relation">關係</param> public void Append(string filter, string relation = "and") { if (string.IsNullOrEmpty(filter)) { return; } if (Filter.Length > 0) { Filter += relation; } Filter += filter; } /// <summary> /// 添加查詢參數 /// </summary> /// <param name="key">鍵</param> /// <param name="value">值</param> public void Add(string key, object value) { if (string.IsNullOrEmpty(key) || value == null) { return; } if (Value == null) { Value = new Dictionary<string, object>(); } if (Value.ContainsKey(key)) { Value[key] = value; } else { Value.Add(key, value); } }
修改測試
StudentEntity entity = null; SqlFilterEntity filter = new SqlFilterEntity(); filter.Append($"ID=@ID"); filter.Add("@ID", 1); entity = _service.GetEntity(filter); if (entity != null) { entity.Name = "我被修改了"; _service.Update(entity); }
這裏直接作分頁的測試,拿Values/{id}這個接口作實驗。
[HttpGet("{id}")] public ActionResult<string> Get(int id) { string value = string.Empty; //value = CacheUtil.Get<string>("cachetest"); //value = SessionUtil.GetSession("test"); //value = CookieUtil.GetCookies("apirlcookietest"); int count = 0; List<StudentEntity> lists = _service.GetPageList(id, 10, "", null, "", out count); value = JsonConvert.SerializeObject(lists); return value; }
這裏大體上介紹了SqlSugar的用法已經一些基礎的不能基礎的封裝,實際使用的話,確定須要擴展完善的,可是做爲教程來說,我也不可能把所有的狀況都考慮到,畢竟業務不一樣,沒有萬金油,只有不停的完善更新,業務場景多了,功能就完善了,一步一步來,一口吃不成個胖子(這個胖子不是你認識的那個胖子),不過仍是感慨下這些開源的好類庫,真的是方便了開發人員,爲了解放程序猿的雙手(yy)作出了嗨翻天的貢獻,好了,迴歸正題,下一篇Aop的測試以及小東西。