• NHibernate学习笔记(3)-实体反射到数据库


    一、开发环境

    NHiberate版本:4.0.4

    开发工具:VS2013

    数据库:SQLServer2012

    二、开发流程

    1.编写领域类与映射文件

    namespace Domain
    {
        public class Product
        {
            public virtual int Id { get; set; }
            public virtual string Name { get; set; }
            public virtual decimal UnitPrice { get; set; }
        }
    }

    编写映射文件Product.hbm.xml:【嵌入的资源】

    <?xml version="1.0" encoding="utf-8" ?>
    
    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Domain" namespace="Domain">
      <class name="Product" table="T_Product" lazy="true" >
        <id name="Id" column="Id" type="int" >
          <generator class="native" />
        </id>
    
        <property name="Name" type="string">
          <column name="Name" length="50"/>
        </property>
    
        <property name="UnitPrice" type="decimal">
          <column name="UnitPrice" precision="14" scale="2"/>
        </property>
    
      </class>
    </hibernate-mapping>

    2.配置NHiberate文件:hibernate.cfg.xml:【始终复制】

    <?xml version="1.0" encoding="utf-8"?>
    <!-- 
    This template was written to work with NHibernate.Test.
    Copy the template to your NHibernate.Test project folder and rename it in hibernate.cfg.xml and change it 
    for your own use before compile tests in VisualStudio.
    -->
    <!-- This is the System.Data.dll provider for SQL Server -->
    <hibernate-configuration  xmlns="urn:nhibernate-configuration-2.2" >
      <session-factory>
        <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
        <property name="connection.connection_string">
          server=.;database=NHibernateDemo;uid=sa;pwd=love;
        </property>
        <property name="adonet.batch_size">10</property>
        <property name="show_sql">true</property>
        <property name="dialect">NHibernate.Dialect.MsSql2012Dialect</property>
        <property name="command_timeout">60</property>
        <property name="hbm2ddl.auto">update</property>
        <property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
        <mapping assembly="Domain"/>
      </session-factory>
    </hibernate-configuration>

    3.使用NHiberate工具生成对应的数据库结构

    class Program
    {
        static void Main(string[] args)
        {
            var cfg = new NHibernate.Cfg.Configuration().Configure("Config/hibernate.cfg.xml");
            //必须要写,否则无法反射
            using (ISessionFactory sessionFactory = cfg.BuildSessionFactory())
            {
    
            }
            Console.WriteLine("ok");
            Console.ReadKey();
        }
    }

    三、代码下载

    1.程序结构

    image

    2.下载地址

    更多精彩内容请看:http://www.cnblogs.com/2star
  • 相关阅读:
    CopyOnWriteArrayList 写时复制思想
    CAS中ABA问题的解决
    彻底解决Chrome“请停用以开发者模式运行的扩展程序”提示(亲测整合)
    解决软件安装无法自定义文件夹,自动安装在C盘 (Windows系统)
    IDEA降低注解检测级别
    大白话带你认识JVM(转)
    Windows 与 Linux (CentOS7) 之间的文件共享
    Dubbo、Zookeeper 以及 Tomcat 启动的相关问题
    创建 maven 项目的时候遇到的问题
    MyBatis 项目的 jar 包导入与源码导入
  • 原文地址:https://www.cnblogs.com/kimisme/p/5269213.html
Copyright © 2020-2023  润新知