WebApi + sqlsugar + storage mode learning records
- 1. Create a template
- 2. Fill in the project name save location
- 3. Checking OpenAPI will automatically configure swagger
- 4. Create storage, service folder, Model layer, add class library
- 5. Install the sqlsugar package
- 6. Paste the code
- 7. Effect example
Preface: Because of the uni-app applet project, so the backend uses this to do it, the applet link: uni-app mall
1. Create a template
2. Fill in the project name save location
3. Checking OpenAPI will automatically configure swagger
Startup
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {<!-- --> if (env. IsDevelopment()) {<!-- --> app.UseDeveloperExceptionPage(); app.UseSwagger(); app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "UniShop.Api v1")); } app. UseRouting(); app. UseAuthorization(); app.UseEndpoints(endpoints => {<!-- --> endpoints. MapControllers(); }); }
4. Create warehouse, service folder, Model layer, add class library
5. Install the sqlsugar package
6. Paste the code
IRepository:
namespace IRepository {<!-- --> public interface IBaseRepository<TEntity> where TEntity : class, new() {<!-- --> bool Create(TEntity entity); bool Delete(int id); bool Edit(TEntity entity); TEntity Find(int id); List<TEntity> QueryList(); } }
IProductRepository:
namespace IRepository {<!-- --> public interface IProductRepository: IBaseRepository<Product> {<!-- --> //Write the private method of IProductRepository here } }
BaseRepository:
namespace Repository {<!-- --> public class BaseRepository<TEntity> : SimpleClient<TEntity>, IBaseRepository<TEntity> where TEntity : class, new() {<!-- --> public BaseRepository(ISqlSugarClient context = null) : base(context)//Note that there must be a default value equal to null {<!-- --> base.Context = DbScoped.SugarScope; //Create a library, create a table and execute it once to comment //base.Context.DbMaintenance.CreateDatabase(); //base.Context.CodeFirst.InitTables( // typeof(Product) // ); } public bool Create(TEntity entity) {<!-- --> return base. Insert(entity); } public bool Delete(int id) {<!-- --> return base. DeleteById(id); } public bool Edit(TEntity entity) {<!-- --> return base.Update(entity); } public TEntity Find(int id) {<!-- --> return base. GetById(id); } public List<TEntity> QueryList() {<!-- --> return base. GetList(); } } }
ProductRepository:
namespace Repository {<!-- --> public class ProductRepository: BaseRepository<Product>, IProductRepository {<!-- --> //Write the private method of ProductRepository here } }
IBaseService:
namespace IService {<!-- --> public interface IBaseService<TEntity> where TEntity : class, new() {<!-- --> bool Create(TEntity entity); bool Delete(int id); bool Edit(TEntity entity); TEntity Find(int id); List<TEntity> QueryList(); } }
IProductService
namespace IService {<!-- --> public interface IProductService: IBaseService<Product> {<!-- --> //Write the private method of IProductService here } }
BaseService:
namespace Service {<!-- --> public class BaseService<TEntity> : IBaseService<TEntity> where TEntity : class, new() {<!-- --> protected IBaseRepository<TEntity> _baseRepository; public bool Create(TEntity entity) {<!-- --> return _baseRepository. Create(entity); } public bool Delete(int id) {<!-- --> return _baseRepository. Delete(id); } public bool Edit(TEntity entity) {<!-- --> return _baseRepository. Edit(entity); } public TEntity Find(int id) {<!-- --> return _baseRepository. Find(id); } public List<TEntity> QueryList() {<!-- --> return _baseRepository. QueryList(); } } }
ProductService:
namespace Service {<!-- --> public class ProductService: BaseService<Product>, IProductService {<!-- --> protected readonly IProductRepository _productRepository; public ProductService(IProductRepository productRepository) {<!-- --> this._productRepository = productRepository; this._baseRepository = productRepository; } } }
ProductController controller
namespace UniShop.Api.Controllers {<!-- --> [Route("api/[controller]")] [ApiController] public class ProductController : ControllerBase {<!-- --> private readonly IProductService _productService; public ProductController(IProductService productService) {<!-- --> this._productService = productService; } [HttpGet("GetProduct")] public ActionResult<ApiResult> GetProduct() {<!-- --> var data = _productService. QueryList(); if(data.Count==0) return ApiResultHelper.Error("Request failed"); return ApiResultHelper.Success(data); } } }
Startup.cs injects the ORM:
namespace UniShop.Api { public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services. AddControllers(); services. AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "UniShop.Api", Version = "v1" }); }); //Inject ORM services.AddSqlSugar(new IocConfig() { ConnectionString = this.Configuration.GetConnectionString("sqlConn"), DbType = IocDbType. SqlServer, IsAutoCloseConnection = true//auto release }); services.AddScoped(); services.AddScoped (); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {<!-- --> if (env. IsDevelopment()) {<!-- --> app.UseDeveloperExceptionPage(); app.UseSwagger(); app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "UniShop.Api v1")); } app. UseRouting(); app. UseAuthorization(); app.UseEndpoints(endpoints => {<!-- --> endpoints. MapControllers(); }); } } }
It should be nothing. SqlSugar ORM specific reference link link: SqlSugar ORM official document