[FasterTable(TableName = "tb_user")] //自動映射表的別名
public class User
{
[FasterIdentity] //自增長ID
[FasterKey] //設爲主鍵
public int UserId { get; set; }
[FasterColumn(ColumnName ="user_name")] //設置列的別名
[FasterKey] //多個主鍵
public string UserName { get; set; }
public string Password { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
}
var _dbConnection = new SqlConnection("server=.;database=test;user id=sa;password=55969126");
//Code First 根據model生成表
string modelPath = @"E:\WorkSpace\Faster\Model\bin\Debug\netstandard2.0\Model.dll";
_dbConnection.CreateTable(modelPath);
//DB First 根據數據庫生成model(當前項目debug 下面的Model文件夾)
_dbConnection.CreateModels();
IUserRepository repository = new UserService();
//批量新增
List<User> userList = new List<User>();
for (int i = 0; i < 10000; i++)
{
userList.Add(new User
{
UserName = "張強" + (i + 1),
Password = "123456",
Email = "[email protected]",
Phone = "18516328675"
});
}
repository.Add(userList);
//批量修改
userList = new List<User>();
for (int i = 0; i < 100; i++)
{
userList.Add(new User
{
UserId = i + 1,
UserName = "張強" + (i + 1),
Password = "zq",
Email = "[email protected]",
Phone = "zq"
});
}
repository.Update(userList);
//根據主鍵查詢
User user = repository.Get<User>(1, "張強1");
//根據條件查詢
var query = repository.GetList<User>(" where userid>@id", new { id = 10 });
//分頁查詢
var result = repository.GetPageList<User>("userid ", " where userid>@id", new { id = 10 }, 2, 20);
// 滿足條件總頁數
int count = result.Item1;
// 第20條,到40條
IEnumerable<User> list = result.Item2;
// 根據主鍵刪除
int delRow = repository.Remove<User>(1, "張強1");
//用戶自定義接口
repository.Login("zq", "123456");
// 基本增刪改查接口
public interface IRepository
{
IEnumerable<T> GetList<T>(string strWhere = "",object param=null);
T Get<T>(params object[] param);
int Add<T>(IEnumerable<T> modelList);
int Update<T>(IEnumerable<T> modelList);
int Remove<T>(params object[] param);
Tuple<int, IEnumerable<T>> GetPageList<T>(string order, string strWhere = "", object param = null, int pageNum = 1, int PageSize = 10);
}
// 用戶接口
public interface IUserRepository:IRepository
{
// 自定義業務邏輯
bool Login(string username, string password);
}
// 基本服務類實現基本接口
public class BaseService : IRepository
{
private IDbConnection _dbConnection { set; get; }
private const string _connectionStr = "server=.;database=test;user id=sa;password=55969126";
public BaseService()
{
_dbConnection = new SqlConnection(_connectionStr);
}
public int Add<T>(IEnumerable<T> modelList)
{
return _dbConnection.Add<T>(modelList);
}
public T Get<T>(params object[] param)
{
return _dbConnection.Get<T>(param);
}
public IEnumerable<T> GetList<T>(string strWhere = "",object param=null)
{
return _dbConnection.GetList<T>(strWhere,param);
}
public int Update<T>(IEnumerable<T> modelList)
{
return _dbConnection.Update<T>(modelList);
}
public int Remove<T>(params object[] param)
{
return _dbConnection.Remove<T>(param);
}
public Tuple<int, IEnumerable<T>> GetPageList<T>(string order, string strWhere = "", object param = null, int pageNum = 1, int PageSize = 10)
{
return _dbConnection.GetPageList<T>(order, strWhere, param, pageNum, PageSize);
}
}
// 用戶繼承接口和基本實現類
public class UserService : BaseService, IUserRepository
{
public bool Login(string username, string password)
{
return true;
}
}
Dapper ORM 開源框架
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.