SqlSugar 是一款簡單易用的ORM ,在國內市場佔有率也比較高,html
在今年10月份官網改版後 提供了完整的服務,讓您的項目沒有後顧之憂git
下載地址 :https://github.com/sunkaixuan/SqlSugargithub
打開VS2019及以上版本 ,而後新建一個Mvc項目,默認MVC是不支持路由的,須要在Startup.cs裏面加入web
app.UseEndpoints(endpoints => { endpoints.MapControllerRoute (name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); endpoints.MapControllerRoute ("areaRoute", "{area:exists}/{controller=Admin}/{action=Index}/{id?}"); });
而後刪除自帶的Pagessql
在根目錄建立Controllers文件夾和Views文件夾 ,建立HomerController.cs和 Index.cshtml數據庫
自帶的IOC使用很是簡單 ,咱們這以Auface ioc爲例子講解app
Autofac ide
Autofac.Extensions.DependencyInjection函數
public class Program { public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .UseServiceProviderFactory(new AutofacServiceProviderFactory())//添加這一行 .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); }); }
public void ConfigureServices(IServiceCollection services) { services.AddRazorPages(); services.AddControllers().AddControllersAsServices();//-----------------添加這一行 } public void ConfigureContainer(ContainerBuilder builder) //----------------添加這個方法 { builder.RegisterType<OrderDal>().InstancePerLifetimeScope(); var webAssemblytype = typeof(Program).Assembly; builder.RegisterAssemblyTypes(webAssemblytype).PropertiesAutowired(); }
public class HomeController : Controller { public OrderDal order { get; set; }//定義他就能注入了 public IActionResult Index() { var list = order.GetList(); return View(); } }
nuget只須要引用一個dll文件,ui
.net 版本選擇 sqlSugar
.net core版本選擇 sqlSugarCore
public class Repository<T> : SimpleClient<T> where T : class, new() { public Repository(ISqlSugarClient context = null) : base(context) { if (context == null) { base.Context = new SqlSugarClient(new ConnectionConfig() { DbType = SqlSugar.DbType.SqlServer, InitKeyType = InitKeyType.Attribute, IsAutoCloseConnection = true, ConnectionString = Config.ConnectionString }); } } /// <summary> /// 擴展方法,自帶方法不能知足的時候能夠添加新方法 /// </summary> /// <returns></returns> public List<T> GetDeleteList() { return Context.Queryable<T>().Where(" isdeleted=1 ").ToList(); } }
就這樣OrderDal就編寫完成了
public class OrderDal: Repository<Order> { //建立OrderItem public Repository<OrderItem> OrderItem => new Repository<OrderItem>(base.Context); public List<OrderItem> GetOrderItems() { return OrderItem.GetList(); //使用OrderItem } public List<Order> GetOrders() { return base.GetList(); //使用自已的倉儲方法 } }
上面簡簡單單幾行就完成了一個IOC+倉儲的 例子
SqlSugar倉儲自帶的方法有不少 ,基本經常使用開發,不能知足的在 Repository中添加倉儲方法
var data1 = base.GetById(1); var data2 = base.GetList(); var data3 = base.GetList(it => it.Id == 1); var data4 = base.GetSingle(it => it.Id == 1); var p = new PageModel() { PageIndex = 1, PageSize = 2 }; var data5 = base.GetPageList(it => it.Name == "xx", p); Console.Write(p.PageCount); var data6 = base.GetPageList(it => it.Name == "xx", p, it => it.Name, OrderByType.Asc); Console.Write(p.PageCount); List<IConditionalModel> conModels = new List<IConditionalModel>(); conModels.Add(new ConditionalModel(){FieldName="id",ConditionalType=ConditionalType.Equal,FieldValue="1"});//id=1 var data7 = base.GetPageList(conModels, p, it => it.Name, OrderByType.Asc); base.AsQueryable().Where(x => x.Id == 1).ToList(); //插入 base.Insert(insertObj); base.InsertRange(InsertObjs); var id = base.InsertReturnIdentity(insertObj); base.AsInsertable(insertObj).ExecuteCommand(); //刪除 base.Delete(insertObj); base.DeleteById(1); base.DeleteById(new int[] { 1, 2 }); base.Delete(it => it.Id == 1); base.AsDeleteable().Where(it => it.Id == 1).ExecuteCommand(); //更新 base.Update(insertObj); base.UpdateRange(InsertObjs); base.Update(it => new Order() { Name = "a", }, it => it.Id == 1); base.AsUpdateable(insertObj).UpdateColumns(it=>new { it.Name }).ExecuteCommand();
上面使用是Auface 實現的MVC,若是咱們要使用自帶的MVC怎麼辦?
一、配置IOC,很是簡單 就幾行代碼
public void ConfigureServices(IServiceCollection services) { services.AddRazorPages(); services.AddScoped<OrderDal>();//添加這2行 services.AddDirectoryBrowser();//添加這2行 }
二、使用IOC ,經過構造函數進行注入
public class HomeController : Controller { public OrderDal order { get; set; } public HomeController(OrderDal order) { this.order = order; } public IActionResult Index() { var list = order.GetList(); return View(); } }