Tables Used
Create Table userinfo
(
Id int identity (1, 1) not null primary key,
Username varchar (50) not null,
PWD varchar (50) not null,
Age int not null
)
Demo project structure:
Domainmodel Layer
Userinfo. CS
Using system;
Namespace domainmodel
{
Public class userinfo
{
Public Virtual int ID {Get; set ;}
Public Virtual string username {set; get ;}
Public Virtual string PWD {set; get ;}
Public Virtual int age {set; get ;}
}
}
Userinfo. HBM. XML (set production operations as embedded resources)
<? XML version = "1.0" encoding = "UTF-8"?>
<Hibernate-mapping xmlns = "urn: nhibernate-mapping-2.2"
Assembly = "domainmodel" namespace = "domainmodel">
<Class name = "domainmodel. userinfo, domainmodel" table = "userinfo">
<ID name = "ID" column = "ID" type = "int32">
<Generator class = "native"> </generator>
</ID>
<Property name = "username" column = "username" type = "string" length = "50" not-null = "true"/>
<Property name = "PWD" column = "PWD" type = "string" length = "50" not-null = "true"/>
<Property name = "Age" column = "Age" type = "int32" not-null = "true"/>
</Class>
</Hibernate-mapping>
Dal layer Introduction
Castle. Core. dll
Castle. dynamicproxy2.dll
Iesi. Collections. dll
Nhib.pdf. dll
Nhibernatehelper. CS
Using system;
Using system. Collections. Generic;
Using system. text;
Using nhib.pdf;
Using nhib.pdf. cfg;
Namespace dal
{
Public static class nhibernatehelper
{
Private Static readonly isessionfactory sessionfactory;
Private Static isession session = NULL;
Static nhibernatehelper ()
{
Sessionfactory = new configuration (). Configure (). buildsessionfactory ();
}
Public static isession getcurrentsession ()
{
If (session = NULL)
{
Session = sessionfactory. opensession ();
}
Return session;
}
Public static void closesessionfactory ()
{
If (sessionfactory! = NULL)
{
Sessionfactory. Close ();
}
}
}
}
Userservice. CS
Using system;
Using system. Collections. Generic;
Using system. text;
Using nhib.pdf;
Using domainmodel;
Namespace dal
{
Public class userservice
{
// Obtain the user by ID
Public userinfo getuserinfobyid (int id)
{
Return nhibernatehelper. getcurrentsession (). Get <userinfo> (ID );
}
}
}
Daltest Layer
Introduce nunit. Framework. dll
Hibernate. cfg. XML (in the bin directory of this layer)
<? XML version = "1.0" encoding = "UTF-8"?>
<Hibernate-configuration xmlns = "urn: nhibernate-configuration-2.2">
<Session-factory>
<Property name = "connection. provider"> nhib.pdf. Connection. driverconnectionprovider </property>
<Property name = "connection. driver_class"> nhib.pdf. Driver. sqlclientdriver </property>
<Property name = "connection. connection_string"> Data Source =. \ sqlexpress; initial catalog = demodb; user id = sa; Password = sa; </property>
<Property name = "show_ SQL"> true </property>
<Property name = "dialect"> nhib.pdf. dialect. mssql2005dialect </property>
<Property name = "use_outer_join"> true </property>
<Mapping Assembly = "domainmodel"/>
</Session-factory>
</Hibernate-configuration>
Daltest. CS (check whether the operation is successful)
Using system;
Using system. Collections. Generic;
Using system. text;
Using domainmodel;
Using nunit. Framework;
Using Dal;
Namespace daltest
{
[Testfixture]
Public class daltestcase
{
Public userservice = NULL;
[Setup]
Public void setup ()
{
Userservice = new userservice ();
}
[Test]
Public void testmethod ()
{
// The username with ID 1 in the database is Zhang
VaR userinfo = userservice. getuserinfobyid (1 );
Assert. areequal (userinfo. username, "Zhang ");
}
}
}
Console output:
------ Test started: Assembly: daltest. dll ------
Nhib.pdf: Select userinfo0 _. ID as id0_0 _, userinfo0 _. username as username0_0 _, userinfo0 _. PWD as pwd0_0 _, userinfo0 _. age as age0_0 _ from userinfo userinfo0 _ Where userinfo0 _. id = @ P0; @ p0 = '1'
1 passed, 0 failed, 0 skipped, took 14.02 seconds (nunit 2.4 ).
It can be seen that this asserted is successfully configured and an SQL statement is printed out!