I vomit blood dedication, including:
1. XDoclet generates an HBM configuration file and SQL statement. 2. XDoclet generates Spring profile ApplicationContext. 3. Container management transaction and resolves latency load 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: via CVS Check Out can also pass. - 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. The project is the configuration template under eclipse3 myeclipse a hibernate spring xdoclet: 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 struts Validator), JSP & JSTL VIEW is included, 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 a problem under Eclipse3.0 myeclipse, can not be automatically refreshed, I didn't try it again, I only like Eclipse! ^ _ ^). 4. Run org.ggyy.util.DatabaseTask (this is one I wrote Class), read src / sql.ddl files, then send SQL statements in the mysql database (using a MySQL built-in Test database). 5. Run SpringBuild.xml (also Ant script, Eclipse can not be automatically refreshed, I simply put each Target is separated! This runs Ant, depressed ing. In the XML - I will understand it yourself. 6. Published to Tomcat5, start the Tomcat server, browser type: http: // localhost: 8080 / pet / db / listcat.sf
Internationalization and Chinese Problem Solution:
1. Messages_zh_cn.properties file must use the transfer tool native2ascii.exe provided by JDK: native2ascii messages_zh_cn.properties msg.txt Paste the generated target file msg.txt copy to replace the content inside Messages_n_cn.properties, this will not I have been garbled. 2. I am solved like this: (1) src / spring-beans.xml has the following statement (Spring-beans.xml actually merges to ApplicationContext.xml by Sprng XDCOLET): Java code :
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! I haven't tried it in other languages.
Use container management to resolve Hibernate delay 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 Interceptor I will not ^ _ ^. 2. The transaction management 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 OpenSessionInview, there is a delay in the View layer Loading problems. In order to avoid this phenomenon, pass the data required by the View layer in advance to the Controller layer in advance, and then pass it by the Controller layer to the View layer; at the same time, unless the Controller layer has explicitly put from the table Data load, otherwise do not try to get data from the table class at the View layer!
Use the base class to implement DAO description BO package: entity.java. All BO class base classes, only one attribute ID, so you can unify the generated policy of the primary key.
Java code:
Package org.ggyy.bo; public class entity {private long id = -1;
/ ** * @ hibernate.id * generator-class = "native" * unsaved-value = "- 1" * / public long getId () {return id;} public void setId (long i) {id = i;} Public Boolean Equals (Object Arg0) {Return this.GetId () == ((entity) arg0) .GETID ();}}
An example of Cat.java. Bo, derived from Entity. Use the locked of the Version.
Java code:
Package org.ggyy.bo;
/ ** * Example of tree data structure: Totte, with Parent and Children, and map to the same field: fk_parent_id * /
/ ** * @ hibernate.class table = "tbl_cat" dynamic-update = "true" dynamic-insert = "true" * optimistic-lock = "version" * / public class Cat extends Entity {private Set children = new HashSet ( PRIVATE CAT PARENT; Private Owner; Private String Name; Private Integer Version;
/ ** * @ hibernate.many-to-one * column = "fk_owner_id" * class = "org.ggyy.bo.owner" * cascade = "save-update" * / public output {Return Owner;} Public void setowner (owner owner) {this.owner = Owner;}
/ ** * @ hibernate.version * / public integer getversion () {Return Version;} public void setVersion (integer version) {this.version = version;
/ ** * @ hibernate.set * cascade = "all" * inverse = "true" * lazy = "true" * @ hibernate.collection-key * column = "fk_parent_id" * @ hibernate.collection-one-to-many * Class = "org.ggyy.bo.cat" * / public set getchildren () {return children;} public void setchildren (set children) {this.children = children;} / ** * @ hibernate.many-to- One * column = "fk_parent_id" * class = "org.ggyy.bo.cat" * cascade = "save-update" * / public cat getparent () {Return Parent;} public void setParent (cat parent) {this.parent = Parent;}
/ ** * @ hibernate.property * / public string getName () {return name;} public void setname (String name) {this.name = name;}}
DAO Package: IEentityDao.java. This DAO interface is the father of all DAO interfaces. Other DAO interfaces are not written by inheriting this interface, increasing, deleting, modifying, and LOAD, just pay attention to your own Finder (or Filtter is OK. Increase and modify the operation as a method: public void store (entity entity). It actually calls SaveorUpdate method. You can also join more common methods, so you have to see your project needs and your own understanding and abstract ability of the system built.
Java code:
package org.ggyy.dao; public interface IEntityDao {public Entity load (String id) throws DataAccessException; public void store (Entity entity) throws DataAccessException; public void delete (String id) throws DataAccessException; public void delete (Entity entity) throws DataAccessException }
Icatdao.java. This DAO only needs little way because the base class has already increased the way to delete Load.
Java code:
Package org.ggyy.dao; public interface ictdao extends ititydao {
/ ** * tree traversal * / public void Catid) THROWS DATAACCESSEXCEPTION;
/ ** * Find the direct child node and use HQL directly. * / Public List FindDirectChildren (String Catid) THROWS DATAACCESSEXCEPTION; PUBLIC LIST FINDROTCATS () THROWS DATAACCESSEXCEPTION;
Ivisit.java traversal interface, look at the FindAllChildren in CaodaoImpl, know how to use this interface.
Java code:
Package org.ggyy.da; public interface ivisit {public void visit (cat c);}
DAO implementation: EntityDaoImpl.java. This is an abstract class. Since the Class of the BO is not known in advance, it is subsequently implemented by each DAO implementation.
Java code:
Abstract public class entitydaoimpl extends hibernatedaosupport implements ititydao {
/ ** * Abstract method is the realization of the remaining subsystem. * / Abstract protected Class getEntityClass (); public Entity load (String id) throws DataAccessException {return (Entity) this.getHibernateTemplate () load (this.getEntityClass (), new Long (id));.} Public void store (Entity entity) throws DataAccessException {this.getHibernateTemplate () saveOrUpdate (entity);.} public void delete (Entity entity) throws DataAccessException {this.getHibernateTemplate () delete (entity);.} public void delete (String id) throws DataAccessException {Entity Entity = (Entity) this.GethibernateTemplate (). Load (this.geeTETETETEMPLATE (); delete (entity);}} (). delete (entity);}
CATDaoImpl.java. Inherited in EntityDaoImpl. I have configured a readonly transaction for Treevisitcat and FindAllChildren. It is not because this method requires transactions, but to solve the problem of latency. In this system, all pairs Many are cavescade = "all" inverse = "true" lazy = "true"; all the many pairs of all-in-one is cascade = "save-update". The main table class is not responsible for loading from the table class, is not responsible for maintenance The main relationship, this is the best. But due to lazy = "true", the following method's Cat.ITerator () is running down (because the problem is loaded). But give It is different, there is a transaction, there is a transaction context (I don't know if it is called like this), there is a HibernateSession context, so there will be no delayed load. This is a bit shameless, in order to make up for performance Loss, I had to make transaction into readonly. For this Query, using OpenSessionInView is line, but this hibernatesis is not good. I think, the best way is to use AOP to configure a HibernateSession context, which is to use AOP To intercept, but unfortunately I won't ^ _ ^. Java code:
/ ** * @ Spring.bean id = "catdaotarget" * @ Spring.property name = "sessionFactory" ref = "sessionFactory" * / public class catdaoimpl extends entitydaoImpl imports ictdao {
/ ** * Requires readonly transaction, in order to solve the problem of Hibernate lazy loading * / public List findAllChildren (String catId) throws DataAccessException {final List children = new ArrayList ();. This.treeVisitCat (catId, new IVisit () { Public void visit (Cat C) {Children.Add (C);}});}});}
/ ** * The transaction that needs Readonly is to solve the latency of Hibernate. * / Public void Treevisitcat (String Catid, Final Ivisit Visit) THROWS DATAACCESSEXCEPTION {cat Catt = (CAT) gethateTemplate () .load (Cat.class, New long (catid)); stack s = new stack (); s.push (catt); while (s.empty () == false {cat c = (cat) s.POP (); visit.visit ( c); set children = c.getchildren (); if (children! = null&&! children.isempty ()) {ipi = children.iterator (); while (ci.hasnext ()) {cat cc = (Cat ) ci.next (); s.push (cc);}}}} protected Class getEntityClass () {return Cat.class;} public List findDirectChildren (String catId) throws DataAccessException {return this.getHibernateTemplate () find ( ". Select Cat from cat as cat where cat.parent .id =? ", new long (catid));} public list findrootcats () throws DataAccessExcection {Return this.GethibernateTemplate (). Find (" Select Cat from cat as cat where cat.parent = null ");}}}}}}} .java and OwnerDaoImpl.java can see that they are basically equal to empty, OwnerDaoImpl only one way: protected class getentityclass () {return {returnal.class;} Many methods of subclasses have many methods Can save.
Java code:
Package org.ggyy.da;
/ ** * @Author jiangyubao * Ownerdao interface * / public interface iownerdao extends ositiTYDAO {}
Java code:
Package org.ggyy.dao.hibernate; import org.ggyy.bo.owner; import org.ggyy.dao.iownerdao;
/ ** OwnerDao achieve a * @author jiangyubao * @ spring.bean id = "ownerDao" * @ spring.property name = "sessionFactory" ref = "sessionFactory" * / public class OwnerDaoImpl extends EntityDaoImpl implements IOwnerDao {protected Class getEntityClass () {RETURN OWNER.CLASS;}} PS: Question: Thank you for the sharing of the landlord, can you give more detailed ApplicationContext.xml file content? Answer: This file is actually using Ant to run the SRC / SpringBuild.xml file. Run Method: Select SRC / SPRINGBUILD.XML, right button, select Run-> Ant Build
Java code:
XML Version = "1.0" encoding = "UTF-8"?> SessionFactory "/> property> bean> Property>