博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
NHibernate配置
阅读量:5073 次
发布时间:2019-06-12

本文共 5605 字,大约阅读时间需要 18 分钟。

1 准备工具 

  NHibernate-GA  下载地址

      NHibernate模板生成   下载地址

2,新建数据库

View Code
create database [TestNHibernatea] go USE [TestNHibernatea] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[Roles]( [RoleId] [int] IDENTITY(1,1) NOT NULL, [RoleName] [varchar](50) COLLATE Chinese_PRC_CI_AS NULL, CONSTRAINT [PK_Roles] PRIMARY KEY CLUSTERED ( [RoleId] ASC )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF --Users表  USE [TestNHibernatea] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[Users]( [UserId] [int] IDENTITY(1,1) NOT NULL, [FirstName] [varchar](50) COLLATE Chinese_PRC_CI_AS NULL, [LastName] [varchar](50) COLLATE Chinese_PRC_CI_AS NULL, [Sex] [varchar](8) COLLATE Chinese_PRC_CI_AS NULL, [Age] [int] NULL, [RoleId] [int] NULL, CONSTRAINT [PK_Users] PRIMARY KEY CLUSTERED ( [UserId] ASC )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO ALTER TABLE [dbo].[Users]  WITH CHECK ADD  CONSTRAINT [FK_Users_Roles] FOREIGN KEY([RoleId]) REFERENCES [dbo].[Roles] ([RoleId]) GO ALTER TABLE [dbo].[Users] CHECK CONSTRAINT [FK_Users_Roles]

3,新建ASP.NET网站  添加类库 DLL,BLL Model 层。

使用 模板生成器生成Model层代码,

OutputDirectory 为生成文件的路径。

SourceDarabase  为数据库名称, 

Assembly 为配置文件里相关属性设置, 

Namespace为类的命名空间。

将生成的.XML文件和.cs文件复制到Model层

    .XML 文件 文件生成操作必须是 嵌入的资源。

View Code
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections; namespace ProjectModel {
[Serializable] public class Roles {
public virtual int RoleId { get; set; } public virtual string RoleName { get; set; } public virtual IList Users { get; set; } } }

在WEB层添加引用 

在DLL层引用  NHibernate.DLL文件  和Model层

在BLL引用  DLL和Model层

DLL层新建NHibernateSession

View Code
private static Configuration cfg; private static ISessionFactory factory; private object _lockObj; public NHibernateSession()         {
cfg = new Configuration().Configure(); factory = cfg.BuildSessionFactory(); _lockObj = new object(); } private ISession session; public ISession Session {
get {
lock (_lockObj) {
if (session == null) {
session = factory.OpenSession(); } } return session; } } /// /// 操作新增的公共方法 /// /// ///
public int AddObject(object obj) {
int result = 0; ITransaction tran = Session.BeginTransaction(); try {
Session.Save(obj); tran.Commit(); result = 1; } catch (Exception ex) {
tran.Rollback(); throw ex; } finally {
Session.Close(); } return result; } /// /// 删除的公共方法 /// /// ///
public int DeleteObject(object obj) {
int result = 0; ITransaction tran = Session.BeginTransaction(); try {
Session.Delete(obj); tran.Commit(); result = 1; } catch (Exception ex) {
tran.Rollback(); throw ex; } finally {
Session.Close(); } return result; } /// /// 更新的公共方法 /// /// ///
public int UpdateObject(object obj) {
int result = 0; ITransaction tran = Session.BeginTransaction(); try {
Session.Update(obj); tran.Commit(); result = 1; } catch (Exception ex) {
tran.Rollback(); throw ex; } finally {
Session.Close(); } return result; } }

新增业务逻辑层

View Code
public class ProjectUserDLL     {
NHibernateSession nb = new NHibernateSession(); public IList
GetRoles() {
string hql = "from Roles"; List
list = (List
)nb.Session.CreateQuery(hql).List
(); return list; } public IList
GetUsers() {
string hql = "from Users"; List
list = (List
)nb.Session.CreateQuery(hql).List
(); return list; } }

新增BLL层

View Code
public class ProjectUserBLL     {
ProjectUserDLL dll = new ProjectUserDLL(); public IList
GetRoles() {
return dll.GetRoles(); } public IList
GetUsers() {
return dll.GetUsers(); } }

在WEB层Bin目录下新增hibernate.cfg.xml文件用于数据库配置

View Code
NHibernate.Driver.SqlClientDriver
Data Source=127.0.0.1;Initial Catalog=TestNHibernatea;uid=sa;Password=sa;
true
NHibernate.Dialect.MsSql2008Dialect
NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle

页面调用

View Code
ProjectUserBLL p = new ProjectUserBLL(); this.Store1.DataSource = p.GetRoles(); this.Store1.DataBind();

 

转载于:https://www.cnblogs.com/haojiahuo/archive/2011/12/01/2270538.html

你可能感兴趣的文章