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. Notes on how to use Webots, especially how to make a robot fly in the air

    How to create a new project Wizard - New project directory   Scene Tree Scene tree is a representati ...

  2. No connection string named '***' could be found in the application config file

    Code-First时更新数据库遇到妖孽问题“No connection string named '***' could be found in the application config fil ...

  3. [Compose] 21. Apply Natural Transformations in everyday work

    We see three varied examples of where natural transformations come in handy. const Right = x => ( ...

  4. IOS 开发环境,证书和授权文件等详解

    (转自:http://blog.csdn.net/gtncwy/article/details/8617788) 一.成员介绍1.    Certification(证书)证书是对电脑开发资格的认证, ...

  5. Debug Assertion Failed! Expression: _pFirstBlock == pHead

    点击Abort之后,查看调用栈,发现异常在函数return时被时产生,进一步看是vector的析构函数被调用时产生,以前没开发过C++项目,没什么经验,这个错误让我很困惑,第一,我电脑上并没有f盘:第 ...

  6. 命令行下使用javah命令生成.h文件,出现“错误: 无法访问android.app.Activity 找不到android.app.Activity的类文件”的解决方法

    在学习NDK中,当我在项目的bin/classes目录下使用javah命令生成头文件时,出现了“错误: 无法访问android.app.Activity 找不到android.app.Activity ...

  7. 从今天起,记录CEF使用开发心得经验

    已经使用CEF来呈现桌面程序界面大半年了,从来没有写过相关博文.发现网上的中文资料甚至英文已经无法满足我的开发需求,不少问题只得自己探索.在此先谢过网络上各位CEF使用开发博文的贡献者,没有你们我也难 ...

  8. Android-NDK编译:cocos2d-x(三) eclipse 导入工程

    NDK 编译后,用eclipse导入cocos2d-x工程 菜单[File]-->[New]-->[Project] ,弹出New Project 对话框 窗口下方 选 [Android] ...

  9. MySql 数据操作类

    /// <summary> /// MySqlHelper 的摘要说明. /// </summary> public class MySqlHelper { public st ...

  10. apache下virtualhost与location合用配置转发SVN控制访问

    使用apache的文件系统配置 使用virtualhost 实现location 重定向 NameVirtualHost *:80 <VirtualHost *:80> ServerNam ...