此文章为.net mvc + Dapper学习实例,文后有实例源码链接。
因不喜欢EF的臃肿和Linq的别扭,以往项目中采用自己弄的轻量级ORM,在思考和借鉴其他ORM框架中,
发现Dapper。Dapper的介绍度娘一堆,这里就不介绍了。
/***表***/
CREATE TABLE Users
(
Id INT PRIMARY KEY IDENTITY(1,1),
UserName NVARCHAR(20),
UserPWD NVARCHAR(32),
NickName NVARCHAR(20),
UserStatus int,
LastLoginOn DATETIME
)
GO
/************************
普通的存储过程
*************************/
CREATE PROC proc_GetUsers
AS
BEGIN
SELECT * FROM Users;
END
GO
/**************************
带参数的存储过程
***************************/
CREATE PROC proc_GetUsers_OutParam
(
@Id INT,
@UserName NVARCHAR(20) output
)
AS
BEGIN
SELECT @UserName=UserName FROM Users WHERE Id=@Id;
END
public class Users
{
///
/// 主键 自增
///
public int? Id { get; set; }
///
/// 用户名
///
public string UserName { get; set; }
///
/// 密码
///
public string UserPWD { get; set; }
///
/// 姓名
///
public string NickName { get; set; }
///
/// 状态
///
public int? UserStatus { get; set; }
///
/// 最后登陆时间
///
public DateTime? LastLoginOn { get; set; }
}
///
/// 用户服务
/// Dapper框架的服务
///
public class Users_Biz
{
const string connstr = "Data Source=.;Initial Catalog=DBTest;User Id=sa;Password=123456;";
//private readonly string connstr =
// "Data Source=.;Initial Catalog=DBTest;User Id=sa;Password=123456;";
///
/// 新增
///
/// 用户对象
public static int Add(Users model)
{
int count = 0;
using (IDbConnection conn=new SqlConnection(connstr))
{
string sql = "insert into Users(UserName,UserPWD,NickName,UserStatus,LastLoginOn) values(@UserName,@UserPWD,@NickName,@UserStatus,@LastLoginOn)";
count =conn.Execute(sql, new { UserName = model.UserName, UserPWD = model.UserPWD, NickName = model.NickName, model.UserStatus, model.LastLoginOn });
}
return count;
}
///
/// 获取列表
///
///
public static List GetList()
{
List list = null;
using (IDbConnection conn = new SqlConnection(connstr))
{
string sql = "select * from Users";
list = conn.Query(sql).ToList();
}
return list;
}
///
/// 通过Id查找
///
///
///
public static Users GetModel(int id)
{
Users model = null;
using (IDbConnection conn = new SqlConnection(connstr))
{
string sql = "select * from Users where Id=@Id";
model = conn.QueryFirstOrDefault(sql, new { Id = id });
}
return model;
}
///
/// 查找最后一个
///
///
public static Users GetLastModel()
{
Users model = null;
using (IDbConnection conn = new SqlConnection(connstr))
{
string sql = "select * from Users order by Id desc";
model = conn.QueryFirstOrDefault(sql);
}
return model;
}
///
/// 查询总行数
///
///
public static int GetCount()
{
int count = 0;
using (IDbConnection conn = new SqlConnection(connstr))
{
string sql = "select count(1) from Users";
count = (int)conn.ExecuteScalar(sql);
}
return count;
}
///
/// 查询List
/// 调用普通存储过程
///
///
public static List GetList_Proc()
{
List list = null;
using (IDbConnection conn = new SqlConnection(connstr))
{
list = conn.Query("proc_GetUsers", null, null, true,null, CommandType.StoredProcedure).ToList();
}
return list;
}
///
/// 通过Id查询UserName
/// 带返回参数的存储过程
///
///
///
public static string GetUserName_Proc(int id)
{
int count = 0;
string userName = string.Empty ;
using (IDbConnection conn = new SqlConnection(connstr))
{
//设置参数集合
DynamicParameters dp = new DynamicParameters();
dp.Add("@Id", id);
dp.Add("@UserName", userName, DbType.String, ParameterDirection.Output);
//执行出差过程
count = conn.Execute("proc_GetUsers_OutParam", dp, null, null, CommandType.StoredProcedure);
//获取参数
userName = dp.Get("@UserName");
}
return userName;
}
}
public ActionResult Index()
{
//1 新增
Users model = new Users()
{
UserName = "Seven",
UserPWD = "13333",
NickName = "赛文",
UserStatus = 1,
LastLoginOn = DateTime.Now
};
int addRes = Users_Biz.Add(model);
//2 查询列表
List list= Users_Biz.GetList();
//3 通过Id查询
model = Users_Biz.GetModel(1);
//4 新增
model.UserName = "Eleven";
model.UserPWD = "88886666";
model.NickName = "伊莱文";
addRes = Users_Biz.Add(model);
//5 查询最后一个
model = Users_Biz.GetLastModel();
//6 查找总个数
int count = Users_Biz.GetCount();
//7 调用普通存储过程
list = Users_Biz.GetList_Proc();
//8 调用带返回参数的存储过程
string userName = Users_Biz.GetUserName_Proc(1);
string json= JsonConvert.SerializeObject(new {
新增1=addRes,
查询列表=list,
通过Id查询=model,
查找总数=count,
调用带返回参数的存储过程=userName
});
return Content(json);
}
至此,Dapper基本的操作已经做完,看上去还是比较简单的。
但是我们会发现一个,这个是需要写SQL语句的,这种感觉很不爽。
想要通过反射的方式来进行SQL的自动拼接,便可以实现不写SQL就能完成实体操作了。
在Nuget中,***Dapper.SimpleCRUD***便是这样的一个类库。
public class Users_BizEx
{
const string connstr = "Data Source=.;Initial Catalog=DBTest;User Id=sa;Password=123456;";
//private readonly string connstr =
// "Data Source=.;Initial Catalog=DBTest;User Id=sa;Password=123456;";
///
/// 新增
///
/// 用户对象
public static int Add(Users model)
{
int? count = 0;
using (IDbConnection conn = new SqlConnection(connstr))
{
count = conn.Insert(model);
}
return count.Value;
}
///
/// 获取列表
///
///
public static List GetList()
{
List list = null;
using (IDbConnection conn = new SqlConnection(connstr))
{
list = conn.GetList().ToList();
}
return list;
}
///
/// 通过Id查找
///
///
///
public static Users GetModel(int id)
{
Users model = null;
using (IDbConnection conn = new SqlConnection(connstr))
{
model = conn.Get(id);
}
return model;
}
///
/// 查找最后一个(分页查询)
///
///
public static Users GetLastModel()
{
Users model = null;
using (IDbConnection conn = new SqlConnection(connstr))
{
model = conn.GetListPaged(1, 1, string.Empty, "Id desc").ToList().FirstOrDefault();
}
return model;
}
///
/// 查询总行数
///
///
public static int GetCount()
{
int count = 0;
using (IDbConnection conn = new SqlConnection(connstr))
{
count = conn.RecordCount(string.Empty);
}
return count;
}
///
/// 查询List
/// 调用普通存储过程
///
///
public static List GetList_Proc()
{
List list = null;
using (IDbConnection conn = new SqlConnection(connstr))
{
list = conn.Query("proc_GetUsers", null, null, true, null, CommandType.StoredProcedure).ToList();
}
return list;
}
///
/// 通过Id查询UserName
/// 带返回参数的存储过程
///
///
///
public static string GetUserName_Proc(int id)
{
int count = 0;
string userName = string.Empty;
using (IDbConnection conn = new SqlConnection(connstr))
{
//设置参数集合
DynamicParameters dp = new DynamicParameters();
dp.Add("@Id", id);
dp.Add("@UserName", userName, DbType.String, ParameterDirection.Output);
//执行出差过程
count = conn.Execute("proc_GetUsers_OutParam", dp, null, null, CommandType.StoredProcedure);
//获取参数
userName = dp.Get("@UserName");
}
return userName;
}
}
Dapper这个轻量级的ORM还是不错的,如果对这个类库的源码感兴趣,可以去git一下。
百度网盘
链接:https://pan.baidu.com/s/1KIqZHjLYtU1oiIUgWysyjw
提取码:8tjj