准备工作
一个接口一个类(数据库管理类)
public interface DBInterface { IDbConnection GetConnection(); IEnumerable<T> Query<T>(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; } /// <summary> /// 连接对象 /// </summary> /// <returns></returns> public IDbConnection GetConnection() { return _dbConnection; } /// <summary> /// 查询方法 /// </summary> /// <typeparam name="T">映射实体类</typeparam> /// <param name="sql">sql语句</param> /// <param name="param">参数对象</param> /// <param name="transaction">事务</param> /// <param name="buffered">是否缓存结果</param> /// <param name="commandTimeout">command超时时间(秒)</param> /// <param name="commandType">command类型</param> /// <returns></returns> public IEnumerable<T> Query<T>(string sql, object param = null, IDbTransaction transaction = null, bool buffered = true, int? commandTimeout = null, CommandType? commandType = null) { return _dbConnection.Query<T>(sql, param, transaction, buffered, commandTimeout, commandType); } /// <summary> /// 执行方法 /// </summary> /// <param name="sql">映射实体类</param> /// <param name="param">参数对象</param> /// <param name="transaction">事务</param> /// <param name="commandTimeout">command超时时间(秒)</param> /// <param name="commandType">command类型</param> /// <returns></returns> 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<IDbConnection, SqliteConnection>(); //注放数据库 services.AddScoped<INetOADB, NetOADB>();
使用
用户实体类
/// <summary> /// 用户实体类 /// </summary> public class User { /// <summary> /// ID /// </summary> public int ID { get; set; } /// <summary> /// 用户名 /// </summary> public string UserName { get; set; } /// <summary> /// 密码 /// </summary> public string Password { get; set; } public Int64 Phone { get; set; } /// <summary> /// 用户名称 /// </summary> public string Name { get; set; } /// <summary> /// 角色ID /// </summary> public int RoleID { get; set; } /// <summary> /// 部门编号 /// </summary> public int DepartmentID { get; set; } }
用户管理接口
public interface IUserRepository { /// <summary> /// 登录 /// </summary> /// <param name="userName">用户名</param> /// <param name="password">密码</param> /// <returns></returns> UserRole Login(string userName, string password); }
用户管理接口实现
public class UserRepository : IUserRepository { /// <summary> /// 数据库对象 /// </summary> DBInterface _db; public UserRepository(DBInterface db) { _db= db; } /// <summary> /// 登录 /// </summary> /// <param name="userName">用户名</param> /// <param name="password">密码</param> /// <returns></returns> 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<UserRole>(sql, new { username = userName, password = password }).SingleOrDefault(); if (userRole == null) { throw new Exception("用户名或密码错误!"); } else { return userRole; } } }
控制器中使用
/// <summary> /// 登录接口 /// </summary> /// <param name="userName"></param> /// <param name="password"></param> /// <param name="returnUrl"></param> /// <returns></returns> //[HttpPost] public IActionResult Login(string userName, string password) { try { var userRole = _userRepository.Login(userName, password); var BuildToken = Token.BuildToken( new Dictionary<string, object> { { "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的接口使用流程与方法做了简单的使用和介绍,不完善的地方需要留给您来完善,不完整的地方需要你自己去完整他,不动脑怎么成长,不动手怎么去验证刘成刚是否正确。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。