NHibernate 学习第五天 用户表的增,删,改,查

1. 数据访问层

  需要一个类  UserSerivce.cs

    

View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using llr.Models;

namespace llr.DAL
{
   public class UserService
    {
       static ISession session;
       public UserService()
       {
           session = new NHibernateHelper().GetSession();
       }

       public static bool Add(User u)
       {
           try
           {
               using (ITransaction ts = session.BeginTransaction())
               {
                   session.Save(u);
                   session.Flush();
                   ts.Commit();
               }
               return true;
           }
           catch (Exception ex)
           {
               throw ex;
           }
       }

       public static bool Update(User u)
       {
           try
           {
               using (ITransaction ts = session.BeginTransaction())
               {
                   session.Update(u);
                   session.Flush();
                   ts.Commit();
               }
               return true;
           }
           catch (Exception ex)
           {
               throw ex;
           }
       }

       public static bool Delete(User u)
       {
           try
           {
               using (ITransaction ts = session.BeginTransaction())
               {
                   session.Delete(u);
                   session.Flush();
                   ts.Commit();
               }
               return true;
           }
           catch (Exception ex)
           {
               throw ex;
           }
 
       }

       public static IList
< User >  SelectAll()
       {
          return session.CreateSQLQuery("select * from users").AddEntity(typeof(User)).List
< User > ();
       }
    }
}

 

 

 

2. 业务层

  需要一个类 UserManager.cs

   

using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Text;
using  llr.Models;
using  llr.DAL;

namespace  llr.BLL
{
   
public   class  UserManager
    {
       
public   static   bool  Add(User u)
       {
           
return  UserService.Add(u);
       }

       
public   static   bool  Update(User u)
       {
           
return  UserService.Update(u);
       }

       
public   static   bool  Delete(User u)
       {
           
return  UserService.Delete(u);
       }

       
public   static  IList < User >  SelectAll()
       {
           
return  UserService.SelectAll();
       }
    }
}

 

3. 表示层

  需要页面显示 用来显示所有的User信息,页面上放一个 GridView 控件

  protected   void  Page_Load( object  sender, EventArgs e)
    {
        
if  ( ! IsPostBack)
        {
            BindUserInfo();
        }
    }

    
void  BindUserInfo()
    {
        IList
< llr.Models.User >  us  =  llr.BLL.UserManager.SelectAll();
        
this .GridView1.DataSource  =  us;
        
this .GridView1.DataBind();
    }

 

4. 数据库结构 T-SQL

 

USE   [ MyNibernate ]
GO
/* ***** Object:  Table [dbo].[Roles]    Script Date: 06/10/2011 15:40:24 ***** */
SET  ANSI_NULLS  ON
GO
SET  QUOTED_IDENTIFIER  ON
GO
CREATE   TABLE   [ dbo ] . [ Roles ] (
    
[ rid ]   [ int ]   IDENTITY ( 1 , 1 NOT   NULL ,
    
[ r_name ]   [ nchar ] ( 10 NULL ,
    
[ r_desc ]   [ nchar ] ( 10 NULL ,
 
CONSTRAINT   [ PK_Roles ]   PRIMARY   KEY   CLUSTERED  
(
    
[ rid ]   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
/* ***** Object:  Table [dbo].[Menus]    Script Date: 06/10/2011 15:40:24 ***** */
SET  ANSI_NULLS  ON
GO
SET  QUOTED_IDENTIFIER  ON
GO
CREATE   TABLE   [ dbo ] . [ Menus ] (
    
[ mid ]   [ int ]   NOT   NULL ,
    
[ m_title ]   [ nchar ] ( 10 NULL ,
    
[ m_parnet ]   [ int ]   NULL ,
    
[ m_url ]   [ nchar ] ( 10 NULL ,
 
CONSTRAINT   [ PK_Menus ]   PRIMARY   KEY   CLUSTERED  
(
    
[ mid ]   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
/* ***** Object:  Table [dbo].[Users]    Script Date: 06/10/2011 15:40:24 ***** */
SET  ANSI_NULLS  ON
GO
SET  QUOTED_IDENTIFIER  ON
GO
CREATE   TABLE   [ dbo ] . [ Users ] (
    
[ Id ]   [ int ]   IDENTITY ( 1 , 1 NOT   NULL ,
    
[ u_name ]   [ nchar ] ( 10 NULL ,
    
[ u_pwd ]   [ nchar ] ( 10 NULL ,
    
[ r_id ]   [ int ]   NOT   NULL ,
 
CONSTRAINT   [ PK_Users ]   PRIMARY   KEY   CLUSTERED  
(
    
[ Id ]   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
/* ***** Object:  Table [dbo].[RoleToMenu]    Script Date: 06/10/2011 15:40:24 ***** */
SET  ANSI_NULLS  ON
GO
SET  QUOTED_IDENTIFIER  ON
GO
CREATE   TABLE   [ dbo ] . [ RoleToMenu ] (
    
[ rid ]   [ int ]   NULL ,
    
[ mid ]   [ int ]   NULL
ON   [ PRIMARY ]
GO
/* ***** Object:  ForeignKey [FK_RoleToMenu_Menus]    Script Date: 06/10/2011 15:40:24 ***** */
ALTER   TABLE   [ dbo ] . [ RoleToMenu ]    WITH   CHECK   ADD    CONSTRAINT   [ FK_RoleToMenu_Menus ]   FOREIGN   KEY ( [ mid ] )
REFERENCES   [ dbo ] . [ Menus ]  ( [ mid ] )
GO
ALTER   TABLE   [ dbo ] . [ RoleToMenu ]   CHECK   CONSTRAINT   [ FK_RoleToMenu_Menus ]
GO
/* ***** Object:  ForeignKey [FK_RoleToMenu_Roles]    Script Date: 06/10/2011 15:40:24 ***** */
ALTER   TABLE   [ dbo ] . [ RoleToMenu ]    WITH   CHECK   ADD    CONSTRAINT   [ FK_RoleToMenu_Roles ]   FOREIGN   KEY ( [ rid ] )
REFERENCES   [ dbo ] . [ Roles ]  ( [ rid ] )
GO
ALTER   TABLE   [ dbo ] . [ RoleToMenu ]   CHECK   CONSTRAINT   [ FK_RoleToMenu_Roles ]
GO
/* ***** Object:  ForeignKey [FK_Users_Roles]    Script Date: 06/10/2011 15:40:24 ***** */
ALTER   TABLE   [ dbo ] . [ Users ]    WITH   CHECK   ADD    CONSTRAINT   [ FK_Users_Roles ]   FOREIGN   KEY ( [ r_id ] )
REFERENCES   [ dbo ] . [ Roles ]  ( [ rid ] )
GO
ALTER   TABLE   [ dbo ] . [ Users ]   CHECK   CONSTRAINT   [ FK_Users_Roles ]
GO

 我在这里:源代码

 

 

你可能感兴趣的:(Hibernate)