ASP. net mvc + spring.net + nhib.pdf + easyui + jquery development case (1)

Source: Internet
Author: User
Tags net xml visual studio 2010

Due to the busy work of the company during this period, I have to learn new things every day. So I haven't written a blog for a long time. Today I am taking the time to write a blog to record what I have studied during this period, I was impressed by the fact that I used easyui components to develop a small example of simple operations on the database, and some common database operations were achieved, I hope that I can exchange ideas with the bloggers in my blog.

First, the development environment I used is Visual Studio 2010, ASP. NET mvc2.0, SQL server2005. The nhib.pdf version I used is 1.2.1, and spring.net version is 1.1.0.

First, I would like to explain my doubts here. I hope someone can answer them. I have thanked them here. When I used the versions, I found nhibernate1.2.1 and spring. the best combination of net1.1.0 (earlier version) and nhibernate3.0 and spring.net 1.3.1 (later version). Do you know what I think is correct? However, nhib.pdf cannot be used in earlier versions. only nhibernate3.0 and later versions support nhib.pdf. However, if the later version is used, we did not provide a function in our project, that is, we intentionally wrote the database connection string incorrectly, then, if I execute a page that does not pass through data operations, for example, this page outputs a sentence, but it also reports an error, saying that the database cannot be connected and the user login fails, but what I want is that the page without database operations can be displayed normally. On the database operations page, can I prompt an error? I don't know which bloggers have any good ideas? This effect can be achieved in earlier versions.

    1. Step 1: Create a database

(1) Open SQL Server 2005, create a database bjk241, and create a new table in the database named user_depinfo to store the entered data, shows the table creation style:

 

    1. Step 2: Create a project, class library, etc.

(1) Open the integrated development environment of Visual Studio 2010 and create an ASP. the project named newkj241 for MVC 2.0 will generate the entire built-in MVC Architecture encapsulated by Microsoft, and then create several Class Libraries in the project, it is used to store some projects that I created later. After the project is created:

 

(2) When we see this image, are we wondering what it is? Don't worry. Next I will introduce the functions of various projects and class libraries.

1) first, we can see that the content in newkj241 is different from the one we just created. That's because I re-created several folders and introduced the folders that I didn't have, the configs folder contains the spring.net XML node, which is used to link the database and map the entire framework. I will introduce it in detail below. The DLL folder contains the DLL we want to reference,:

 

The easyui folder stores the easyui class libraries we need to reference. Others are automatically generated when they are created, which will be discussed later.

2) newkj241.model class library is used to store mapped database files.

3) newkj241.idao class library is used to implement the interface for implementing database methods in the project.

4) newkj241. The Role Of The nhibernatedao class library is to implement the idao interface and complete the method to be implemented by the interfaceCode.

5) The newkj241.ibll class library is used to implement the interfaces we want to use spring. net in the project, which is exactly the same as the newkj241.idao interface.

6) newkj241.bll class library implements the Nhibernate implementation method and inherits from the ibll project to use the method written by spring. net.

Next I will explain in detail what I wrote. We can discuss any shortcomings or shortcomings.

    1. Step 3: Nhibernate ing File

(1) first, we need to map the file of table information in the database. How can we map it here? So much code ?? Don't worry. We can use codesmith, a component that automatically generates a ing file. This software can help us quickly generate a ing file, for more information about how to use this software, see my previous blog, where I will detail how to use this software: http://www.cnblogs.com/hanyinglong/archive/2012/04/21/2462302.html.

(2) Next, create a folder mappings in newkj241.model. in this folder, we store the ing file, which is generated and appended to the project:

 

1) The role of userdepinfo is to define fields in the database. When codesmith is used to generate fields, the pattern field attribute is not added to the virtual field. We manually add the field attribute. The Code is as follows:

Protected int _ id;

Protected int _ depid;

Protected string _ depname;

Public userdepinfo (){}

Public userdepinfo (INT depid, string depname)

{

This. _ depid = depid;

This. _ depname = depname;

}

Public Virtual int ID

{

Get {return _ id ;}

Set {_ id = value ;}

}

Public Virtual int depid

{

Get {return _ depid ;}

Set {_ depid = value ;}

}

Public Virtual string depname

{

Get {return _ depname ;}

Set

{

If (value! = NULL & value. length> 50)

Throw new argumentoutofrangeexception ("invalid value for depname", value, value. tostring ());

_ Depname = value;

}

}

2) the code of the ing file is as follows: Note: here we need to change this XML file to an embedded resource. Otherwise, we will not be able to implement the function in advance. How can we change it? It's very easy. Right-click -- attribute -- generate the operation and change it to the embedded resource. This is OK. After the preliminary preparation is complete, we will start to write the method below. You can read my blog at http://www.cnblogs.com/hanyinglong/archive/2012/04/20/2459314.html.

<? XML version = "1.0" encoding = "UTF-8"?>

<Hibernate-mapping xmlns = "urn: nhibernate-mapping-2.2"

Assembly = "newkj241.model. userdepinfo"

Namespace = "newkj241.model. userdepinfo">

<Class name = "newkj241.model. userdepinfo, newkj241.model" table = "user_depinfo" lazy = "false">

<! -- The following is a sentence for using the cache, but the second-level cache in this project is commented out, so it is also commented here -->

<! -- <Cache Usage = "read-write"/> -->

<! -- Department table primary key id -->

<ID name = "ID" type = "int32" unsaved-value = "null">

<Column name = "ID" length = "4" SQL-type = "int" not-null = "true" unique = "true"/>

<Generator class = "native"/>

</ID>

<! -- Department table id -->

<Property name = "depid" type = "int32">

<Column name = "depid" length = "4" SQL-type = "int" not-null = "false"/>

</Property>

<! -- Department table name -->

<Property name = "depname" type = "string">

<Column name = "depname" length = "50" SQL-type = "varchar" not-null = "false"/>

</Property>

</Class>

</Hibernate-mapping>

    1. Step 4: Create a DaO interface to implement database operations

(1) create a class under newkj241.idao class library named iuserdepinfodao, and then write the interface to implement the method in this class. The Code is as follows:

// Where indicates the constraints on the Type Variable T. where T: Class indicates that the type variable t is inherited from the class or the class itself.

Public interface iuserdepinfodao <t> where T: Class

{

/// <Summary>

/// Obtain an object

/// </Summary>

/// <Param name = "ID"> Primary Key </param>

/// <Returns> entity </returns>

T get (Object ID );

 

/// <Summary>

/// Obtain an object

/// </Summary>

/// <Param name = "ID"> Primary Key </param>

/// <Returns> entity </returns>

T load (Object ID );

 

/// <Summary>

/// Insert an object

/// </Summary>

/// <Param name = "entity"> entity </param>

/// <Returns> inserted data </returns>

Object save (T entity );

 

/// <Summary>

/// Modify an object

/// </Summary>

/// <Param name = "entity"> Entity Data </param>

Void Update (T entity );

 

/// <Summary>

/// Save or modify an object

/// </Summary>

/// <Param name = "entity"> Entity Data </param>

Void saveorupdate (T entity );

 

/// <Summary>

/// Delete an object

/// </Summary>

/// <Param name = "ID"> primary key ID </param>

Void Delete (Object ID );

 

/// <Summary>

/// Delete an object

/// </Summary>

/// <Param name = "odlist"> set of primary key IDs, delete multiple items </param>

Void Delete (string idlist );

 

/// <Summary>

/// Read the method for querying without entering any information

/// </Summary>

/// <Param name = "sort"> sort by a field </param>

/// <Param name = "order"> sort by a field </param>

/// <Returns> </returns>

Ilist <userdepinfo> loadbyall (string sort, string order );

 

/// <Summary>

/// Method for reading user input conditions for query

/// </Summary>

/// <Param name = "sort"> sort by a field </param>

/// <Param name = "order"> sort by a field </param>

/// <Param name = "name"> only user-input information </param>

/// <Returns> </returns>

Ilist <userdepinfo> loadallcheck (string sort, string order, string name );

 

/// <Summary>

///

/// </Summary>

/// <Param name = "Total"> total quantity </param>

/// <Param name = "page"> current page number </param>

/// <Param name = "rows"> the number displayed on each page </param>

/// <Param name = "order"> sort by a field </param>

/// <Param name = "sort"> sort by a field </param>

/// <Param name = "depname"> only user-input information </param>

/// <Returns> </returns>

Ilist <userdepinfo> loadallbypage (out long total, int page, int rows, string order, string sort, string depname );

}

There is nothing to say here, and the writing methods are quite fixed !!

    1. Step 5: Create a nhibernatedao project and implement the idao Interface

(1) create a class under the newkj241.nhib.pdf class library named userdepinfodao. This method inherits from the idao interface and implements all the methods in the idao interface. When writing this code, we must introduce DLL, the introduced dll:

 

The Code is as follows:

Public class userdepinfodao <t>: hibernatedaosupport, iuserdepinfodao <t> where T: Class

{

Public t get (Object ID)

{

Return this. hibernatetemplate. Get <t> (ID );

}

 

Public t load (Object ID)

{

Return this. hibernatetemplate. Load <t> (ID );

}

 

Public Virtual Object save (T entity)

{

Return this. hibernatetemplate. Save (entity );

}

 

Public void Update (T entity)

{

This. hibernatetemplate. Update (entity );

}

 

Public void saveorupdate (T entity)

{

This. hibernatetemplate. saveorupdate (entity );

}

 

Public void Delete (Object ID)

{

VaR entity = This. hibernatetemplate. Get <t> (ID );

If (entity = NULL)

{

Return;

}

Else

{

This. hibernatetemplate. Delete (entity );

}

}

 

Public void Delete (string idlist)

{

String [] IDL = idlist. Split (',');

Int Len = IDL. length;

For (INT I = 0; I <Len; I ++)

{

VaR entity = This. hibernatetemplate. Get <t> (Int. parse (IDL [I]. tostring ()));

If (entity = NULL)

{

Return;

}

Else

{

This. hibernatetemplate. Delete (entity );

}

}

}

 

Public ilist <userdepinfo> loadbyall (string sort, string order)

{

String hql = "from userdepinfo order by" + sort + "" + order;

Return this. hibernatetemplate. Find <userdepinfo> (hql). tolist ();

 

}

 

Public ilist <userdepinfo> loadallcheck (string sort, string order, string name)

{

String hql = "from userdepinfo where depname like? Order by "+ sort +" "+ order;

Return this. hibernatetemplate. Find <userdepinfo> (hql, new object [] {"%" + name + "%"}). tolist ();

}

 

 

Public ilist <userdepinfo> loadallbypage (out long total, int page, int rows, string order, string sort, string depname)

{

Ilist <userdepinfo> List = NULL;

If (depname = NULL)

{

List = This. loadbyall (sort, order );

}

Else

{

List = This. loadallcheck (sort, order, depname );

}

Total = List. longcount ();

List = List. Skip (page-1) * Rows). Take (rows). tolist ();

Return list;

}

}

Ah, I have been working for a day. I am tired. I went back to school. I will continue tomorrow. All I have written today is the foundation, and I will write about this project later until this project is completed !!, It is also the most knowledge point. Is it learning? Step by step. Come on !!!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.