First of all. I'll have to spit it out. This ant manages the tools for deploying the project. As well as hibernate just learned, import me this one simple question whole day more. It's really inefficient.
During these two days, ① learned about Ant. Knowing the very flexible step project in ant, I know some basic wording of the Build.xml file. ② also learned to complete the project deployment in the MyEclipse integration tool.
③ and in both cases. By using the Hbm2java and HBM2DDL tools in Hibernatetools, the object-relational mapping file is used to generate Pojo and SQL files (which are data tables) on their own initiative.
One, in a Web project, use these two tools to generate Pojo and SQL
1. Structure of the project:
2, the project needs to import the jar package:
①Freemarker.jar Be sure to import in. Otherwise it will be reported abnormal
② Hibernate-tools.jar. Provides Hbm2java and HBM2DDL tools that enable you to generate Pojo and SQL from a mapping file.
③Log4j.jar
④Mysql-connector-java.jar. It is also possible to use other database drivers.
⑤slf4j related three jar packages are best to import all in. Relevant knowledge and learning, be able to check the official documents on their own.
All the remaining jar packages of ⑥ are all jars lib/required in hibernate.
3. Configuration used by Hibernate initialization
(Either the configuration file, or the XML way, is both possible.) But usually the choice of XML is more general and more common)
In this demo, these two files are used. See the contents of two files in detail:
Hibernate.properties (Database related information)
hibernate.dialect=org.hibernate.dialect.mysqldialecthibernate.connection.driver_class= Com.mysql.jdbc.driverhibernate.connection.url=jdbc:mysql://localhost:3306/testhibernate.connection.username= Roothibernate.connection.password=roothibernate.show_sql=truejavax.persistence.validation.mode=none
Hibernate.cfg.xml ()
<?xml version= ' 1.0 ' encoding= ' UTF-8 '? ><! DOCTYPE hibernate-configuration Public "-//hibernate/hibernate configuration DTD 3.0//en" "http// Hibernate.sourceforge.net/hibernate-configuration-3.0.dtd ">
4, Object relational mapping file Hbm.xml
The two tools in Hibernatetools are the two files that generate Pojo and SQL
User.hbm.xml
<?
XML version= "1.0"?
><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://hibernate.sourceforge.net/ Hibernate-mapping-3.0.dtd ">
<!--pay special attention here, this indicates the package where the Pojo object is located, and when generating Pojo with Hbm2java, it will be added to the Java file header with a pack XXX; --><class name= "user" table= "user" dynamic-insert= "true" dynamic-update= "true" ><id name= "UserId" type= " Integer "><generator class=" Identity "/></id><property name=" Account "type=" string "length=" 64 "/ ><property name= "Name" type= "string" length= "all"/><property name= "nickname" Type= "string" length= "128"/ ><property name= "status" type= "integer" length= "1"/><property name= "Phones" type= "string" length= "128"/ ><property name= "Emails" type= "string" length= "all"/><set name= "addresses" table= "Address" inverse= "true "Cascade=" All "sort=" unsorted "order-by=" zipcode ASC "><key column=" userId "/><one-to-many class=" Address "/></set></class>
Address.hbm.xml<?XML version= "1.0"?
><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://hibernate.sourceforge.net/ Hibernate-mapping-3.0.dtd ">
5,build.xml file(This file is an ant script that writes HBM2DDL and Hbm2java tasks, which allows for more flexible management of the project)
<?XML version= "1.0" encoding= "UTF-8"?
><project name= "Hibernate" default= "compile" basedir= "." ><echo>===========================================================</echo><echo>| Hibernate |</echo><echo>=========================================================== </echo><property name= "src" value= "src" ></property><property name= "Src.java" value= "src"/> <!--note Here The source package location specifies the root location, for the following reasons--><property name= "src.conf" value= "src/conf"/><property name= "Lib.dir" Value= "Webroot/web-inf/lib"/><property name= "Classes.dir" value= "webroot/web-inf/classes"/><property Name= "Schema.dir" value= "db"/> <!--classpath--><path id= "classpath" ><pathelement location= "${ Classes.dir} "/><fileset dir=" ${lib.dir} "><include name=" **/*.jar "/></fileset></path> <!--Prepare folder--><target name= "Prepare" ><mkdir dir= "${classes.dir}"/></target><target name= "Prepare-shcema-diR "><mkdir dir=" ${schema.dir} "/></target><!--Delete folder for compilation again--><target name=" clean " description= "Delete all compiled files" ><delete dir= "${classes.dir}"/></target><!--compile the source files in Src.java-->< Target Name= "Compile.java" depends= "Prepare" description= "compile Java source" ><javac srcdir= "${src.java}" destdir= "${ Classes.dir} "debug=" on "><classpath refid=" classpath "/></javac><copy todir=" ${classes.dir} "> <fileset dir= "${src.conf}" ><include name= "**/*"/></fileset><fileset dir= "${src.java}" > <exclude name= "**/*.java"/></fileset></copy></target><!--compile all source files (Src.java)-- <target name= "Compile" depends= "Compile.java"/><!--create. Java form *.hbm.xml--><target name= " Hbm2java "depends=" Compile "description=" Generate Java source from the O/R mapping Files "><taskdef name=" Hbm2java " Classname= "Org.hibernate.tool.ant.HibernateToolTask" classpathref= "Classpath"/>
6. Process diagram for execution
7, the result of the execution diagram:
Such as. Both Pojo and SQL have been successfully generated.
--------------------------------------------------------------------------------------------------------------- ---------------------------
In this special thanks to other predecessors of the sharing, from his share and my groping. Let me know how to use Hibernatetools in a Web project
Two tools to generate what is needed. Of course, this is only into the door, Ascension also need to be perhaps learning and practice
--------------------------------------------------------------------------------------------------------------- ---------------------------
Second, in a Java project, use these two tools to generate Pojo and SQL
This aspect. The previous blog post has been reproduced.
The notes are almost the same, and only the configuration of individual details needs attention. Not repeated here.
In the MyEclipse Web Project/java project, use the Hbm2java and HBM2DDL tools in Hibernate-tools to proactively generate Pojo and database scripts based on HBM files