0%

SqlSugar CRUD

最基础且最重要的 CRUD

这里只演示一个简单的 CRUD

具体靠自己深入了解

首先创建一个仓储

不了解仓储的小伙伴请看 SqlSugar-仓储官方文档

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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 = "server=.;database=SqlSugarDemo;uid=sa;pwd=123456;"
});
}
}
}

如果是个人开发可以不用写 interface

如果是团队开发建议写上,有利于团队间的合作

无论是个人还是团队,最好还是写上因为以后工作是会用到接口编程

1
2
3
4
5
6
7
8
9
10
11
12
public class UserRepository : Repository<Users>, IUserRepository
{
public IEnumerable<Users> GetAllUser()
{
return base.GetList();
}

public Users GetUserById(int id)
{
return base.GetById(id);
}
}

IOC 注入

在 Startup 文件添加下面代码

1
services.AddScoped<IUserRepository,UserRepository>();

C (create)

1
2
3
4
5
6
7
8
9
10
[HttpPost]
public string AddUser(Users entity)
{
_user.AddUser(entity);
return new
{
Code = 200,
Msg = "添加用户成功!"
}.SerializeObject();
}

R (retrieve)

1
2
3
4
5
6
7
8
9
10
11
12
[HttpGet]
public string GetUser()
{
var tmp = _user.GetAllUser();

return new
{
Code = 200,
Msg = "获取所有用户成功!",
Data = tmp,
}.SerializeObject();
}

U (update)

1
2
3
4
5
6
7
8
9
10
11
[HttpPut]
[Route("{id}")]
public string UpdateUser(Users entity,int id)
{
_user.UpdateUser(entity,id);
return new
{
Code = 200,
Msg = "修改用户成功!"
}.SerializeObject();
}

D (delete)

1
2
3
4
5
6
7
8
9
10
[HttpDelete]
public string DeleteUser(int id)
{
_user.DeleteUser(id);
return new
{
Code = 200,
Msg = "删除用户成功!"
}.SerializeObject();
}

SerializeObject 方法

1
2
3
4
5
6
7
8
9
10
11
public static string SerializeObject(this object obj)
{
var options =
new JsonSerializerOptions
{
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
WriteIndented = true
};
return JsonSerializer.Serialize(obj, options);
}

欢迎关注我的其它发布渠道