Nhibernate + MySQL 类型映射

用SQLyog工具创建表

然后用自动映射工具NHibernate Mapping Generator对表做自动映射,得到

这个是可视化界面,后面有对应的代码。

using System;
using System.Collections.Generic;
using System.Text;
using FluentNHibernate.Mapping;
using Sample.CustomerService.Domain; 

namespace Sample.CustomerService.Maps {

    public class TTesttypeMap : ClassMap<TTesttype> {

        public TTesttypeMap() {
            Table("t_testtype");
            LazyLoad();
            Id(x => x.Id).GeneratedBy.Assigned().Column("Id");
            Map(x => x.Type2).Column("type2");
            Map(x => x.Type3).Column("type3");
            Map(x => x.Type4).Column("type4");
            Map(x => x.Type5).Column("type5");
            Map(x => x.Type6).Column("type6");
            Map(x => x.Type7).Column("type7");
            Map(x => x.Type8).Column("type8");
            Map(x => x.Type9).Column("type9");
            Map(x => x.Type10).Column("type10");
            Map(x => x.Type11).Column("type11");
            Map(x => x.Type12).Column("type12");
            Map(x => x.Type13).Column("type13");
            Map(x => x.Type14).Column("type14");
            Map(x => x.Type15).Column("type15");
            Map(x => x.Type16).Column("type16");
            Map(x => x.Type17).Column("type17");
            Map(x => x.Type18).Column("type18");
            Map(x => x.Type19).Column("type19");
            Map(x => x.Type20).Column("type20");
            Map(x => x.Type21).Column("type21");
            Map(x => x.Type22).Column("type22");
            Map(x => x.Type23).Column("type23");
            Map(x => x.Type24).Column("type24");
            Map(x => x.Type25).Column("type25");
            Map(x => x.Type26).Column("type26");
            Map(x => x.Type27).Column("type27").Not.Nullable();
            Map(x => x.Type28).Column("type28");
            Map(x => x.Type29).Column("type29");
            Map(x => x.Type30).Column("type30");
            Map(x => x.Type31).Column("type31");
            Map(x => x.Type32).Column("type32");
            Map(x => x.Type33).Column("type33");
        }
    }
}

实体:

using System;
using System.Text;
using System.Collections.Generic;

namespace Sample.CustomerService.Domain {

    public class TTesttype {
        public virtual string Id { get; set; }
        public virtual int? Type2 { get; set; }
        public virtual string Type3 { get; set; }
        public virtual string Type4 { get; set; }
        public virtual byte[] Type5 { get; set; }
        public virtual int? Type6 { get; set; }
        public virtual int? Type7 { get; set; }
        public virtual string Type8 { get; set; }
        public virtual DateTime? Type9 { get; set; }
        public virtual DateTime? Type10 { get; set; }
        public virtual string Type11 { get; set; }
        public virtual string Type12 { get; set; }
        public virtual string Type13 { get; set; }
        public virtual float? Type14 { get; set; }
        public virtual int? Type15 { get; set; }
        public virtual string Type16 { get; set; }
        public virtual string Type17 { get; set; }
        public virtual string Type18 { get; set; }
        public virtual int? Type19 { get; set; }
        public virtual string Type20 { get; set; }
        public virtual string Type21 { get; set; }
        public virtual string Type22 { get; set; }
        public virtual string Type23 { get; set; }
        public virtual int? Type24 { get; set; }
        public virtual string Type25 { get; set; }
        public virtual DateTime? Type26 { get; set; }
        public virtual DateTime Type27 { get; set; }
        public virtual string Type28 { get; set; }
        public virtual int? Type29 { get; set; }
        public virtual string Type30 { get; set; }
        public virtual string Type31 { get; set; }
        public virtual string Type32 { get; set; }
        public virtual string Type33 { get; set; }
    }
}

第二种映射是通过xml映射文件来做的。

映射文件:

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping assembly="Sample.CustomerService" namespace="Sample.CustomerService.Domain" xmlns="urn:nhibernate-mapping-2.2">
  <class name="TTesttype" table="t_testtype" lazy="true" >
    <id name="Id" column="Id" />
    <property name="Type2">
      <column name="type2" sql-type="bigint(20)" not-null="false" />
    </property>
    <property name="Type3">
      <column name="type3" sql-type="binary(1)" not-null="false" />
    </property>
    <property name="Type4">
      <column name="type4" sql-type="bit(1)" not-null="false" />
    </property>
    <property name="Type5">
      <column name="type5" sql-type="blob" not-null="false" />
    </property>
    <property name="Type6">
      <column name="type6" sql-type="tinyint(1)" not-null="false" />
    </property>
    <property name="Type7">
      <column name="type7" sql-type="tinyint(1)" not-null="false" />
    </property>
    <property name="Type8">
      <column name="type8" sql-type="char(1)" not-null="false" />
    </property>
    <property name="Type9">
      <column name="type9" sql-type="date" not-null="false" />
    </property>
    <property name="Type10">
      <column name="type10" sql-type="datetime" not-null="false" />
    </property>
    <property name="Type11">
      <column name="type11" sql-type="decimal(10,0)" not-null="false" />
    </property>
    <property name="Type12">
      <column name="type12" sql-type="double" not-null="false" />
    </property>
    <property name="Type13">
      <column name="type13" sql-type="enum(‘A‘,‘B‘,‘C‘)" not-null="false" />
    </property>
    <property name="Type14">
      <column name="type14" sql-type="float" not-null="false" />
    </property>
    <property name="Type15">
      <column name="type15" sql-type="int(11)" not-null="false" />
    </property>
    <property name="Type16">
      <column name="type16" sql-type="longblob" not-null="false" />
    </property>
    <property name="Type17">
      <column name="type17" sql-type="longtext" not-null="false" />
    </property>
    <property name="Type18">
      <column name="type18" sql-type="mediumblob" not-null="false" />
    </property>
    <property name="Type19">
      <column name="type19" sql-type="mediumint(9)" not-null="false" />
    </property>
    <property name="Type20">
      <column name="type20" sql-type="mediumtext" not-null="false" />
    </property>
    <property name="Type21">
      <column name="type21" sql-type="decimal(10,0)" not-null="false" />
    </property>
    <property name="Type22">
      <column name="type22" sql-type="double" not-null="false" />
    </property>
    <property name="Type23">
      <column name="type23" sql-type="set(‘X‘,‘Y‘,‘Z‘)" not-null="false" />
    </property>
    <property name="Type24">
      <column name="type24" sql-type="smallint(6)" not-null="false" />
    </property>
    <property name="Type25">
      <column name="type25" sql-type="text" not-null="false" />
    </property>
    <property name="Type26">
      <column name="type26" sql-type="time" not-null="false" />
    </property>
    <property name="Type27">
      <column name="type27" sql-type="timestamp" not-null="true" />
    </property>
    <property name="Type28">
      <column name="type28" sql-type="tinyblob" not-null="false" />
    </property>
    <property name="Type29">
      <column name="type29" sql-type="tinyint(4)" not-null="false" />
    </property>
    <property name="Type30">
      <column name="type30" sql-type="tinytext" not-null="false" />
    </property>
    <property name="Type31">
      <column name="type31" sql-type="varbinary(1024)" not-null="false" />
    </property>
    <property name="Type32">
      <column name="type32" sql-type="varchar(8)" not-null="false" />
    </property>
    <property name="Type33">
      <column name="type33" sql-type="year(4)" not-null="false" />
    </property>
  </class>
</hibernate-mapping>

没什么内容,只是记录一下,因为在做映射的时候,都不知道怎么匹配类型,这里可以做个参考。

时间: 2024-08-27 05:49:20

Nhibernate + MySQL 类型映射的相关文章

NHibernate+MySql常见问题

版本: NHibernate :NHibernate 4.0.1GA MySql:MySql 5.0.1 常见问题一: "NHibernate.HibernateException"类型的未经处理的异常在 NHibernate.dll 中发生 其他信息: Could not create the driver from NHibernate.Driver.MySqlDataDriver. 解决方案: 把MySql.Data.dll程序集引入到项目中,这个问题就解决了.原因在于找不到驱动

Hibernate 基本类型映射

Hibernate映射类型 Java类型 标准SQL类型 大小和取值范围 integer 或者 int int 或者 Integer INTEGER 4字节,-2^31 ~ 2^31-1 long long 或者 Long BIGINT 8字节,-2^63 ~ 2^63-1 short short 或者 Short SMALLINT 2字节,-2^15 ~ 2^15-1 byte byte 或者 Byte TINYINT 1字节,-128 ~ 127 float float 或者 Float F

[hibernate]基本值类型映射之日期类型

hibernate基本值类型映射中日期类型支持date,time,timestamp这三个选项,其中 date:对应数据库中的date类型,表示只显示日期 time:对应数据库中的time类型,表示只显示时间 timestamp:对应数据库中的timestamp类型,如果数据库支持datetime类型则对应的是datetime类型,表示日期时间都显示(此处为经过本人测试后的猜测). 以下摘自:开源中国 MySQL 中用来存储完整时间的字段包括有 TIMESTAMP 和 DATETIME 两个.很

[Spring Data MongoDB]学习笔记--_id和类型映射

_id字段的映射: MongoDB要求所有的document都要有一个_id的字段. 如果我们在使用中没有传入_id字段,它会自己创建一个ObjectId. { "_id" : ObjectId("53e0ff0b0364cb4a98ce3bfd"), "_class" : "org.springframework.data.mongodb.examples.hello.domain.Person", "name&q

总结Unity IOC容器通过配置实现类型映射的几种基本使用方法

网上关于Unity IOC容器使用的方法已很多,但未能做一个总结,故我这里总结一下,方便大家选择. 首先讲一下通过代码来进行类型映射,很简单,代码如下: unityContainer = new UnityContainer(); //实例化一个容器 unityContainer.RegisterType<IClassTest, ClassTest>(); //注册类型映射 unityContainer.Resolve<IClassTest>();//解析并获取类型的实例 然而实际

Unity3D游戏开发之使用场景Ⅰ:建立类型映射

欢迎来到unity学习.unity培训.unity企业培训教育专区,这里有很多Unity3D资源.Unity3D培训视频.Unity3D教程.Unity3D常见问题.Unity3D项目源码,[狗刨学习网]unity极致学院,致力于打造业内unity3d培训.学习第一品牌. 总的来看,Unity的使用场景主要有以下几个: 建立类型映射 用于单例模式 用于依赖注入 接下来,将逐一介绍各使用场景. 在创建对象时,毫无疑问,类型映射是我们无法回避的一个问题,同时也是一系列工厂模式的根本出发点.类型映射,

Unity IOC容器通过配置实现类型映射的几种基本使用方法

网上关于Unity IOC容器使用的方法已很多,但未能做一个总结,故我这里总结一下,方便大家选择. 首先讲一下通过代码来进行类型映射,很简单,代码如下 unityContainer = new UnityContainer(); //实例化一个容器 unityContainer.RegisterType<IClassTest, ClassTest>(); //注册类型映射 unityContainer.Resolve<IClassTest>();//解析并获取类型的实例 然而实际情

数据词典与ABAP类型映射

声明:原创作品,转载时请注明文章来自SAP师太技术博客:www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将追究法律责任!原文链接:http://www.cnblogs.com/jiangzhengjun/p/4292791.html 数据词典与ABAP类型映射 这些不能够直接在ABAP程序中使用,但可以使用对它们进行包装后的Data element来间接使用,在程序中使用数据字典类型时,将转化为相应的ABAP内置类型. Type 允许最大长度m M

Windows 7安装MySQL最后一步提示错误“mysql 终结点映射器中没有更多的终结点可用的” 解决方法

今天在Windows7系统安装MySQL5.5的时候,碰到错误提示"mysql 终结点映射器中没有更多的终结点可用的" 原因如下: 点击确认后,MySQL可以正常使用