一、开发环境

数据库:SQLServer2008

编译器:VS2010

.Net版本:.Net Framework 4.0

二、涉及第三方程序集

NHibernate.dll:版本3.2

Iesi.Collections.dll

三、开发过程

1.项目结构

2.添加Product.cs

namespace NHLearn.Domain
{
public class Product
{
public int Id { get; set; }
public String Name { get; set; }
public decimal UnitPrice { get; set; }
}
}

3.添加Product.hbm.xml,属性为嵌入的资源

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="false">
<class name="NHLearn.Domain.Product,NHLearn.Domain" table="T_Product">
<id name="Id" column="Id" type="int">
<generator class="native" />
</id>
<property name="Name" column="Name" />
<property name="UnitPrice" column="UnitPrice" />
</class>
</hibernate-mapping>

4.配置NHibernate

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler,NHibernate" requirePermission="false"/>
</configSections> <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
<session-factory>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider, NHibernate</property>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property> <property name="connection.connection_string">Server=(local);initial catalog=NHLearn_DB;uid=sa;pwd=woaini</property>
<property name="show_sql">true</property>
<property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
</session-factory>
</hibernate-configuration> </configuration>

5.增删改成

5.0控制台中代码

namespace NHLearn.UI
{
class Program
{
private static ISession session = null;
private static ISessionFactory factory = null;
private static ITransaction trans = null;
static void Main(string[] args)
{
#region 1.0初始化session工厂
Configuration config = new Configuration().AddAssembly("NHLearn.Domain");
factory = config.BuildSessionFactory();
session = factory.OpenSession();
#endregion //Add();
//Query();
//Modify();
//Delete(); Console.ReadKey();
} }
}

5.1添加方法

public static void Add()
{
Product model = new Product()
{
Name = "Apple",
UnitPrice = 2.6m
};
session.Save(model);
}

5.2查询方法

public static void Query()
{
Product model = session.Get(typeof(Product), ) as Product;
Console.WriteLine(model.Name);
}

5.3修复方法

public static void Modify()
{
Product model = session.Get(typeof(Product), ) as Product;
model.Name = "Orange";
session.Update(model);
Console.WriteLine(model.Name);
}

5.4删除方法

public static void Delete()
{
trans = session.BeginTransaction();
try
{
Product model = session.Get(typeof(Product), ) as Product;
session.Delete(model);
trans.Commit();
Console.WriteLine("删除成功");
}
catch (Exception ex)
{
trans.Rollback();
Console.WriteLine(ex.Message);
}
}

四、其他说明

增删改都是可以加事务的

NHibernate3.2学习笔记的更多相关文章

  1. NHibernate3.2学习笔记-几种查询方式

    一.开发环境 数据库:SQLServer2008 编译器:VS2010 .Net版本:.Net Framework 4.0 二.开发过程 1.项目结构 承接上一篇 2.执行sql语句 (1)基本语法 ...

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

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

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

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

  4. PHP-会员登录与注册例子解析-学习笔记

    1.开始 最近开始学习李炎恢老师的<PHP第二季度视频>中的“章节5:使用OOP注册会员”,做一个学习笔记,通过绘制基本页面流程和UML类图,来对加深理解. 2.基本页面流程 3.通过UM ...

  5. 2014年暑假c#学习笔记目录

    2014年暑假c#学习笔记 一.C#编程基础 1. c#编程基础之枚举 2. c#编程基础之函数可变参数 3. c#编程基础之字符串基础 4. c#编程基础之字符串函数 5.c#编程基础之ref.ou ...

  6. JAVA GUI编程学习笔记目录

    2014年暑假JAVA GUI编程学习笔记目录 1.JAVA之GUI编程概述 2.JAVA之GUI编程布局 3.JAVA之GUI编程Frame窗口 4.JAVA之GUI编程事件监听机制 5.JAVA之 ...

  7. seaJs学习笔记2 – seaJs组建库的使用

    原文地址:seaJs学习笔记2 – seaJs组建库的使用 我觉得学习新东西并不是会使用它就够了的,会使用仅仅代表你看懂了,理解了,二不代表你深入了,彻悟了它的精髓. 所以不断的学习将是源源不断. 最 ...

  8. CSS学习笔记

    CSS学习笔记 2016年12月15日整理 CSS基础 Chapter1 在console输入escape("宋体") ENTER 就会出现unicode编码 显示"%u ...

  9. HTML学习笔记

    HTML学习笔记 2016年12月15日整理 Chapter1 URL(scheme://host.domain:port/path/filename) scheme: 定义因特网服务的类型,常见的为 ...

随机推荐

  1. Ubuntu 16.04无法在WPS中输入中文的问题解决

    1. sudo gedit /usr/bin/wps 增加 export XMODIFIERS="@im=fcitx" export QT_IM_MODULE="fcit ...

  2. cakephp2.3.8中何为component

    大胆假设,小心求证         记得这句话是以前读高三的时候,一个数学老师(圆头,有点胖胖的老师,不是很记得),经常挂在嘴边的一句话, 对于我们不理解,或者说是无法确定的东西,我们不妨先大胆地去假 ...

  3. ubuntu 中 iptables 和 ufw 的关系

    我突然发现,自己平常使用的 iptables 和 ufw 到底是啥关系?平常其实iptables和ufw在配置防火墙,开启端口是,还是偶尔会使用到的. 没去思考过这两者是啥关系,哎...,这就不够好了 ...

  4. [VueJS + Typescript] Decouple Dependencies Using IoC Containers in Vue with TypeScript and InversifyJS

    Using Object Oriented Programming, OOP, style allows us to apply Inversion of Control, IoC, and more ...

  5. CLLocation的属性以及使用的解释

    http://blog.csdn.net/u012496940/article/details/47405345  上一篇的链接(一个定位实例) 从上一篇中的实例了解所使用的一些元素: CLLcati ...

  6. webpack-输出

    输出(Output) 配置 output 选项可以控制 webpack 如何向硬盘写入编译文件. 注意,即使可以存在多个入口起点,但只指定一个输出配置. 用法(Usage) 在 webpack 中配置 ...

  7. PromiseKit入门

    原文:Getting Started with PromiseKit 作者:Michael Katz 译者:kmyhy 异步编程真的让人头疼.不管你怎样小心,总是easy出现臃肿的托付.混乱的完毕句柄 ...

  8. debug 和release 的区别

    http://blog.csdn.net/h_wlyfw/article/details/26688677

  9. initial ram disk

    1 什么是initial ram disk 它就是一个做好了的文件系统,其存储空间是ram.在kernel启动的第一个阶段,会被mount成根文件系统. 2 为什么需要initial ram disk ...

  10. shell 爬虫 从日志分析到数据采集与分析

    [root@VM_61_158_centos ~]# curl http://ip.chinaz.com/220.112.233.179 |grep -e Whwtdhalf.*span.*span. ...