.net5 core webapi项目实战之四:数据库操作的编码实现

1 . 用户表t_user的字段如下表:

字段名称 类型 描述
user_id int 用户唯一编号
nickname varchar(10) 用户昵称
email varchar(30) 登录邮箱
password varchar(20) 登陆密码
role_code varchar(10) 角色代码(1:普通用户,2:银卡会员,3:金卡会员,4:钻石会员)
state_code char(1) 状态代码(1:正常,0:锁定,默认是1)
mobile varchar(15) 手机号
age smallint 用户年龄
register_time datetime 注册日期

2 . 为了聚焦在.net core上,所有数据库操作都用伪代码实现,即只做接口,不具体编码实现。

3 . 在models文件夹中新建 user.cs,用于数据的强类型操作,代码如下:

 1     public class User
 2     {
 3         public int UserId{ get; set; }
 4         public string Nickname { get; set; }
 5         public string Email { get; set; }
 6         public string Password { get; set; }
 7         public int RoleCode { get; set; }
 8         public int StateCode { get; set; }
 9         public string Mobile { get; set; }
10         public int Age { get; set; }
11         public DateTime RegisterTime { get; set; } 
12 
13     }

4 . 在应用的根目录上新建DataAccess的文件夹,并新增两个文件,IUserDao.cs和MySqlUserDao.cs,

其中IUserDao.cs是接口,定义对表的操作接口,MySqlUserDao.cs是类文件,继承自IUserDao.cs,负责针对MySQL数据库的操作

IUserDao.cs代码如下:

 1     public interface IUserDao
 2     {
 3         public List<User> GetUserList();
 4 
 5         public void CreateUser(User user);
 6 
 7         public void UpdateUserRole(int userId, int roleCode);
 8 
 9         public void UpdateUserState(int userId, int stateCode);
10 
11         public User GetUser(int userId);
12 
13         public void UpdateUser(User user);
14 
15         public void UpdatePassword(int userId, string oldPassword, string newPassword);
16     }

针对MySQL数据库,我们使用MySQL官方提供的开发工具包来做数据库的操作,在使用前,

需要先用"管理NeGet程序包(N)..."的功能引入MySql.Data开发工具包,如下:

输入mysql后搜索即可看到此开发工具包,直接安装就可以了,如下:

 

在MySqlUserDao.cs中用using MySql.Data.MySqlClient添加引用,然后就可以使用Connection/Command/Reader等对象了,伪代码如下:

  1 using System;
  2 using System.Collections.Generic;
  3 using MySql.Data.MySqlClient;
  4 
  5 namespace WebApiDemo
  6 {
  7     public class MySqlUserDao : IUserDao
  8     {
  9         private MySqlConnection _conn;
 10 
 11         public MySqlUserDao()
 12         {
 13             _conn = new MySqlConnection();
 14         }
 15 
 16         public void CreateUser(User user)
 17         {
 18             try
 19             {
 20                 //do database operation... 
 21                 return;
 22             }
 23             catch { throw; }
 24             finally { _conn.Close(); } 
 25         }
 26 
 27         public User GetUser(int userId)
 28         {
 29             try
 30             {
 31                 User user = new User()
 32                 {
 33                     UserId = 1,
 34                     Nickname = "张三",
 35                     Email = "zhangsan@suho.com",
 36                     Password = "56ftytysvr",
 37                     RoleCode = 1,
 38                     StateCode = 1,
 39                     Mobile = "",
 40                     Age = 28,
 41                     RegisterTime = DateTime.Now
 42                 };
 43                 return user;
 44             }
 45             catch { throw; }
 46             finally { _conn.Close(); }
 47         }
 48 
 49         public List<User> GetUserList()
 50         {
 51             try
 52             {
 53                 User user1 = new User()
 54                 {
 55                     UserId = 1,
 56                     Nickname = "张三",
 57                     Email = "zhangsan@suho.com",
 58                     Password = "56ftytysvr",
 59                     RoleCode = 1,
 60                     StateCode = 1,
 61                     Mobile = "13856785678",
 62                     Age = 28,
 63                     RegisterTime = DateTime.Now
 64                 };
 65                 User user2 = new User()
 66                 {
 67                     UserId = 2,
 68                     Nickname = "李四",
 69                     Email = "lisi@yahoo.com",
 70                     Password = "hjgh786dgsw",
 71                     RoleCode = 2,
 72                     StateCode = 1,
 73                     Mobile = "13866667777",
 74                     Age = 31,
 75                     RegisterTime = DateTime.Now
 76                 };
 77 
 78                 List<User> list = new List<User>();
 79                 list.Add(user1);
 80                 list.Add(user2);
 81                 return list;
 82             }
 83             catch { throw; }
 84             finally { _conn.Close(); }
 85         }
 86 
 87         public void UpdatePassword(int userId, string oldPassword, string newPassword)
 88         {
 89             try
 90             {
 91                 //do database operation... 
 92                 return;
 93             }
 94             catch { throw; }
 95             finally { _conn.Close(); }
 96         }
 97 
 98         public void UpdateUser(User user)
 99         {
100             try
101             {
102                 //do database operation... 
103                 return;
104             }
105             catch { throw; }
106             finally { _conn.Close(); }
107         }
108 
109         public void UpdateUserRole(int userId, int roleCode)
110         {
111             try
112             {
113                 //do database operation... 
114                 return;
115             }
116             catch { throw; }
117             finally { _conn.Close(); }
118         }
119 
120         public void UpdateUserState(int userId, int stateCode)
121         {
122             try
123             {
124                 //do database operation... 
125                 return;
126             }
127             catch { throw; }
128             finally { _conn.Close(); }
129         }
130     }
131 }

至此,数据库的操作就准备好了,在具体的项目中,只需要在try{}语句中添加相关的数据库操作代码就可以了,

当然,如果使用的是SQL Server或Oracle等数据库,需要添加相应的开发工具包。

 

注:在同一个项目中添加的所有类或者接口都尽可能放在同样的命名空间下,这样编码会比较方便。

 

posted @ 2021-01-26 15:22  屏风马  阅读(2473)  评论(1编辑  收藏  举报