JSP + SPRING + Hibernate

xiaoxiao2021-03-14  185

Special thanks to

Before the article is the most beginning, I would like to thank my friends, I will give me a great help from his great help, and it is very good for this part of the knowledge. Familiar with and very enthusiastic people, I would like to thank Steady and his forum http://www.js66.com/java/index.php, and friends on the forum, because JSF is a new technology, Steady And the friends on his forum are trying to translate the e-text document, and answer questions about a variety of newcomers on his forum. I hope that Chinese programmers will be able to get started faster in this part of this. The development of JSF made his greatest contribution, I was fortunate to join their open source organization, and I will do my best to help everyone.

first part

Design framework

Nowadays, all things must be well coupled, based on this idea, there is a new approach to web page programming: JSF Spring Hibernate.

In the performance layer, we used JSF. The greatest advantage of JSF is the page componentization, and the traditional page programming is different. The traditional page is going out of a page PORST, and JSF will put the value that is filled in each page control and each The event of the page control makes a binding, through the use of the manage bean and back beans, connect the java bean in the background, often doing the most in the performance layer is the page jump, through the manage bean to complete the page jump, if used The calling method is implemented by the configuration file settings, and the back bean is a Manage Bean, that is, the label and management bean are the same, but the back bean has to handle events, methods, can be said to be a business bean, Manage bean is just a simple attribute call, which does not need to be involved in any business. Summary is to complete: collect user input, present data, control page navigation, instead of user interaction with business logic.

The Spring framework is used in the business layer, which is the idea of ​​IOC. Since it is loosely coupled, it is very flexible, for example, in the DAO layer, we can use HQL or use SQL, which can be interchanged, and it is programmed to interface. Thought, each source file exposes an interface file, we have to modify the content of the actual source file, we can arbitrarily modify, as long as the interface is constant, or the same place in the same place, you don't have to pay any modifications inside. The third point is that the level is clear, and the low-headed high is the four layers of Model, DAO, Service, Web, and each layer is called, and the task of each layer is clear.

In the background uses the Hibernate mapped database into objects, the operation of the database is the operation of the object.

So the following one panel diagram is quite clear.

the second part

Implement test

achieve:

Page layer:

It is very convenient to use JSF. He is an extension of Struts. It is more detailed relative to the Struts processing page action. In Struts, the page is submitted to an ation process. In the JSF in the JSF, the value is input for each control and the value input for each control. The specified is bound to the control level, so that the processing of the action is more meticulous, so that the page programming can be as easy as WinForm programming, plus the plugin for JSF programming has been well supported, and it is a visualization, so it is complete You can use drag operation to implement page design. When writing page programs, we can use a good plug-in tool M7 drag page control, each page to set its value after generating, if the Text Control is to set the associated bean properties, if it is the Button control to set its event action The method of calling the bean when jumping, and its ID, this ID is the from-outcome of the jump page in the configuration file (value different from the value of the tag, which can determine the jump to different pages), this is the page Inside a major thing, if you use the Srping framework, you must specify the Spring framework in web.xml, and you want to specify the path to the most important configuration file ApplicationContext of the Spring framework. This web.xml is configured. It is very important because the database's related configuration and the call of the data service layer bean is in the configuration file in ApplicationContext.xml. If web.xml does not specify the path or not configured by ApplicationContext, then it is equal to the database, or no Using the Spring Framework, you don't recognize it, so web.xml is a very important configuration file. AplicationContext.xml file, if it is not a unit test, put it under Config, otherwise put it below Classes. Otherwise it will not recognize it. Business Layer:

According to the call relationship, it is divided into four layers from low to high, namely: Model, Dao, Service, Web.

The Model layer is a corresponding database class generated by Hibernate, a database map, which contains a class file and a profile of * .hbm.xml.

The DAO layer is directly operating on the data layer. If you use hibernate, you can use the HQL surface to query the language to directly manipulate the Model layer, don't care about the business layer, something, some methods, some of the parameters, only the full operation of the MODEL layer, And return to an object, the return value of the entire operation is placed inside the object. In particular, if the Spring framework is used, it is introduced into the Spring package, which can use the gethateTemplate () method. If you do not write a SEESION class separately, call the object seeion to call Hibernate.

The ServeICE layer operates the DAO layer, that is, you can use the HQL surface object language to direct object-oriented operation DAO, business logic is also written in this level, and return an object, all pages will be used in this layer, page The operating layer can be used directly.

The web layer is the page interaction and control layer, directly calls the business logic inside the Server layer, a large number of GET, SET methods Operating page control properties, and the page can be done directly to a wide variety of values.

Data layer:

You can generate a class file corresponding to the database with Hibernate tools. Inside the database name, a table name, field name, a table name, field name, and a configuration file * .hbm.xml, an example file is simple, and the full text is as follows:

Just specify the path, field name, length, type, etc. of the database. Then dealing with the database is equal to the dealing with this class.

Test:

There are too many codes, interested colleagues can ask me to discuss and discuss together.

the third part

Use experience and feelings

1. Page layer:

Due to good support, we can use visual IDE editing pages, convenient, visual degree, and connection background beans can also be implemented by this Ide. Very convenient. The page can be assembled and reused, a little OO's idea.

2. Business Layer:

a. Since Spring's dependency injection (IOC) is used, it is very convenient to configure it, just need to perform the basic configuration of the ApplicationCText.xml of the database and the DAO bean and Serve bean configured for database operations. Connections and settings on both sides, if you want to change your business or database, you can directly modify this profile, and then import new source files and databases.

b. The business is divided into 4 floors. The associations and relationships of each layer are clear, business operations and data processes are quite clear, and each layer is very convenient. Any layer can be modified and maintained separately, such as adding new The business is directly added to the business layer. If you want to change in the page control layer, you can only use the Modified Web layer, and the other layers are associated by the configuration file and class name, so as long as the configuration file is If the class name does not change, there will be no changes. If you want to connect a new file, modify the path of the configuration file and the file name, this is also dependent on injection ideas.

c. Programming to interface, if the specification writer, write an interface and a two files, the interface only has a way, and another file is implemented, and the implementation of the specific method is written. Just call the interface file, you can use the way, if there is any change in the method, we don't have to care, as long as the interface is still the interface, very good ideas, although very old, but it is very full.

3. Data layer:

The data layer uses Hibernate, just like Hibernate, map the database into an object, and the following operations are object-oriented, and after using the HQL language, I will have more deep, string hql = "SELECT Pass from user a where a. Name =: name and pass =: pass "; this HQL can see that the lookup is completely objective, and if you write the business yourself, there is this idea. I have not had this idea. For example," We want Querying an incoming parameter is the pass of the PASS in the database. "We have to complete this feature, we should write the following method in the business layer:

Public user getuser (String name) {

User bean = new user ();

Bean.setname (Name);

Return (user) UserDao.GetObjectBybean (bean);

}

Then write the following method in the Web layer

Public String showpass () {

User UserInfo = LoginService.getuser (Name);

Pass = userinfo.getpass ();

Return "Showpass";

}

It is seen that everything is to operate on the object. If we want to get the value of the PASSWORD field, we get this object's PASS attribute, and the business layer we put in the query result or get the result is also the whole, also That is to get everything in this object, and when we need it, what to take, what to take.

In general:

The advantage is that the layers are very fine, the level is clear, the code maintenance and management is convenient, the coupling is quite loose, and the decoupling is convenient for mutual calls between any layers. (Only need to modify the configuration file and pay attention to the file path and the file name ), The page is easy to write.

The disadvantages are immature JSF, and the use of unpredictable problems in the use of the use, there are not many documents and help, there is no good technical support.

Other considerations:

1. The unit test is placed under the CLASSES, otherwise placed under Config, so the web.xml ApplicationContext file path is modified.

2. Data source and connection pool JAR package: Commons-DBCP DBCP is an open source project of Apache is a data source and connection pool

3. DBCP Dependent Pack (Pool): DBCP is a data source, and DBCP requires Pool for this package as a connection pool.

4. Database JDBC three jar package: msbase.jar, mssqlserver.jar, msutil.jar

5. Three XML files are quite critical, it is web.xml, face-config.xml, AppLiactionContext.xml file, web.xml Specifies the path to use the Spring framework and AppLiactionContext.xml, Face-config.xml specified page jump, The page calls the path and specific items of the background bean, the AppLiactionContext.xml file configures the bean that connects the database and the operation database. These three XML files are very important and must be configured.

6. If you use the Spring framework, you can introduce the Spring package, you can use the gethibernateTemplate () method, if you do not write a SEESION class separately, call the Hibernate method by calling the object SEESION.

转载请注明原文地址:https://www.9cbs.com/read-129260.html

New Post(0)