准备工作

一个接口一个类(数据库管理类)

    public interface DBInterface
    {
        IDbConnection GetConnection();
        IEnumerable Query(string sql, object param = null, IDbTransaction transaction = null, bool buffered = true, int? commandTimeout = null, CommandType? commandType = null);
        int Execute(string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null);

    }
    public class DB:DBInterface
    {
        IDbConnection _dbConnection;
        public DB(IDbConnection dbConnection, string connectionString)
        {
            _dbConnection = dbConnection;
            _dbConnection.ConnectionString = connectionString;
        }
        /// 
        /// 连接对象
        /// 
        /// 
        public IDbConnection GetConnection()
        {
            return _dbConnection;
        }
        /// 
        /// 查询方法
        /// 
        /// 映射实体类
        /// sql语句
        /// 参数对象
        /// 事务
        /// 是否缓存结果
        /// command超时时间(秒)
        /// command类型
        /// 
        public IEnumerable Query(string sql, object param = null, IDbTransaction transaction = null, bool buffered = true, int? commandTimeout = null, CommandType? commandType = null)
        {
            return _dbConnection.Query(sql, param, transaction, buffered, commandTimeout, commandType);
        }
        /// 
        /// 执行方法
        /// 
        /// 映射实体类
        /// 参数对象
        /// 事务
        /// command超时时间(秒)
        /// command类型
        /// 
        public int Execute(string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)
        {
            return _dbConnection.Execute(sql, param, transaction, commandTimeout, commandType);
        }

    }

数据库管理接口和类都已经准备好,接下来就是注入进去才能进行使用

    //sqlite连接字符串
    var connectionString = string.Format("Data Source={0}/db.sqlite", System.IO.Directory.GetCurrentDirectory());
    //将数据库连接字符串注入进去
    services.AddSingleton(connectionString);
    //sqlieconnection注放
    services.AddScoped();
    //注放数据库
    services.AddScoped();

使用

用户实体类

    /// 
    /// 用户实体类
    /// 
    public class User
    {
        /// 
        /// ID
        /// 
        public int ID
        { get; set; }
        /// 
        /// 用户名
        /// 
        public string UserName
        { get; set; }

        /// 
        /// 密码
        /// 
        public string Password
        { get; set; }

        public Int64 Phone
        { get; set; }
        /// 
        /// 用户名称
        /// 
        public string Name
        { get; set; }
        /// 
        /// 角色ID
        /// 
        public int RoleID
        { get; set; }
        /// 
        /// 部门编号
        /// 
        public int DepartmentID
        { get; set; }
    }

用户管理接口

    public interface IUserRepository
    {
        /// 
        /// 登录
        /// 
        /// 用户名
        /// 密码
        /// 
        UserRole Login(string userName, string password);
    }

用户管理接口实现

 public class UserRepository : IUserRepository
 {
         /// 
        /// 数据库对象
        /// 
        DBInterface _db;

        public UserRepository(DBInterface db)
        {
            _db= db;

        }
        /// 
        /// 登录
        /// 
        /// 用户名
        /// 密码
        /// 
        public UserRole Login(string userName, string password)
        {
            string sql = "select " +
                "users.*,roles.rolename " +
                "from users join roles on users.roleid=roles.id " +
                "where username=@username and password=@password";
            var userRole = _db.Query(sql, new { username = userName, password = password }).SingleOrDefault();
            if (userRole == null)
            {
                throw new Exception("用户名或密码错误!");
            }
            else
            {
                return userRole;
            }

        }
 }

控制器中使用

        /// 
        /// 登录接口
        /// 
        /// 
        /// 
        /// 
        /// 
        //[HttpPost]
        public IActionResult Login(string userName, string password)
        {
            try
            {
                var userRole = _userRepository.Login(userName, password);
                var BuildToken = Token.BuildToken(
                    new Dictionary {
                     { "userid", userRole.ID },
                     { "username", userRole.Name },
                     { "phone", userRole.Phone }
                 });
                HttpContext.Session.SetString("token", BuildToken);
                return backFun.success(Msg: "登录成功", Data: new { token = BuildToken,userId= userRole.ID,userInfo= userRole });
            }
            catch (Exception ex)
            {
                return backFun.error(Msg: ex.Message);
            }

        }

    写到这里基本上跟都已经搞定,虽然有些不完善,但是asp.net cor和dapper的接口使用流程与方法做了简单的使用和介绍,不完善的地方需要留给您来完善,不完整的地方需要你自己去完整他,不动脑怎么成长,不动手怎么去验证刘成刚是否正确。