zoukankan      html  css  js  c++  java
  • NHibernate使用

    参考:http://www.cnblogs.com/hbhzsysutengfei/p/6078898.html  博客

    首先结构如下:

    首先添加数据库的配置文件hibernate.cfg.xml(生成操作嵌入式资源和始终复制)

    <?xml version="1.0" encoding="utf-8" ?>
    
    <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    
      <session-factory>
    
        <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
    
        <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
    
        <property name="connection.connection_string">Data Source=.;user=sa;password=654123;Initial Catalog=NHibernate</property>
    
        <property name="dialect">NHibernate.Dialect.MsSql2012Dialect</property>
    
        <property name="show_sql">true</property>
    
        <property name="hbm2ddl.auto">update</property>
    
    
    
        <!--Mapping files 嵌入式资源 表格table 名字不可以是user-->
    <mapping assembly="MyNHibernate"/>
    </session-factory>
    </hibernate-configuration>

    增加一个User实体

          所有的属性都是使用virtual来修饰(延迟加载有用),添加对应的get set方法

             在MSServer 中,user 是关键字,因此不能够有user的表格,可以使用其他来代替

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace MyNHibernate.Models
    {
        public class User
        {
            public virtual int Id { get; set; }
            public virtual string CustomName { get; set; }
            public virtual string Account { get; set; }
            public virtual string Password { get; set; }
            public virtual string Email { get; set; }
            public virtual string Mobile { get; set; }
            public virtual int? CompanyId { get; set; }
            public virtual string CompanyName { get; set; }
            public virtual int State { get; set; }
            public virtual int UserType { get; set; }
            public virtual DateTime? LastLoginTime { get; set; }
            public virtual int CreatorId { get; set; }
            public virtual DateTime CreateTime { get; set; }
            public virtual int? LastModifierId { get; set; }
            public virtual DateTime? LastModifyTime { get; set; }
        }
    }

    创建实体映射User.hbm.xml,并设置为嵌入资源(右键—属性—生成操作—嵌入的资源)

    <?xml version="1.0" encoding="utf-8" ?>
    <!--assembly:项目名称,namespace:在项目中Model类的命名空间,如果省略的话,需要在后面的class name属性中填写完整的名字空间和类名-->
    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="MyNHibernate" namespace="MyNHibernate.Models" default-lazy="true">
    
      <!--注意table的名字不能写成user,否则报错,因为user是数据库的关键字-->
      <!--注意:name的第一个为类的命名空间,第二个为程序集的名字-->
      <class name="MyNHibernate.Models.User,MyNHibernate" table="[dbo].[User]" lazy="true" dynamic-insert="true" dynamic-update="true">
      <!--<class name="User" table="client">-->
        <id name="Id" column="[Id]">
          <generator class="identity" />
        </id>
        <property name="CustomName" column="[Name]"/>
        <property name="Account" column="[Account]"/>
        <property name="Password" column="[Password]"/>
        <property name="Email" column="[Email]"/>
        <property name="Mobile" column="[Mobile]"/>
        <property name="CompanyId" column="[CompanyId]"/>
        <property name="CompanyName" column="[CompanyName]"/>
        <property name="State" column="[State]"/>
        <property name="UserType" column="[UserType]"/>
        <property name="LastLoginTime" column="[LastLoginTime]"/>
        <property name="CreatorId" column="[CreatorId]"/>
        <property name="CreateTime" column="[CreateTime]"/>
        <property name="LastModifierId" column="[LastModifierId]"/>
        <property name="LastModifyTime" column="[LastModifyTime]"/>
    
      </class>
    
    </hibernate-mapping>

    编写NHibernate的帮助类NHibernateFactory,初始化NHibernate的环境,获取Session

    using NHibernate;
    using NHibernate.Cfg;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace MyNHibernate
    {
        public class NHibernateFactory
        {
             private static readonly  ISessionFactory sessionFactory;
    
            private static string HibernateHbmXmlFileName = "hibernate.cfg.xml";
    
            //private static ISession session
    
            static NHibernateFactory()
    
            {
                string totalPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ConfigFiles\hibernate.cfg.xml");
                Configuration configuration = new Configuration().Configure(totalPath);
                //iSessionFactory = configuration.BuildSessionFactory();
                sessionFactory = configuration.BuildSessionFactory();// new Configuration().Configure().BuildSessionFactory();
    
            }
    
            public static ISessionFactory GetSessionFactory()
    
            {
    
                return sessionFactory;
    
            }
    
            public static ISession GetSession()
    
            {
    
                return sessionFactory.OpenSession();
    
            }
    
            public static void CloseSessionFactory()
    
            {
    
                
    
            }
        }
    }

    查询和添加:

    using MyNHibernate.Models;
    using NHibernate;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace MyNHibernate
    {
        class Program
        {
            static void Main(string[] args)
            {
    
                User user = new User();// createUser();
                user.Account = "qaer";
                user.CompanyId = 1;
                user.Password = "123456456";
                user.UserType = 2;
                user.State = 1;
                user.CreateTime = DateTime.Now;
                user.CreatorId = 1;
                user.CompanyName = "ceshi";
                user.Email = "126585899@qq.com";
                user.CustomName = "23213";
                user.LastModifierId = 1;
                user.Mobile = "5585464454";
                ISession session = NHibernateFactory.GetSession();
                var dd = session.Get<User>(7);
    var data = session.Query<User>().Where(c => c.Id > 4).ToList(); ITransaction tx
    = session.BeginTransaction(); session.Save(user); tx.Commit(); session.Close();
    } } }

    未能加载问题:http://blog.csdn.net/tyh800220/article/details/1733133

  • 相关阅读:
    控制器之间的通信(传值方法)以及多次调用通信
    关于ios项目沙盒中的文件和Xcode项目创建的文件
    解决cell循环利用造成的重复勾选
    让TabelView视图中自定义的Toolbar固定(不随cell的移动而移动)
    jsonString转NSDictionary
    日期字符串转换 and 两个日期相减
    Java虚拟机 简介
    浅谈操作系统对内存的管理(转)
    Java虚拟机规范(Java SE 7)笔记
    StringUtils
  • 原文地址:https://www.cnblogs.com/1107988049-qq/p/6640593.html
Copyright © 2011-2022 走看看