NHibernate初学一之简单增删改查(二)
L就没有效果:
复制代码
using Wujy.ModelLibrary.Entity;
using NHibernate;
using Wujy.DalLibrary.DalHelp;
namespace Wujy.DalLibrary
{
public class SchoolDal
{
private ISession isession;
public SchoolDal()
{
isession = new NHibernateHelper().GetSession();
}
public void Add(SchoolModel model)
{
//如查不用transaction.Commit()则要运和isession.Flush()否则无法执行SQL就没有效果
//ITransaction transaction = isession.BeginTransaction();
//isession.Save(model);
//transaction.Commit();
isession.Save(model);
isession.Flush();
}
public bool Update(SchoolModel model)
{
try
{
isession.Update(model);
isession.Flush();
return true;
}
catch (Exception ex)
{
return false;
}
finally
{
isession.Close();
}
}
public bool Delete(SchoolModel model)
{
try
{
isession.Delete(model);
isession.Flush();
return true;
}
catch (Exception ex)
{
return false;
}
finally
{
isession.Close();
}
}
public SchoolModel GetSchoolById(Guid ID)
{
return isession.Get(ID);
}
public IList GetSchoolList()
{
IList
list = null;
list = isession.QueryOver().List();
return list;
}
}
}
复制代码
4:因为本实例重点是对NHibernate运用,对于逻辑层就简单的引用;代码也很简单:
复制代码
using Wujy.ModelLibrary.Entity;
using Wujy.DalLibrary;
namespace Wujy.BllLibrary
{
public class SchoolBll
{
public static void Add(SchoolModel model)
{
new SchoolDal().Add(model);
}
public static bool Update(SchoolModel model)
{
return new SchoolDal().Update(model);
}
public static bool Delete(SchoolModel model)
{
return new SchoolDal().Delete(model);
}
public static SchoolModel GetSchoolById(Guid ID)
{
return new SchoolDal().GetSchoolById(ID);
}
public static IList GetSchoolList()
{
return new SchoolDal().GetSchoolList();
}
}
}
复制代码
5:WebUI除简单调用BLL层外,另一个比较重要是NHibernate连接数据库的配置文件,其中其属性复制到输出目录改为"始终复制";Hibernate.cfg.xml内容:
复制代码
< xml version="1.0" encoding="utf-8" >