Procedure:
1. Create a class in the database that persists the. NET class.
2. Create a. Net class to be persisted
3. Create a ing file to tell NH how to persist the attributes of these classes.
4. Create the NH configuration file and how to connect the NH to the database in the future
5. Use APIs provided by NH
1. Download nhib.pdf
Go to the Website: http://www.hibernate.org/download the corresponding Nhibernate package
Ii. Example 1: Environment: Windows 7 (64 bit) + vs2010 + SQL Server 2008 + codesmith professional 5.2
Setup1:
Create a database (NH and create a table in the NH database)
View code
UseNH
Go
--Create a data table
Create TableT_persion
(
IDInt Identity(1,1)Primary Key,
NameNvarchar(50),
PWDVarchar(32)
)
Go
Setup2:
Open vs2010 and create a console application project <Nhtest>; InNhtestCreate a model class library project entitymodel under the project
Setup3:
Decompress the nhibernate_template.rar file, open nhib.pdf. CST, and enter the information in codesmith main, such:
Click Generate to generate the file.T_persion.hbm.xml + t_persion.csCopy the generated file to the entitymodel project.
Note: The generated t_persion.hbm.xml must be modified. Otherwise, it cannot be used correctly;
1. Modify the t_persion.hbm.xml file (the modifiedCodeAs shown below)
View code
<? XML version = "1.0" encoding = "UTF-8" ?>
<! -- Here the xmlns = "urn: nhibernate-mapping-2.2" value should be consistent with the value in the nhibernate-mapping.xsd file using nhib.pdf -->
< Hibernate-Mapping Xmlns = "Urn: nhibernate-mapping-2.2" >
< Class Name = "Entitymodel. tpersion, entitymodel" Table = "T_persion" >
<! -- Class attribute name -->
< ID Name = "ID" Type = "Int32" Unsaved-Value = "Null" >
<! -- Database Name -->
< Column Name = "ID" Length = "4" SQL-type = "Int" Not-Null = "True" Unique = "True" Index = "Pk1_t _ persio _ 3213e83f7f60ed59" />
<! -- Indicates that the content is automatically generated. -->
< Generator Class = "Native" />
</ ID >
< Property Name = "Name" Type = "String" >
< Column Name = "Name" Length = "50" SQL-type = "Nvarchar" Not-Null = "False" />
</ Property >
< Property Name = "PWD" Type = "String" >
< Column Name = "PWD" Length = "32" SQL-type = "Varchar" Not-Null = "False" />
</ Property >
</ Class >
</ Hibernate-Mapping >
2. Change the attributes of all the t_persion.hbm.xml files under the entitymodel project to the embedded resources.
Compile the entitymodel project and generate entitymodel. dll <The principle here is: encapsulate the object and the object's ing file.>
Setup3:
Added references to entitymodel. dll and nhib.pdf. DLL files in the nhtest project;
Before proceeding to the next step, let's think about a question: Under ADO. net, what is the procedure for connecting to the database? Should we connect to the database using nhib.pdf?
Setup4: Configure nhib.pdf
Add an application configuration file in the nhtest project and configure it in the app. config file <See the configuration Template under nhib.pdf in the configuration_templates folder.>
View code
<? XML version = "1.0" encoding = "UTF-8" ?>
< Configuration >
< Configsections >
< Section Name = "Hibernate-configuration" Type = "Nhibernate. cfg. configurationsectionhandler" Requirepermission = "True" > </ Section >
</ Configsections >
< Hibernate-Configuration Xmlns = "Urn: nhibernate-configuration-2.2" >
< Session-factory >
<! -- Data-driven providers -->
< Property Name = "Connection. provider" > Nhib.pdf. Connection. driverconnectionprovider, nhib.pdf </ Property >
<! -- Driver for database connection -->
< Property Name = "Connection. driver_class" > Nhib.pdf. Driver. sqlclientdriver </ Property >
<! -- Database connection string -->
< Property Name = "Connection. connection_string" >
Server = (127.0.0.1); initial catalog = nH; persist Security info = true; user id = sa; Password = 20070927
</ Property >
<! -- Page size -->
< Property Name = "Adonet. batch_size" > 10 </ Property >
<! -- Whether to display SQL statements -->
< Property Name = "Show_ SQL" > False </ Property >
< Property Name = "Dialect" > Nhibect. dialect. mssql2008dialect </ Property >
< Property Name = "Command_timeout" > 60 </ Property >
< Property Name = "Query. substitutions" > True 1, false 0, yes 'y', no 'n' </ Property >
</ Session-factory >
</ Hibernate-Configuration >
</ Configuration >
Setup5: Uses APIs of nhibloud to encode the crud of data.