标签:net keep DApp https multi list() cti 分享 com
从字面理解,O是Object,对象;R是Relation,关系;M是Mapping,映射。所以,用一句话概括就是:ORM是一种对象关系映射的技术。
Dapper 是.NET下的一种ORM框架。
使用NuGet安装
打开visual studio的项目,依次点击工具
,NuGet包管理器
,管理解决方案的NuGet程序包
;再点击浏览
,搜索dapper
,点击搜索结果中的Dapper
,勾选项目
,选择安装
;
在解决方案管理器中点击项目
,查看引用
,如果有Dapper
,说明安装成功。
首先,我们在Model层写一个Person类,他有ID,Name,Remark。
同样我们在数据库也有一个Person表,包含id,name,remark三个字段,其中id是主键自增。
而后我们在DAL层写一个PersonDB类,提供对Person的基本访问。connectionString是数据库连接字符串,由配置文件读取。
将一个对象person插入数据库。插入代码文本如下。@Name的意思是自动将person里的Name值绑定上去。
public static int Insert(Person person) { using (IDbConnection connection = new SqlConnection(connectionString)) { return connection.Execute("insert into Person(Name,Remark) values(@Name,@Remark)", person); } }
批量插入:
/// <summary> /// 批量插入Person数据,返回影响行数 /// </summary> /// <param name="persons"></param> /// <returns>影响行数</returns> public static int Insert(List<Person> persons) { using (IDbConnection connection = new SqlConnection(connectionString)) { return connection.Execute("insert into Person(Name,Remark) values(@Name,@Remark)", persons); } }
public static int Delete(Person person) { using (IDbConnection connection = new SqlConnection(connectionString)) { return connection.Execute("delete from Person where id=@ID", person); } } public static int Delete(List<Person> persons) { using (IDbConnection connection = new SqlConnection(connectionString)) { return connection.Execute("delete from Person where id=@ID", persons); } }
public static int Update(Person person) { using (IDbConnection connection = new SqlConnection(connectionString)) { return connection.Execute("update Person set name=@name where id=@ID", person); } } public static int Update(List<Person> persons) { using (IDbConnection connection = new SqlConnection(connectionString)) { return connection.Execute("update Person set name=@name where id=@ID", persons); } }
/// <summary> /// 无参查询所有数据 /// </summary> /// <returns></returns> public static List<Person> Query() { using (IDbConnection connection = new SqlConnection(connectionString)) { return connection.Query<Person>("select * from Person").ToList(); } } /// <summary> /// 查询指定数据 /// </summary> /// <param name="person"></param> /// <returns></returns> public static Person Query(Person person) { using (IDbConnection connection = new SqlConnection(connectionString)) { return connection.Query<Person>("select * from Person where id=@ID", person).SingleOrDefault(); } }
/// <summary> /// In操作 /// </summary> public static List<Person> QueryIn() { using (IDbConnection connection = new SqlConnection(connectionString)) { var sql = "select * from Person where id in @ids"; //参数类型是Array的时候,dappper会自动将其转化 return connection.Query<Person>(sql, new { ids = new int[2] { 1, 2 }, }).ToList(); } } public static List<Person> QueryIn(int[] ids) { using (IDbConnection connection = new SqlConnection(connectionString)) { var sql = "select * from Person where id in @ids"; //参数类型是Array的时候,dappper会自动将其转化 return connection.Query<Person>(sql, new { ids }).ToList(); } }
为此我们引入以下Book类,同样在数据库里设置这个表。
public class Book { public int ID { get; set; } public int PersonID { get; set; } public string BookName { get; set; } }
/// <summary> /// 多语句操作 /// </summary> public static void QueryMultiple() { using (IDbConnection connection = new SqlConnection(connectionString)) { var sql = "select * from Person; select * from Book"; var multiReader = connection.QueryMultiple(sql); var personList = multiReader.Read<Person>(); var bookList = multiReader.Read<Book>(); multiReader.Dispose(); } }
我们是面向对象编程,所以一个对象里面会有许多其他子对象,这个子对象里面又有其自己的子对象,这种关系在数据库里的表示就是外键。
比如我们有一本书book,它有主人person,book是一个对象,主人又是一个对象。
public class BookWithPerson { public int ID { get; set; } public Person Pers { get; set; } public string BookName { get; set; } }
public static BookWithPerson QueryJoin(Book book) { using (IDbConnection connection = new SqlConnection(connectionString)) { var sql = @"select b.id,b.bookName,p.id,p.name,p.remark from Person as p join Book as b on p.id = b.personId where b.id = @id;"; var result = connection.Query<BookWithPerson, Person, BookWithPerson>(sql, (bookWithPerson, person) => { bookWithPerson.Pers = person; return bookWithPerson; }, book); //splitOn: "bookName"); return (BookWithPerson)result; } }
委托回调类型1
,委托回调类型2
,返回类型
。形参的三个参数分别是sql语句
,map委托
,对象参数
。所以整句的意思是先根据sql语句查询;同时把查询的person信息赋值给bookWithPerson.Pers,并且返回bookWithPerson;book是对象参数,提供参数绑定的值。标签:net keep DApp https multi list() cti 分享 com
原文地址:https://www.cnblogs.com/gangle/p/9287082.html