Dapper是一款轻量级ORM工具。如果你在小的项目中,使用Entity Framework、NHibernate 来处理大数据访问及关系映射,未免有点杀鸡用牛刀。你又觉得ORM省时省力,这时Dapper 将是你不二的选择。
1. 使用Sqlserver创建测试表
2.创建winform应用程序,引用Dapper封装基础应用和框架
3.创建简单页面实现CRUD
CREATE DATABASE test
USE test
GO
CREATE TABLE Test
(
testId INT PRIMARY KEY IDENTITY,
testName VARCHAR(50) NULL
)
INSERT INTO Test VALUES ('CallmeYhz')
INSERT INTO Test VALUES('周公瑾')
public class Test
{
public int testId { get; set; }
public string testName { get; set; }
}
该类实现接口
public interface IDaoBase<T, TKey> where T : class
{
int Count(IPredicate predicate, bool buffered = false);
bool Delete(IPredicate predicate);
bool DeleteByID(TKey key);
bool DeleteByIDList(Expression<Func<T, object>> expression, IEnumerable<TKey> idList);
int Execute(string sql, dynamic param = null);
bool Exists(string sql, dynamic param = null, bool buffered = false);
IEnumerable<TReturn> Get<TFirst, TSecond, TReturn>(string sql, Func<TFirst, TSecond, TReturn> map, dynamic param = null, IDbTransaction transaction = null, bool buffered = true, string splitOn = "Id", int? commandTimeout = null);
IList<T> GetAll();
T GetByID(TKey key);
IEnumerable<T> GetByIDList(Expression<Func<T, object>> expression, IEnumerable<TKey> idList, bool buffered = false);
T GetEntity(IPredicate predicate, bool buffered = false);
T GetEntity(string sql, object param, bool buffered = false);
PagedList<T> GetEntityPageList(SearchBase search);
IList<T> GetList(IPredicate predicate = null, IList<ISort> sort = null, bool buffered = false);
IList<T> GetList(string sql, object param = null, bool buffered = true);
IList<TEntity> GetList<TEntity>(string sql, object param = null, bool buffered = true);
SqlMapper.GridReader GetMultiple(string sql, dynamic param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null);
TScale GetScale<TScale>(string sql, dynamic param = null, bool buffered = false);
dynamic Insert(T entity);
void InsertBatch(IEnumerable<T> entityList, IDbTransaction transaction = null);
bool Update(T entity);
void UpdateBatch(IEnumerable<T> entityList);
}
数据连接工厂
public class ConnectionFactory
{
/// <summary>
/// 数据库连接
/// </summary>
public static string ConnectionString { set; get; }
/// <summary>
/// 数据库类型
/// </summary>
public static DatabaseType DataBaseType { set; get; }
/// <summary>
/// 初始化
/// </summary>
/// <param name="conectionString">连接串</param>
/// <param name="dataBaseType">数据库类型</param>
public static void Init(string conectionString, DatabaseType dataBaseType = DatabaseType.SqlServer)
{
ConnectionFactory.ConnectionString = conectionString;
ConnectionFactory.DataBaseType = dataBaseType;
}
/// <summary>
/// 创建数据库连接
/// </summary>
/// <returns></returns>
public static IDbConnection CreateConnection()
{
IDbConnection connection = null;
switch (ConnectionFactory.DataBaseType)
{
case DatabaseType.SqlServer:
connection = new SqlConnection(ConnectionFactory.ConnectionString);
break;
}
return connection;
}
}
public class TestDao : DaoBase<Test,int>
{
/// <summary>
/// 获取所有数据
/// </summary>
/// <returns></returns>
public IList<Test> GetAllList()
{
string sql = @"
SELECT*FROM Test";
return this.GetList<Test>(sql);
}
/// <summary>
/// 根据名称获取实体
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public Test GetByName(string name)
{
string sql = @"
SELECT*FROM Test WHERE testName=@testName";
return this.GetEntity(sql, new { testName = name });
}
}
构造一个简单Winform界面
配置数据库连接字符串并且初始化
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<connectionStrings>
<!-- 测试 -->
<add name="db" connectionString=" Data Source=.;Initial Catalog=test;Integrated Security=SSPI; " />
</connectionStrings>
</configuration>
//初始化数据库连接
ConnectionFactory.Init(ConfigurationManager.ConnectionStrings["db"].ConnectionString);