zoukankan      html  css  js  c++  java
  • USING NHIBERNATE WITH MySQL

    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.

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

    2. default.xml

    Update assembly dependencies.

     1 <?xml version="1.0"?>
     2 <project name="SQLite with NHibernate" default="run">
     3     <property name="debug" value="true" />
     4     <property name="outdir" value="bin" />
     5     <property name="libdir" value="../lib" />
     6     <property name="mysql_libdir" value="../../MySQL/lib/v4.0" />
     7     <target name="clean" description="remove all generated files">
     8         <delete dir="${outdir}" />
     9     </target>
    10     <target name="build" description="compiles the source code">
    11         <mkdir dir="${outdir}" />
    12         <csc debug="${debug}" output="${outdir}/NHExample.exe" target="exe">
    13             <references basedir="${libdir}">
    14                 <include name="NHibernate.dll" />
    15             </references>
    16             <sources>
    17                 <include name="Domain/Product.cs" />
    18                 <include name="NHExample.cs" />
    19             </sources>
    20             <resources dynamicprefix="true" prefix="NHExample.Domain">
    21                 <include name="Mappings/*.hbm.xml" />
    22             </resources>
    23         </csc>
    24         <copy todir="${outdir}">
    25             <fileset basedir="${libdir}">
    26               <include name="NHibernate.dll" />
    27               <include name="NHibernate.xml" />
    28               <include name="Iesi.Collections.dll" />
    29               <include name="Iesi.Collections.xml" />
    30             </fileset>
    31         </copy>
    32         <copy todir="${outdir}">
    33             <fileset basedir="etc">
    34               <include name="hibernate.cfg.xml" />
    35             </fileset>
    36         </copy>
    37         <copy todir="${outdir}">
    38             <fileset basedir="${mysql_libdir}">
    39                 <include name="mysql.data.dll" />
    40                 <include name="mysql.data.entity.dll" />
    41             </fileset>
    42         </copy>
    43     </target>
    44     <target name="run" depends="build">
    45         <exec program="${outdir}/NHExample.exe" workingdir="${outdir}"/>
    46     </target>
    47 </project>
    View Code

    执行的输出结果:

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

  • 相关阅读:
    [UWP]使用CompositionLinearGradientBrush实现渐变画笔并制作动画
    [WPF 自定义控件]模仿UWP的ProgressRing
    [UWP]占领标题栏
    [WPF 自定义控件]关于ScrollViewer和滚动轮劫持(scroll-wheel-hijack)
    [WPF 自定义控件]给WPF一个HyperlinkButton
    VisualStudio中的单元测试
    重温《单元测试的艺术》,总结常用知识点
    [WPF 自定义控件]自定义Expander
    nhibernate入门使用经验
    个人搜藏小技巧:eclipse 设定proxy,仍不能连网的问题
  • 原文地址:https://www.cnblogs.com/huys03/p/3485629.html
Copyright © 2011-2022 走看看