Xdoclet + ant proactively generates hibernate profiles

Source: Internet
Author: User

When using hibernate, too many hibernate profiles are a daunting issue.

Recently contacted the Xdoclet tool.

It is actually a tool for generating your own active code. Xdoclet cannot be executed alone and must be used with other tools, such as Ant.

Suppose Ant's Tools don't work. It is recommended to first add ant's Basic knowledge to learn Xdoclet.


What is Ant?

Apache Ant is a Java-based build tool. The Build tool is used in software development to convert the source code and other input files into the form of a executable file (which may also be converted to an installable product image).

As your application's build process becomes more complex. Make sure that the exact same build steps are used during each build. At the same time, realize as many self-initiatives as possible in order to produce consistent build numbers in a timely fashion.

Many of the other content of their own lessons to learn to use, not in the elaboration.


What is Xdoclet?

Xdoclet can through you in the Java source of some special gaze information, self-generated configuration files for you, source code and so on, such as the web, the deployment of EJB description file, for you to generate struts struts-config.xml configuration file, JavaScript checksum.


1. Download Xdoclet, unzip.

2. Define Xdoclet tasks in Ant's Build.xml


What we do here is to generate the hibernate mapping file based on the Hibernate entity class. So you have to have Hibernate's entity class first.


Person entity class:


<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" >package com.bjsxt.oa.model;/** * * @author Administrator * @hibernate. Class table= "T_person" */public class Person {/ * * @hibernate. ID * generator-class= "native" */private int id;/** * @hibernate. Property */private String name;/** * @hibe Rnate.property */private String sex;/** * @hibernate. Property */private String address;/** * @hibernate. Property */privat E string duty;/** * @hibernate. Property */private String phone;/** * @hibernate. Property */private String description;/** * @hibernate. Many-to-one */private orgnization org;public String getaddress () {return address;} public void setaddress (String address) {this.address = address;} Public String GetDescription () {return description;} public void SetDescription (String description) {this.description = description;} Public String Getduty () {return duty;} public void Setduty (String duty) {this.duty = duty;} public int getId () {return ID;} public void setId (int id) {THIS.Id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public String Getphone () {return phone;} public void Setphone (String phone) {this.phone = phone;} Public String Getsex () {return sex;} public void Setsex (String sex) {this.sex = sex;} Public Orgnization getorg () {return org;} public void setorg (orgnization org) {this.org = org;}} </span></span>

After adding the corresponding annotations on the entity class, define a new Xdoclet task in Ant.



<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" ><?xml version= "1.0" encoding= "UTF-8"?

><project name= "OA system build Script" default= "Generate Hibernate Profile" basedir= "." > <property name= "src.dir" value= "${basedir}/src"/> <property name= "Build.dir" value= "${basedir}/bin"/& Gt;<property name= "Webapp.dir" value= "${basedir}/src/webapp"/><property name= "Xdoclet.home" value= "D:\ xdoclet-plugins-1.0.3 "/> <!--Build classpath--<path id=" Xdoclet.task.classpath "> <fileset D Ir= "${xdoclet.home}/lib" > <include name= "**/*.jar"/> </fileset> <fileset dir= "${xdocle T.home}/plugins "> <include name=" **/*.jar "/> </fileset> </path><taskdef name=" Xdocl ET "classname=" org.xdoclet.ant.XDocletTask "classpathref=" Xdoclet.task.classpath "/><target name=" Generate Hibernate profile "><xdoclet><fileset dir=" ${src.dir}/com/bjsxt/oa/model "><include name=" **/*. Java "/></fileset><componentclassname=" Org.xdoclet.plugin.hibernate.HibernateConfigPlugin "destdir=" ${src.dir} "version= "3.0" hbm2ddlauto= "Update" jdbcurl= "Jdbc:mysql://127.0.0.1/oa" jdbcdriver= "Com.mysql.jdbc.Driver" Jdbcusername = "root" jdbcpassword= "123456" dialect= "Org.hibernate.dialect.MySQLDialect" showsql= "true"/></xdoclet> </target><target name= "Generate Hibernate mapping File" ><xdoclet><fileset dir= "${src.dir}/com/bjsxt/oa/ Model "><include name=" **/*.java "/></fileset><component classname=" Org.xdoclet.plugin.hibernate.HibernateMappingPlugin "version=" 3.0 "destdir=" ${src.dir} "/></xdoclet>< /target></project></span></span>


Click the ant plugin build plugin


The generated Person.hbm.xml


<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" ><?xml version= "1.0" encoding= "iso-8859-1"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://hibernate.sourceforge.net/ Hibernate-mapping-3.0.dtd ">

Hibernate.cfg.xml


<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" ><?xml version= "1.0" encoding= "iso-8859-1"? ><! DOCTYPE hibernate-configuration Public "-//hibernate/hibernate configuration DTD 3.0//en" "http// Hibernate.sourceforge.net/hibernate-configuration-3.0.dtd ">
can be seen in this article. having understood the hibernate core, we are able to use this tool to help us develop. Ant is a great helper for our Java developers by being easy-to-use in their own initiative to build and deploy Java programs, and not easily good and flexible .

Xdoclet + ant proactively generates hibernate profiles

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.