In previous USING NHIBERNATE WITH SQLITE, we connect SQLITE with ORM framework NHibernate. One of the biggest advantage of ORM is that most code need not be changed when switching different DBMS. In this article, MySQL is used for DBMS.

For the switching, hibernate.cfg.xml and default.build have to be updated.

1. hibernate.cfg.xml

Update the driver and connection string.

 <?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory name="NHibernate.Test">
<property name="connection.driver_class">NHibernate.Driver.MySqlDataDriver</property>
<property name="connection.connection_string">Server=localhost;Database=test;User ID=root;Password=</property>
<property name="dialect">NHibernate.Dialect.MySQLDialect</property>
<property name="query.substitutions">true=1;false=0</property>
<property name="show_sql">true</property>
</session-factory>
</hibernate-configuration>

2. default.xml

Update assembly dependencies.

 <?xml version="1.0"?>
<project name="SQLite with NHibernate" default="run">
<property name="debug" value="true" />
<property name="outdir" value="bin" />
<property name="libdir" value="../lib" />
<property name="mysql_libdir" value="../../MySQL/lib/v4.0" />
<target name="clean" description="remove all generated files">
<delete dir="${outdir}" />
</target>
<target name="build" description="compiles the source code">
<mkdir dir="${outdir}" />
<csc debug="${debug}" output="${outdir}/NHExample.exe" target="exe">
<references basedir="${libdir}">
<include name="NHibernate.dll" />
</references>
<sources>
<include name="Domain/Product.cs" />
<include name="NHExample.cs" />
</sources>
<resources dynamicprefix="true" prefix="NHExample.Domain">
<include name="Mappings/*.hbm.xml" />
</resources>
</csc>
<copy todir="${outdir}">
<fileset basedir="${libdir}">
<include name="NHibernate.dll" />
<include name="NHibernate.xml" />
<include name="Iesi.Collections.dll" />
<include name="Iesi.Collections.xml" />
</fileset>
</copy>
<copy todir="${outdir}">
<fileset basedir="etc">
<include name="hibernate.cfg.xml" />
</fileset>
</copy>
<copy todir="${outdir}">
<fileset basedir="${mysql_libdir}">
<include name="mysql.data.dll" />
<include name="mysql.data.entity.dll" />
</fileset>
</copy>
</target>
<target name="run" depends="build">
<exec program="${outdir}/NHExample.exe" workingdir="${outdir}"/>
</target>
</project>

执行的输出结果:

Done. We have make the DBMS switching without coding and changing mappings.

USING NHIBERNATE WITH MySQL的更多相关文章

  1. 【C#】使用NHibernate连接MySQL数据库及增删改查

    学习资料 http://www.sikiedu.com/course/51/task/891/show https://www.codeproject.com/Articles/26123/NHibe ...

  2. 使用NHibernate连接MySQL数据库及增删改查

    学习资料 http://www.sikiedu.com/course/51/task/891/show https://www.codeproject.com/Articles/26123/NHibe ...

  3. 利用NHibernate与MySQL数据库交互

    本文章使用Visual Studio作为开发工具,并建立在已经安装MySQL数据库的前提. NHibernate是一个面向.NET环境的对象/关系数据库映射工具.官网:http://nhibernat ...

  4. Fluent NHibernate and Mysql,SQLite,PostgreSQL

    http://codeofrob.com/entries/sqlite-csharp-and-nhibernate.html https://code.google.com/archive/p/csh ...

  5. MySql(一)_利用NHibernate和MySql交互

    1.基础配置,添加MySql和nHibernate的引用 (1)   添加引用,导入MySql.data.dll:   利用MySql提供的API操作: (2) 添加引用,导入NHibernate.d ...

  6. NHibernate+MySql (erro 解决方法)

    error1.  Could not create the driver from NHibernate.Driver.MySqlDataDriver 解决方法:在使用Nhibernate连接Mysq ...

  7. [NHibernate] 入门实例 NHibernate 3.3 GA + VS2010 +MySQL

    题外话: 中国的技术论坛要赶上stackoverflow的水平.至少还需35年.中国程序猿笔者的特点是:太浮躁,太easy下总结.太自得其乐,虽说写的是为了让别人更好的看.却也演变成了一种" ...

  8. 【NHibernate】列“ReservedWord”不属于表 ReservedWords

    NHibernate+FluentNHibernate+MySql 运行时黄页显示下边的异常,项目中找了半天没出现过这个列的关键字. [ArgumentException: 列“ReservedWor ...

  9. 架构之路(九)Session Per Request

    前面的两篇反应很差:没评论没赞.很伤心啊,为什么呢?搞得我好长一段时间都没更新了——呵呵,好吧,我承认,这只是我的借口.不过,还是希望大家多给反馈.没有反馈,我就只能猜了:前面两篇是不是写得太“粗”了 ...

随机推荐

  1. [代码]--给GridControl中的某列添加图片

    要让GridControl的某列显示图片只需要数据源中有图片就可以正确显示 1.给DataSet添加一列,格式为image ds.Tables[].Columns.Add("SIGN&quo ...

  2. linux服务器mysql数据库新建数据库并配置数据库用户

    第一步:进入数据库 mysql -uroot -p 提示输入密码,输入你的root用户密码(默认不显示) 如下图: 第二步:创建一个数据库 create database 数据库名称 ;(注意分号结尾 ...

  3. Luogu2661 信息传递(图论)

    Luogu2661 信息传递(图论) Description 有n个同学(编号为1到n)正在玩一个信息传递的游戏.在游戏里每人都有一个固定的信息传递对象,其中,编号为i的同学的信息传递对象是编号为Ti ...

  4. (转)在Eclipse中用TODO标签管理任务(Task)

    背景:eclipse是一款功能十分强大的编辑,如果能够熟练运用,必定事半功倍,但如果不求甚解,无疑是给自己制造麻烦. 1 标签的使用 1.1 起因 如上图所示,在程序中有很多todo的标签出现,但是却 ...

  5. 调试技巧 ------ printf 的使用技巧

    编译器宏:__FUNCTION__,__FILE__,__LINE__ #define __debug #ifdef __debug //#define debug(format,...) print ...

  6. Python学习笔记5-时间模块time/datetime

    import time time.sleep(2) #等待几秒 # 1.格式化好的时间 2018-1-14 16:42 # 2.时间戳 是从unix元年到现在所有的秒数 # 3.时间元组 #想时间戳和 ...

  7. MongoDB探索之路(三)——索引

    1.索引介绍 2.创建语句 1)基础索引 在字段age 上创建索引,1(升序);-1(降序):db.users.ensureIndex({age:1}) _id 是创建表的时候自动创建的索引,此索引是 ...

  8. php拾遗: 类型约束

    突然间什么都不想干,感觉就像来大姨夫一样..但是又不能断了每个工作日都写博客的习惯..所以今天水一下吧. PHP用了快2年了,但是这东西竟然第一次看到,突然间,觉得自己有掉回战五渣的行列了.翻开官方文 ...

  9. AngularJs-$parsers自我理解-解析

    $parsers 首先先了解下它具体的作用,当用户与控制器进行交互的时候.ngModelController中的$setViewValue()方法就会被调用,$parsers的数组中函数就会以流水线的 ...

  10. js和jquery使按钮失效为不可用状态的方法

    设置disabled属性为true即为不可用状态. html代码: <input type="button" value="提交" id="bt ...