http://blog.bennymichielsen.be/2009/01/04/using-fluent-nhibernate-in-spring-net/

http://comments.gmane.org/gmane.comp.windows.dotnet.nhibernate.user.general/21840

http://codegur.com/2049968/configuring-asp-net-mvc-2-with-spring-net-and-fluentnhibernate

SQL:

CREATE TABLE StaffManager
(
ManagerId INT IDENTITY(1,1) PRIMARY KEY,
ManagerStaffIdKey INT FOREIGN KEY
REFERENCES StaffMember(StaffId), --职员ID 外键
ManagerIs BIT DEFAULT(1), --账号是否有效
ManagerName NVARCHAR(50) NOT NULL, --登录账号
ManagerPassWord VARCHAR(50) NOT NULL, --密码
ManagerMail VARCHAR(100) NOT NULL, --找回密码邮箱
ManagerDate DATETIME DEFAULT(GETDATE())
--管理人员ID 外键
)
GO

  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spring.Data.NHibernate; //4.0
using NHibernate.Cfg;
using FluentNHibernate.Automapping;
using FluentNHibernate.Conventions.Helpers;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using System.Reflection;
using FluentNHibernate;
using NHibernate.Tool.hbm2ddl;
using NHibernate; namespace BasicProject.NHibernateInfra.Implementation {
public class FluentNhibernateLocalSessionFactoryObject : LocalSessionFactoryObject{
///// <summary>
///// Sets the assemblies to load that contain fluent nhibernate mappings.
///// </summary>
///// <value>The mapping assemblies.</value>
//public string[] FluentNhibernateMappingAssemblies {
// get;
// set;
//} public string[] FluentNhibernateMappingAssemblies { get; set; }
public string ConnectionStringName { get; set; }
static readonly object factorylock = new object(); protected override void PostProcessConfiguration(Configuration config) {
ConnectionStringName = "Server=geovindu;Database=geovindu;User ID=root;Password=geovindu";
base.PostProcessConfiguration(config);
FluentConfiguration fluentConfig = Fluently.Configure(config)
.Database(MySQLConfiguration.Standard.ConnectionString(ConnectionStringName))
.ExposeConfiguration(cfg => new SchemaUpdate(cfg).Execute(false, true)); Array.ForEach(FluentNhibernateMappingAssemblies,
assembly => fluentConfig.Mappings(
m => m.FluentMappings.AddFromAssembly(Assembly.Load(assembly))
.Conventions.Add(FluentNHibernate.Conventions.Helpers.DefaultLazy.Never())
)
);
fluentConfig.BuildSessionFactory();
}
} //class
}

  

using System;
using System.Text;
using System.Collections.Generic;
using NHibernate.Validator.Constraints; namespace Domain.Entities { public class StaffManager {
public StaffManager() {
CardCancellations = new List<CardCancellation>();
CardRecords = new List<CardRecord>();
JobRightsAssignments = new List<JobRightsAssignment>();
StaffManagerLogs = new List<StaffManagerLog>();
StaffUserChecks = new List<StaffUserCheck>();
ViolateRegulations = new List<ViolateRegulation>();
}
public virtual int ManagerId { get; set; }
public virtual StaffMember StaffMember { get; set; }
public virtual bool? ManagerIs { get; set; }
[NotNullNotEmpty]
[Length(50)]
public virtual string ManagerName { get; set; }
[NotNullNotEmpty]
[Length(50)]
public virtual string ManagerPassWord { get; set; }
[NotNullNotEmpty]
[Length(100)]
public virtual string ManagerMail { get; set; }
public virtual DateTime? ManagerDate { get; set; }
public virtual IList<CardCancellation> CardCancellations { get; set; }
public virtual IList<CardRecord> CardRecords { get; set; }
public virtual IList<JobRightsAssignment> JobRightsAssignments { get; set; }
public virtual IList<StaffManagerLog> StaffManagerLogs { get; set; }
public virtual IList<StaffUserCheck> StaffUserChecks { get; set; }
public virtual IList<ViolateRegulation> ViolateRegulations { get; set; }
}
}

  

using System;
using System.Collections.Generic;
using System.Text;
using FluentNHibernate.Mapping;
using Domain.Entities; namespace Domain.Mappings { /// <summary>
///
/// </summary>
public class StaffManagerMap : ClassMap<StaffManager> { public StaffManagerMap() {
Table("StaffManager");
LazyLoad();
Id(x => x.ManagerId).GeneratedBy.Identity().Column("ManagerId");
References(x => x.StaffMember).Column("ManagerStaffIdKey");
Map(x => x.ManagerIs).Column("ManagerIs");
Map(x => x.ManagerName).Column("ManagerName").Not.Nullable().Length(50);
Map(x => x.ManagerPassWord).Column("ManagerPassWord").Not.Nullable().Length(50);
Map(x => x.ManagerMail).Column("ManagerMail").Not.Nullable().Length(100);
Map(x => x.ManagerDate).Column("ManagerDate");
HasMany(x => x.CardCancellations).KeyColumn("CancelManagerIdKey");
HasMany(x => x.CardRecords).KeyColumn("CardManagerIdKey");
HasMany(x => x.JobRightsAssignments).KeyColumn("RightsManagerIdKey");
HasMany(x => x.StaffManagerLogs).KeyColumn("ManagerIdKey");
HasMany(x => x.StaffUserChecks).KeyColumn("CheckManagerIdKey");
HasMany(x => x.ViolateRegulations).KeyColumn("ViolateManagerIdKey");
}
}
}

  

 public abstract class Repository<T> : ILongKeyedRepository<T> where T : class{
private ISessionFactory sessionFactory;
/// <summary>
/// Session factory for sub-classes.
/// </summary>
public ISessionFactory SessionFactory {
protected get { return sessionFactory; }
set { sessionFactory = value; }
} /// <summary>
/// Get's the current active session. Will retrieve session as managed by the
/// Open Session In View module if enabled.
/// </summary>
public ISession CurrentSession {
get { return SessionFactory.GetCurrentSession(); }
} public T FindBy(long id) {
return CurrentSession.Get<T>(id);
} public IQueryable<T> All() {
return CurrentSession.Query<T>();
} public T FindBy(System.Linq.Expressions.Expression<Func<T, bool>> expression) {
throw new NotImplementedException();
} public IQueryable<T> FilterBy(System.Linq.Expressions.Expression<Func<T, bool>> expression) {
return All().Where(expression).AsQueryable();
} public void Add(T entity) {
CurrentSession.Save(entity);
} public void Add(IEnumerable<T> entities) {
foreach (var entity in entities) {
CurrentSession.Save(entity);
}
} public void Update(T entity) {
CurrentSession.Update(entity);
} public void Delete(T entity) {
CurrentSession.Delete(entity);
} public void Delete(IEnumerable<T> entities) {
foreach (var entity in entities) {
CurrentSession.Save(entity);
}
}
} //end class

  

    public class StaffManagerRepository : Repository<StaffManager> {

    } /

  Web.config:

  <spring>
<context>
<resource uri="~/Config/objects.xml"/> <!-- 嵌入在程序集中的配置文件 ,首先是程序集名称,然后命名空间,最后文件名, 注意名称的大小写必须完全一致 -->
<resource uri="assembly://geovindu.Dao/geovindu.Dao.Config/dataAccess.xml"/>
<resource uri="assembly://geovindu.Dao/geovindu.Dao.Config/objects.xml"/>
<resource uri="assembly://geovindu.Service/geovindu.Service.Config/objects.xml"/>
</context> <!--数据库配置服务器地址-->
<databaseSettings>
<add key="db.server" value="LF-WEN\GEOVINDU"/>
<add key="db.database" value="TSQLFundamentals2008"/>
<add key="db.userid" value="sa"/>
<add key="db.password" value="7888888"/>
</databaseSettings>
</spring>

  

objects.xml
    <object id="SessionFactory" type="geovindu.NHibernateInfra.Implementation.FluentNhibernateLocalSessionFactoryObject, geovindu.NHibernateInfra.Implementation">
<!--<property name="DbProvider" ref="DbProvider"/>-->
<property name="ConnectionStringName" value="MysqlConnection"/>
<property name="ExposeTransactionAwareSessionFactory" value="true" />
<property name="FluentNhibernateMappingAssemblies">
<list>
<value>geovindu.NHibernateInfra.Implementation</value>
</list>
</property>
<property name="HibernateProperties">
<dictionary>
<entry key="connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/>
<!--<entry key="dialect" value="NHibernate.Dialect.MsSql2008Dialect"/>
<entry key="connection.driver_class" value="NHibernate.Driver.SqlClientDriver"/>-->
<!--<entry key="current_session_context_class" value="Spring.Data.NHibernate.SpringSessionContext, Spring.Data.NHibernate33"/>-->
<entry key="cache.use_second_level_cache" value="true" />
<entry key="cache.provider_class"
value="NHibernate.Cache.HashtableCacheProvider,NHibernate" />
<entry key="max_fetch_depth" value="0" />
</dictionary>
</property> <property name="ExposeTransactionAwareSessionFactory" value="true" />
</object> <!-- Transaction Management Strategy - local database transactions -->
<object id="transactionManager"
type="Spring.Data.NHibernate.HibernateTransactionManager, Spring.Data.NHibernate33">
<property name="DbProvider" ref="DbProvider"/>
<property name="SessionFactory" ref="SessionFactory"/>
</object>

  

 DAO:objects.xml
  <!-- NHibernate 配置 涂聚文 Geovin Du -->

<!-- 可以通过 name 为其指定别名 name="SessionFactory" -->
<object id="NHibernateSessionFactory" type="Spring.Data.NHibernate.LocalSessionFactoryObject,Spring.Data.NHibernate4">
<!--关于数据库连接的配置,直接使用 DbProvider 中的设置,这样,不需要为 Hibernate 再提供连接串和驱动-->
<property name="DbProvider" ref="DbProvider"/>
<!--包含有映射文件的程序集,需要分析的hbm程序集名称-->
<property name="MappingAssemblies">
<list>
<value>Domain</value>
</list>
</property>
<!--其他的参数-->
<property name="HibernateProperties">
<dictionary>
<!--方言-->
<entry key="dialect" value="NHibernate.Dialect.MsSql2005Dialect"/>
<entry key="use_proxy_validator" value="false" />
<entry key="show_sql" value="true"/>
</dictionary>
</property>
<!--必须增加此项说明,与 Spring 的声明式事务集成-->
<property name="ExposeTransactionAwareSessionFactory" value="true" />
</object> <!-- Fluent NHibernate 配置 涂聚文 Geovin Du--> <!-- 可以通过 name 为其指定别名 name="SessionFactory" -->
<object id="NHibernateSessionFactory" type="Geovin.Du.Domain.FluentNhibernateLocalSessionFactoryObject, Domain">
<!--关于数据库连接的配置,直接使用 DbProvider 中的设置,这样,不需要为 Hibernate 再提供连接串和驱动-->
<property name="DbProvider" ref="DbProvider"/>
<!--包含有映射文件的程序集,需要分析的Mappings程序集名称-->
<property name="FluentNhibernateMappingAssemblies">
<list>
<value>Domain</value>
</list>
</property>
<!--其他的参数-->
<property name="HibernateProperties">
<dictionary>
<!--方言-->
<entry key="dialect" value="NHibernate.Dialect.MsSql2005Dialect"/>
<entry key="use_proxy_validator" value="false" />
<entry key="show_sql" value="true"/>
</dictionary>
</property>
<!--必须增加此项说明,与 Spring 的声明式事务集成-->
<property name="ExposeTransactionAwareSessionFactory" value="true" />
</object>

  

Fluent NHibernate and Spring.net的更多相关文章

  1. 全自动迁移数据库的实现 (Fluent NHibernate, Entity Framework Core)

    在开发涉及到数据库的程序时,常会遇到一开始设计的结构不能满足需求需要再添加新字段或新表的情况,这时就需要进行数据库迁移. 实现数据库迁移有很多种办法,从手动管理各个版本的ddl脚本,到实现自己的mig ...

  2. 【翻译】Fluent NHibernate介绍和入门指南

    英文原文地址:https://github.com/jagregory/fluent-nhibernate/wiki/Getting-started 翻译原文地址:http://www.cnblogs ...

  3. Fluent Nhibernate之旅(五)--利用AutoMapping进行简单开发

    Fluent Nhibernate(以下简称FN)发展到如今,已经相当成熟了,在Nhibernate的书中也相应的推荐了使用FN来进行映射配置,之前写的FN之旅至今还有很多人会来私信我问题,说来惭愧, ...

  4. [Fluent NHibernate]第一个程序

    目录 写在前面 Fluent Nhibernate简介 基本配置 总结 写在前面 在耗时两月,NHibernate系列出炉这篇文章中,很多园友说了Fluent Nhibernate的东东,也激起我的兴 ...

  5. [Fluent NHibernate]一对多关系处理

    目录 写在前面 系列文章 一对多关系 总结 写在前面 上篇文章简单介绍了,Fluent Nhibernate使用代码的方式生成Nhibernate的配置文件,以及如何生成持久化类的映射文件.通过上篇的 ...

  6. Fluent NHibernate and Mysql,SQLite,PostgreSQL

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

  7. Fluent NHibernate关系映射

    1.好处:Fluent NHibernate让你不再需要去写NHibernate的标准映射文件(.hbm.xml), 方便了我们的代码重构,提供了代码的易读性,并精简了项目代码 实现: (1).首先我 ...

  8. fluent nhibernate 初体验

    离开.net框架两年时间,发展的很快呀.原先自我感觉良好到以为只差一个MVP的考核什么的,现在觉得真的差好远了. 呵呵,废话就不多说了.这次花了两天时间才拿下fluent nhibernate的fir ...

  9. Fluent NHibernate之旅

    Fluent NHibernate 之旅 导航篇: [原创]Fluent NHibernate之旅开篇: [原创]Fluent NHibernate之旅二--Entity Mapping: [原创]F ...

随机推荐

  1. Customer IEnuramble Extension

    public static class IEnurambleExtension { public static IEnumerable<TSource> DistinctBy<TSo ...

  2. Equals Finalize GetHashCode GetType MemberwiseClone ReferenceEquals ToString String.IsInterned

    参考资料: http://blog.csdn.net/afgasdg/article/details/6889383 http://www.cnblogs.com/skyivben/archive/2 ...

  3. Java Web 工作技巧总结 16.10

    摘要: 原创出处:www.bysocket.com 泥瓦匠BYSocket 希望转载,保留摘要,谢谢! 在你成为领导者以前,成功只同自己的成长有关.当你成为领导者以后,成功都同别人的成长有关. 1.聊 ...

  4. nginx server_name

    在我的机子了nginx的  server_name要配制成127.0.0.1才能用,否则就报错,刚试用nginx还不知道为什么,先记下来

  5. zk框架销毁Page上的Component

    销毁Page上的Component ZK的组件之间是树状结构的,每一组件都只有一个根. 从页面上销毁一个组件可以通过下面两种方式来实现: 1. 组件不是根组件时:Component.setParent ...

  6. 不同gdb,相同数据集合并

    众所周知,数据处理是GIS中一项重要且繁琐的工作,处理数据的工具和方法也太多了,在做数据处理的时候,经常会遇到这样的问题:对存储在不同gdb中.并且数据集名称相同的数据进行合并处理: 如图:数据组织如 ...

  7. 白话Redis与Memcached区别

    如果简单地比较Redis与Memcached的区别,外在的区别是: 1  Redis不仅仅支持简单的k/v类型的数据,同时还提供list,set,zset,hash等数据结构的存储. 2  Redis ...

  8. ECMAScript5新增对象语法糖getter和setter

    在新的ECMAScript5中新添加了两个语法糖,这两个语法糖是这样的. var obj = (function(){ var num = 10; return { get n(){ return n ...

  9. Innodb Read IO 相关参数源代码解析

    前言:最近在阅读Innodb IO相关部分的源代码.在阅读之前一直有个疑问,show global status 中有两个指标innodb_data_reads 和 innodb_data_read. ...

  10. [IR] Information Extraction

    阶段性总结 Boolean retrieval 单词搜索 [Qword1 and Qword2]               O(x+y) [Qword1 and Qword2]- 改进: Gallo ...