PetaPoco是一款适用于.Net 和Mono的微小、快速、单文件的微型ORM。

PetaPoco有以下特色:

  • 微小,没有依赖项……单个的C#文件可以方便的添加到任何项目中。
  • 工作于严格的没有装饰的Poco类,和几乎全部加了特性的Poco类
  • Insert/Delete/Update/Save and IsNew 等帮助方法。
  • 分页支持:自动得到总行数和数据
  • 支持简单的事务
  • 更好的支持参数替换,包括从对象属性中抓取命名的参数。
  • 很好的性能,剔除了Linq,并通过Dynamic方法快速的为属性赋值
  • T4模板自动生成Poco类
  • 查询语言是Sql……不支持别扭的fluent或Linq语法(仁者见仁,智者见智)
  • 包含一个低耦合的Sql Builder类,让内联的Sql更容易书写
  • 为异常信息记录、值转换器安装和数据映射提供钩子。(Hooks for logging exceptions, installing value converters and mapping columns to properties without attributes.)
  • 兼容SQL Server, SQL Server CE, MySQL, PostgreSQL and Oracle。
  • 可以在.NET 3.5 或Mono 2.6或更高版本上运行
  • 在.NET 4.0 和Mono 2.8下支持dynamic
  • NUnit单元测试
  • 开源(Apache License)
  • 所有功能大约用了1500行代码

可以从这里获得PetaPoco:

创建PetaPoco对象,并执行查询:

// Create a PetaPoco database object
var db=new PetaPoco.Database("connectionStringName"); // Show all articles
foreach (var a in db.Query<article>("SELECT * FROM articles"))
{
Console.WriteLine("{0} - {1}", a.article_id, a.title);
}

  得到一个scalar:

long count=db.ExecuteScalar<long>("SELECT Count(*) FROM articles");

  得到一行记录:

var a = db.SingleOrDefault<article>("SELECT * FROM articles WHERE article_id=@0", ));

获取分页数据(将获取一个PageFetch对象):

  PetaPoco分页执行过程:

var result=db.Page<article>(, , // <-- page number and items per page
"SELECT * FROM articles WHERE category=@0 ORDER BY date_posted DESC", "coolstuff");

    1.生成并执行一个查询,获取匹配的数据行数。

    2.修改原始的查询语句,只能得到所有匹配的一个子集。

    pageFetch对象:一个展示单页数据和分页控制的类。

public class Page<T> where T:new()
{
public long CurrentPage { get; set; }
public long ItemsPerPage { get; set; }
public long TotalPages { get; set; }
public long TotalItems { get; set; }
public List<T> Items { get; set; }
}

获取数据的方式Query 和 Fetch:

  Fetch返回一个一个POCO类的List<>,而Query使用迭代所有数据,但是这些数据没有被加载到内存中。 

  不带查询的命令:

db.Execute("DELETE FROM articles WHERE draft<>0");

  Inserts、Updates 和 Deletes:

// Create the article
var a=new article();
a.title="My new article";
a.content="PetaPoco was here";
a.date_created=DateTime.UtcNow; // Insert it
db.Insert("articles", "article_id", a); // by now a.article_id will have the id of the new article
// Get a record
var a=db.SingleOrDefault<article>("SELECT * FROM articles WHERE article_id=@0", ); // Change it
a.content="PetaPoco was here again"; // Save it
db.Update("articles", "article_id", a);
db.Update("articles", "article_id", new { title="New title" }, );
// Delete an article extracting the primary key from a record
db.Delete("articles", "article_id", a); // Or if you already have the ID elsewhere
db.Delete("articles", "article_id", null, ); 

修饰POCO类:

// Represents a record in the "articles" table
[PetaPoco.TableName("articles")]
[PetaPoco.PrimaryKey("article_id")]
public class article
{
public long article_id { get; set; }
public string title { get; set; }
public DateTime date_created { get; set; }
public bool draft { get; set; }
public string content { get; set; }
} 

  简化后的insert、update、delete:

// Insert a record
var a=new article();
a.title="My new article";
a.content="PetaPoco was here";
a.date_created=DateTime.UtcNow;
db.Insert(a); // Update it
a.content="Blah blah";
db.Update(a); // Delete it
db.Delete(a);

  delete和update的其它方式:

// Delete an article
db.Delete<article>("WHERE article_id=@0", ); // Update an article
db.Update<article>("SET title=@0 WHERE article_id=@1", "New Title", );

  忽略某列:

public class article
{
[PetaPoco.Ignore]
public long SomeCalculatedFieldPerhaps
{
get; set;
}
}

  使用类和列的属性来指明哪些列需要映射:

// Represents a record in the "articles" table
[PetaPoco.TableName("articles")]
[PetaPoco.PrimaryKey("article_id")]
[PetaPoco.ExplicitColumns]
public class article
{
[PetaPoco.Column]publiclong article_id { get; set;}
[PetaPoco.Column]publicstring title { get; set;}
[PetaPoco.Column]publicDateTime date_created { get; set;}
[PetaPoco.Column]public bool draft { get; set;}
[PetaPoco.Column]publicstring content { get; set;}
}

T4 模板:

  • PetaPoco.Core.ttinclude - includes all the helper routines for reading the DB schema(数据库模式)
  • PetaPoco.Generator.ttinclude - the actual template that defines what's generated(实体模板)
  • Database.tt - the template itself that includes various settings and includes the two other ttinclude files.(模板本身)

  use the template:

  1. Add the three files to you C# project
  2. Make sure you have a connection string and provider name set in your app.config or web.config file
  3. Edit ConnectionStringName property in Records.tt (ie: change it from "jab" to the name of your connection string)
  4. Save Database.tt.

自动的Select语句:

  运行一个不以select开头的查询, PetaPoco会自动的将它加上。

IsNew 和Save 方法:

  IsNew:检测是否为新增。

  Save:根据判断的结果执行Insert或Update。

事务:

using (var scope=db.Transaction)
{
// Do transacted updates here // Commit
scope.Complete();
}

注意:为了使用事务,所有操作都需要相同的PetaPoco Database对象实例。

PetaPoco 笔记的更多相关文章

  1. Mini ORM——PetaPoco笔记

    Mini ORM--PetaPoco笔记 记录一下petapoco官网博客的一些要点.这些博客记录了PetaPoco是如何一步步改进的. 目录: Announcing PetaPoco PetaPoc ...

  2. Mini ORM——PetaPoco笔记(转)

    记录一下petapoco官网博客的一些要点.这些博客记录了PetaPoco是如何一步步改进的. 目录: Announcing PetaPoco PetaPoco-Improvements PetaPo ...

  3. 微型ORM:PetaPoco 学习资料整理

    github地址:https://github.com/CollaboratingPlatypus/PetaPoco petapoco 实体中字段去掉关联(类似于EF中的NotMap) 微型ORM:P ...

  4. .net群讨论笔记

    本篇为.net群中学到的零碎知识点笔记,将持续更新 感谢群内大神,群号(128485855) 1.关于URL编码问题encodeURIComponent 2.c#直接打开本机的excel(包括其他类型 ...

  5. 如鹏网学习笔记(十五)ASP.NET MVC核心基础笔记

    一.ASP.Net MVC简介 1,什么是ASP.NET MVC? HttpHandler是ASP.net的底层机制,如果直接使用HttpHandler进行开发难度比较大.工作量大.因此提供了ASP. ...

  6. git-简单流程(学习笔记)

    这是阅读廖雪峰的官方网站的笔记,用于自己以后回看 1.进入项目文件夹 初始化一个Git仓库,使用git init命令. 添加文件到Git仓库,分两步: 第一步,使用命令git add <file ...

  7. js学习笔记:webpack基础入门(一)

    之前听说过webpack,今天想正式的接触一下,先跟着webpack的官方用户指南走: 在这里有: 如何安装webpack 如何使用webpack 如何使用loader 如何使用webpack的开发者 ...

  8. SQL Server技术内幕笔记合集

    SQL Server技术内幕笔记合集 发这一篇文章主要是方便大家找到我的笔记入口,方便大家o(∩_∩)o Microsoft SQL Server 6.5 技术内幕 笔记http://www.cnbl ...

  9. PHP-自定义模板-学习笔记

    1.  开始 这几天,看了李炎恢老师的<PHP第二季度视频>中的“章节7:创建TPL自定义模板”,做一个学习笔记,通过绘制架构图.UML类图和思维导图,来对加深理解. 2.  整体架构图 ...

随机推荐

  1. 理解ASP.NET MVC的路由系统

    引言 路由,正如其名,是决定消息经由何处被传递到何处的过程.也正如网络设备路由器Router一样,ASP.NET MVC框架处理请求URL的方式,同样依赖于一张预定义的路由表.以该路由表为转发依据,请 ...

  2. javascript对象定义和操作

    //js对象定义有三种方式//js方法定义有三种方式 function fn(){} var fun = function(){} var fun = new function() {} //**** ...

  3. 部署HBase远程访问的问题集合(Eclipse)

    实现远程访问HBase,可以通过Eclipse开发工具方便进行代码调试. 为了方便jar包各种版本的管理,才用maven进行代码构建 首先,下载并安装maven以及M2Eclipse插件 其次,配置m ...

  4. Shell教程4-Shell替换

    如果表达式中包含特殊字符,Shell 将会进行替换.例如,在双引号中使用变量就是一种替换,转义字符也是一种替换. 举个例子: 复制纯文本新窗口   #!/bin/bash a=10 echo -e & ...

  5. SQLServer如何快速生成100万条不重复的随机8位数字

    最近在论坛看到有人问,如何快速生成100万不重复的8位编号,对于这个问题,有几点是需要注意的: 1.    如何生成8位随机数,生成的数越随机,重复的可能性当然越小 2.    控制不重复 3.    ...

  6. myeclipse 打开xml jsp页面慢 有时候会自动退出

    Myeclipse默认打开文件的方式是 jsp design,每次双击或者使用Ctrl+Shift+R打开 就会用这个打开 ,太慢了而且多次导致Myeclipse挂掉.可以通过以下的方式转化成你想要的 ...

  7. Java RunTime Environment (JRE) or Java Development Kit (JDK) must be available in order to run Eclipse. ......

    mkdir jre cd jre ln -s 你的JDK目录/bin bin 例如:ln -s /usr/lib/jvm/jdk1.8.0_25/bin bin 原文地址:http://www.cnb ...

  8. 【C++】统计代码覆盖率(三)

    报告集成到jenkins才是最终目的,因此又进行了部分资料查找,得到html和xml报告集成jenkins的配置如下: 一 集成html报告 这种方式集成在你已经用gcov+lcov生成了html报告 ...

  9. When not to automate 什么时候不进行自动化

    The cornerstone of test automation is the premise that the expected application behavior is known. W ...

  10. bzoj 3270 博物馆(高斯消元)

    [题意] 两人起始在s,t点,一人pi概率选择留在i点或等概率移动,问两人在每个房间相遇的概率. [思路] 把两个合并为一个状态,(a,b)表示两人所处的状态,设f[i]为两人处于i状态的概率.则有转 ...