今天复习hibernate的相关知识,突然感觉实体类很多,配置文件超级麻烦(为什么不用反转?因为现在有人不是从数据库层开始做设计,所以正向的配置也要熟悉),出于偷懒的心理,我找了找有没有什么方法可以解决这个问题!哈哈,被我找到了,使用ant和xdoclet生成即可。但是网络上的资源比较散,对于初学者有点难!现在我把我的详细做法记录如下,供大家参考!
准备:
1:下载ant,地址:http://ant.apache.org/bindownload.cgi 解压到任意位置
2:下载xdoclet插件 地址:http://xdoclet.codehaus.org/ 同样解压
下面开始配置ant
1:环境变量path的配置,添加ant的bin,我的是 E:\ant-1.8.4\bin 注意分号间隔(图1)。
2:环境变量ANT_HOME配置,新建变量名称ANT_HOME,值是ant的根目录,E:\ant-1.8.4(图2)。
3:如果java_home没有设置,那么也设置下,jdk的安装路径(图3)。
检验配置是否正确:dos命令行下,输入ant,提示找不到一个xml文件,那么就对了!其他的命令自行上度娘!
下面打开IDE,先前用的myeclipse8.6的有点问题!致使我纠结了半天!果断换了6.5的进行开发。
完工的工程目录结构(图4).
新建web项目,写好一个简单的实体类,我写的Student
package com.huadun.dto; /** * * @author huadunsoft.gaokai * 2013.03.07 */ public class Student { private String ID; private String Name; private String Sex; public Student(String id, String name, String sex) { super(); ID = id; Name = name; Sex = sex; } public Student() { super(); // TODO Auto-generated constructor stub } public String getID() { return ID; } public void setID(String id) { ID = id; } public String getName() { return Name; } public void setName(String name) { Name = name; } public String getSex() { return Sex; } public void setSex(String sex) { Sex = sex; } }
接下来右键工程名称Export----》ant buildfiles---》next---->finish-->ok。删除里面的代码,只留下这么多:
<?xml version="1.0" encoding="UTF-8"?> <!-- WARNING: Eclipse auto-generated file. Any modifications will be overwritten. To include a user specific buildfile here, simply create one in the same directory with the processing instruction <?eclipse.ant.import?> as the first entry and export the buildfile again. --> <project basedir="." default="build" name="Ant"> </project>
下面像其中添加必要的代码:
<?xml version="1.0" encoding="UTF-8"?> <!-- WARNING: Eclipse auto-generated file. Any modifications will be overwritten. To include a user specific buildfile here, simply create one in the same directory with the processing instruction <?eclipse.ant.import?> as the first entry and export the buildfile again. --> <project basedir="." default="buildhibernatecfgxml" name="Ant"> <property name="src.dir" value="${basedir}/src"/> <!--指定目录--> <property name="xdoclet.home" value="E:/ant-1.8.4/xdoclet103"/> <!--指定xdoclet插件的目录,注意分隔符用的是/--> <!-- Build classpath --> <!-- 路径定义 --> <path id="xdoclet.task.classpath"> <fileset dir="${xdoclet.home}/lib"> <include name="**/*.jar"/> </fileset> <fileset dir="${xdoclet.home}/plugins"> <include name="**/*.jar"/> </fileset> </path> <!--定义类的包路径--> <taskdef name="xdoclet" classname="org.xdoclet.ant.XDocletTask" classpathref="xdoclet.task.classpath" /> <!--生成hibernate.cfg.xml destdir指定文件的生成路径--> <target name="buildhibernatecfgxml"> <xdoclet> <fileset dir="${src.dir}/com/huadun/dto"> <include name="**/*.java"/> </fileset> <component classname="org.xdoclet.plugin.hibernate.HibernateConfigPlugin" destdir="${src.dir}" version="3.0" hbm2ddlauto="update" jdbcurl="jdbc:jtds:sqlserver://localhost:1433;instance=SQLDB;DatabaseName=test" jdbcdriver="net.sourceforge.jtds.jdbc.Driver" jdbcusername="sa" jdbcpassword="123456" dialect="org.hibernate.dialect.MySQLDialect" showsql="true" /> </xdoclet> </target> <target name="生成hibernate映射文件xxx.hbm.xml"> <xdoclet> <fileset dir="${src.dir}/com/huadun/dto"> <include name="**/*.java"/> </fileset> <component classname="org.xdoclet.plugin.hibernate.HibernateMappingPlugin" version="3.0" destdir="${src.dir}" /> </xdoclet> </target> </project>
修改实体类:为其添加注释 :
主要是一些标签
package com.huadun.dto; /** * * @author huadunsoft.gaokai * 2013.03.07 * @hibernate.class table="Student" */ public class Student { private String ID; private String Name; private String Sex; public Student(String id, String name, String sex) { super(); ID = id; Name = name; Sex = sex; } public Student() { super(); // TODO Auto-generated constructor stub } /** * @hibernate.id * generator-class="native" */ public String getID() { return ID; } public void setID(String id) { ID = id; } /** *@hibernate.property */ public String getName() { return Name; } public void setName(String name) { Name = name; } /** *@hibernate.property */ public String getSex() { return Sex; } public void setSex(String sex) { Sex = sex; } }
接下来我们右键工程---》属性---》myeclipse--》xdoclet---》add standard 选择standard hibernate ok即可,如图5,6
此时生成配置文件xdoclet-build.xml,里面的内容不解释。到这里可以算是结束了,接下来只要运行build.xml就可以生成了,注意运行时右键build.xml-->run as 选第二个,否则只生成默认的配置文件,至此完成。
我会是优秀的90后!