Spring + Hibernate Multi-Layer WEB Development Template under Eclipse

xiaoxiao2021-03-06  14

1. XDoclet generates an HBM configuration file and SQL statement.

2. XDoclets Generate Spring Profile ApplicationContext.

3. Container management transaction and resolve delay loading issues.

4. Resolve internationalization and Chinese issues.

5. Bo, DAO, Business Service, Controller, View (JSTL or JSP), a total of five-layer structure.

6. Form binding, form verification. It is possible to use BO to bind to BO, you can't be tied to BO, you have to do your Commut (similar to struts).

7. DAO test case design skills. DAO test case design should be achieved (I summarized the experience, whether it is still looking forward to:

(1) Independence: The data record used by the test is generated by the test program.

(2) Portability: can pass through CVS Check Out to another machine. - The company leaders run the test to see green and happy after the CHECK OUT in its machine.

(3) No intrusion on the database: The data record generated by the test program is finally deleted by yourself.

(4) Test completeness: Try to ensure that all methods are tested, and of course, it is a bit difficult, especially when it is involved in multi-table query, so I just said that I don't do my best.

The test case of the Service layer should also be designed, but sometimes the amount of data that needs to be initialized is too large. At the end, there are too many data to be deleted, I feel that it is not worth the loss, everyone can work.

8. Use the Hibernate map to solve the tree data structure - Cat.java, which also contains the writing format of the xdoclet tag that uses the Version optimistic lock.

9. Simplify the writing of DAOs with inheritance strategies. This is important, use inheritance, some DAO interfaces and interface implementations are empty.

-------------------------------- ^ _ ^ The following instructions are a bit chaotic, I have time to change. ^ _ ^ -------------------------------- Environmental Description: Spring Hibernate is a lightweight solution, in any J2EE The container can run. I use MySQL4 TOMCAT5, the configuration file is also for MySQL. In order to make the beginner can get started quickly, I recommend you use MySQL4 Tomcat5 so you don't have to modify the configuration! Project Description:

1. This project is a Hibernate Spring XDoclet's configuration template under Eclipse3 MyEClipse:

From bo, dao interface, dao implement, daoTest, bussiness interface, bussiness implement, bussiness test, command (similar to ActionForm), Controller (similar to the Action), Validator (similar to the struts validator), jsp & jstl view are included, own slow Look at it slowly. 2. Hibernate configuration file, SQL statement generates the use xdoclet, Spring bean configuration file is also generated with xdoclet, fully automated, cool! 3. This is a small pet management, the cat has a child cat, so it is an Ibernate Realize the data structure of the tree, very hibernate!

Operating instructions: Install Eclipse3 MyEclipse (MYECLIPSE for Eclipse3 version) first, build a project called PET in Eclipse, cover this project copy, refresh the project is absolutely OK!

1. Modify the hibernate.properties file under the root directory, I use mysql if you also use mysql, change username and password - the default configuration is username = root, password is empty, if you install mysql does not set up ROOT The password, then don't change anything.

2. Download spring, hibernate2.x, xdoclet2.1, ant, mysql_jdbc_driver (I use this driver: mysql-connection-java-3.0.14-production-bin.jar), put all JAR files under their lib directory all JAR files Entering the Webroot / Web-INF / LIB directory - the most troubles of beginners, which JAR is also required.

3. Run src / build.xml, then right click on the project PET, refresh, generate src / org / ggyy / bo / *. Hbm.xml file; Run SRC / Build.xml, refresh the project, generate SRC / SQL. DDL file (this is the problem under Eclipse3.0 myeclipse, can not be automatically refreshed, other IDE I have not tried, I only like Eclipse! ^ _ ^).

4. Run org.ggyy.util.databaseTask (this is a class I wrote), read the src / sql.ddl file, and then send SQL statements to the MySQL database (Test database built into the MySQL).

5. Run SpringBuild.xml (also Ant script, Eclipse can't be automatically refreshed, I simply separate each Target! This runs Ant, depressed ing), generates Spring's configuration file Webroot / Web-INF / ApplicationContext.xml. Additional bean Declare written in src / spring-beans.xml, merge it into APLICATIONCONTEXT.XML from Spring to AplicationContext.xml.

6. Published to Tomcat5, start Tomcat Server, Browser Type: http: // localhost: 8080 / pet / db / listcat.sf

Internationalization and Chinese Problem Solution:

1. Messages_zh_cn.properties file must be converted using the transfer tool Native 12ASCII.exe provided by JDK:

Native2ASCII messages_zh_cn.properties msg.txt The generated target file msg.txt copy paste the content inside the message_en_cn.properties so that it will not be garbled.

2. I am solved like this problem:

(1) SRC / SPRING-Beans.xml has the following statement (Spring-beans.xml actually merges to ApplicationContext.xml by Sprng XDCOLET):

Java code:

Class = "ORG.

Springframework.

JDBC.

DataSource.

DrivermanagerDataSource ">

COM.

Mysql.

JDBC.

Driver

jdbc: mysql:

// localhost: 3306 / test? Useunicode = true & characterencoding = GB2312

root

Here, use the TEST database in MySQL, please note the useUnicode = true & characterencoding = GB2312 in the URL. This is my mysql unique, and I have not tried other database environments, and friends who use other databases should pay attention. (2) Webroot / Web-INF / Web.xml has encoded Fillter declaration, also GB2312, look it over. (3) Each JSP header file, <% @ page contenttype = "text / html; charset = GB2312"%>, of course, GB2312 is also! Other languages ​​I have not tried it. With container management transactions to solve Hibernate latency loading issues:

1. Solving delayed loading in Spring is simple, just configure a transaction, there is a transaction context, there is no delay loading problem. This problem is solved with AOP to solve some, but unfortunately like this I won't be ^ _ ^.

2. The transaction of the container is only managed in the DAO layer and the service layer (I feel very varied in the Controller layer), because there is no Filtter using the OpenSessionInView, there is a problem that the VIEW layer is labeled. In order to avoid this phenomenon In advance, the service or DAO layer passes the data required by the View to the Controller layer, and then passed to the View layer by the Controller layer; at the same time, unless the Controller layer has explicitly loads the data from the table, don't Trying to get data from the table!

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

New Post(0)