您好,登錄后才能下訂單哦!
準備工作
一個接口一個類(數據庫管理類)
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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。