我的NHibernate Helper

NHibernate 数据操作封装
本文介绍了一个针对 NHibernate 的数据操作封装类,支持获取 SessionFactory、执行 SQL 语句、保存、更新、删除对象等常见操作,并提供了生成 DDL 文件的功能。
NHibernate 常用操作的封装,支持根据Mapping File生成DDL.

ContractedBlock.gif ExpandedBlockStart.gif
None.gifusing System;
None.gif
using System.Collections.Generic;
None.gif
using System.Text;
None.gif
using System.Collections;
None.gif
using System.Data;
None.gif
None.gif
using Training.Common;
None.gif
using NHibernate.Cfg;
None.gif
using NHibernate;
None.gif
using NHibernate.Engine;
None.gif
None.gif
namespace Com.Baige
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
public static class NHibernateHelper
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ContractedSubBlock.gifExpandedSubBlockStart.gif        
For data operation#region For data operation
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// The singleton Sessionfactory
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private static NHibernate.ISessionFactory SessionFactory = null;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// The configuration file name
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private static string ConfigFileName = "hibernate.cfg.xml";
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Gets the sessionfactory
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="assemblyName">Application assembly name ,configuration will search the objects in the assembly</param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        private static ISessionFactory GetSessionFactory(string assemblyName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (SessionFactory != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return SessionFactory;
ExpandedSubBlockEnd.gif            }

InBlock.gif            NHibernate.Cfg.Configuration Cfg 
= null;
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Cfg 
= new Configuration().Configure(ConfigFileName);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch  (Exception e)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                NHibernate.HibernateException ex 
= new HibernateException(e);
InBlock.gif                Logger.Error(e.Message);
InBlock.gif                
throw (ex);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
if (assemblyName != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                System.Reflection.Assembly aa 
= System.Reflection.Assembly.Load(assemblyName);
InBlock.gif                Cfg.AddAssembly(aa);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                SessionFactory 
= Cfg.BuildSessionFactory();
InBlock.gif                
return SessionFactory;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (Exception e)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                NHibernate.HibernateException ex 
= new HibernateException(e);
InBlock.gif                Logger.Error(e.Message);
InBlock.gif                
throw (ex);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Another method of gets sessions
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        private static ISessionFactory GetSessionFactory()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return GetSessionFactory(null);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Get session
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public static ISession CurrentSession()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ISessionFactory factory 
= GetSessionFactory();
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ISession Session 
= factory.OpenSession();
InBlock.gif                
return Session;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (Exception e)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                NHibernate.HibernateException ex 
= new HibernateException(e);
InBlock.gif                Logger.Error(e.Message);
InBlock.gif                
throw (ex);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Reload configuration
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public static void ReloadConfig()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            SessionFactory 
= null;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Use Hibernate to execute SQL statment
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="query"></param>
ExpandedSubBlockEnd.gif        
/// <returns></returns> IList

InBlock.gif        public static System.Collections.IList ExecuteSQL(string query)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            System.Collections.IList result 
= new ArrayList();
InBlock.gif
InBlock.gif            ISessionFactoryImplementor s 
= (ISessionFactoryImplementor) GetSessionFactory();
InBlock.gif            IDbCommand cmd 
= s.ConnectionProvider.Driver.CreateCommand();
InBlock.gif            cmd.CommandText 
= query;
InBlock.gif
InBlock.gif            IDbConnection conn 
= s.OpenConnection();
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                cmd.Connection 
= conn;
InBlock.gif                IDataReader rs 
= cmd.ExecuteReader();
InBlock.gif
InBlock.gif                
while (rs.Read())
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
int fieldCount = rs.FieldCount;
InBlock.gif                    
object[] values = new Object[fieldCount];
InBlock.gif                    
for (int i = 0; i < fieldCount; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        values[i] 
= rs.GetValue(i);
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    result.Add(values);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (Exception e)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                NHibernate.HibernateException ex 
= new HibernateException(e);
InBlock.gif                Logger.Error(e.Message);
InBlock.gif                
throw (ex);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                s.CloseConnection(conn);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return result;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Saves an object to DB
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="obj"></param>

InBlock.gif        public static void SaveObject(Object obj)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ISession session 
= CurrentSession();
InBlock.gif            ITransaction tr 
= session.BeginTransaction();
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                session.Save(obj);
InBlock.gif                tr.Commit();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                tr.Rollback();
InBlock.gif                Logger.Error(ex.Message);
InBlock.gif                
throw (ex);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (session.IsOpen)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    session.Close();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Update an object to DB
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="obj"></param>

InBlock.gif        public static void UpdateObject(Object obj)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ISession session 
= CurrentSession();
InBlock.gif            ITransaction tr 
= session.BeginTransaction();
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                session.Update(obj);
InBlock.gif                tr.Commit();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                tr.Rollback();
InBlock.gif                Logger.Error(ex.Message);
InBlock.gif                
throw (ex);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (session.IsOpen)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    session.Close();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// SaveOrUpdate an object to DB
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="obj"></param>

InBlock.gif        public static void SaveOrUpdateObject(Object obj)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ISession session 
= CurrentSession();
InBlock.gif            ITransaction tr 
= session.BeginTransaction();
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                session.SaveOrUpdate(obj);
InBlock.gif                tr.Commit();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                tr.Rollback();
InBlock.gif                Logger.Error(ex.Message);
InBlock.gif                
throw (ex);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (session.IsOpen)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    session.Close();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// <summary>
InBlock.gif        
/// Delete an object
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="obj"></param>

InBlock.gif        public static void DeleteObject(Object obj)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ISession session 
= CurrentSession();
InBlock.gif            ITransaction tr 
= session.BeginTransaction();
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                session.Delete(obj);
InBlock.gif                tr.Commit();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                tr.Rollback();
InBlock.gif                Logger.Error(ex.Message);
InBlock.gif                
throw (ex);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (session.IsOpen)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    session.Close();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Find  by HQL
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="obj"></param>

InBlock.gif        public static System.Collections.IList Find(string HQL)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ISession session 
= CurrentSession();
InBlock.gif            System.Collections.IList list 
= null;
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                list 
= session.Find(HQL);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Logger.Error(ex.Message);
InBlock.gif                
throw (ex);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (session.IsOpen)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    session.Close();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
return list;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// To get the specify  object from DB
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="ClassType"></param>
InBlock.gif        
/// <param name="obj"></param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public static Object GetObject(Type ClassType, object ID)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ISession session 
= CurrentSession();
InBlock.gif            
object Getobj = null;
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Getobj 
= session.Get(ClassType, ID);
InBlock.gif                
return Getobj;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Logger.Error(ex.Message);
InBlock.gif                
throw (ex);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (session.IsOpen)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    session.Close();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// To see whether an object exists
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="ClassType"></param>
InBlock.gif        
/// <param name="obj"></param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public static bool ObjExist(Type ClassType, object ID)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
object Getobj = GetObject(ClassType, ID);
InBlock.gif            
if (Getobj == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return false;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return true;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
For making hbm2ddl#region For making hbm2ddl
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// DDL file path
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private static string DllOutPutPath = @"D:\\HbmDDL.txt";
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Configuration file name
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private static string DllMappingFile = "hibernate.cfg.xml";
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Make DDL file
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="hbmConfigurationfile">The hbm configuration file</param>

InBlock.gif        public static void MakeDDL(string hbmConfigurationfile)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (!System.IO.File.Exists(hbmConfigurationfile))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return;
ExpandedSubBlockEnd.gif            }

InBlock.gif            NHibernate.Cfg.Configuration Cfg 
= null;
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif               Cfg
= new Configuration().Configure(hbmConfigurationfile);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (Exception e)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Logger.Error(e.Message);
InBlock.gif                
throw new HibernateException(e);
ExpandedSubBlockEnd.gif            }

InBlock.gif            NHibernate.Tool.hbm2ddl.SchemaExport schemaExport 
= new NHibernate.Tool.hbm2ddl.SchemaExport(Cfg);
InBlock.gif            schemaExport.SetOutputFile(DllOutPutPath);
InBlock.gif            schemaExport.Create(
falsefalse);
InBlock.gif            
//schemaExport.Execute(false, false, false, false);
ExpandedSubBlockEnd.gif
        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Make DDL file
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public static void MakeDDL()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            MakeDDL(DllMappingFile);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

转载于:https://www.cnblogs.com/njbaige/archive/2006/05/11/397671.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值