一、环境配置
path
c:\ruby\bin;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\system32\WBEM;C:\Program Files\Common Files\Ulead Systems\MPEG;C:\Nokia\Tools\Nokia_Developers_Suite_for_J2ME_3_0\bin;C:\Nokia\Update_Manager\bin;C:\Program Files\SecureCRT;%JAVA_HOME%\bin;C:\Python24;C:\ruby\bin;C:\ruby;C:\Python24\bin;C:\JavaServer\mysql-5.0.16-win32;C:\JavaServer\mysql-5.0.16-win32\bin;D:\Dev-Cpp\MinGW\bin;%ANT_HOME%\BIN;C:\JavaServer\MySQL Server 5.0\bin;;C:\Program Files\cvsnt;C:\PROGRA~1\ATT\Graphviz\bin;%MAVEN_HOME%\bin
MAVEN_HOME
C:\JavaServer\maven-2.0.3
TOMCAT_HOME
C:\JavaServer\apache-tomcat-5.5.14
RUBYOPT
rubygems rubygems rubygems
JAVA_HOME
C:\Program Files\Java\jdk1.5.0_02
CLASSPATH
C:\Program Files\Java\jdk1.5.0_02\lib\tools.jar;C:\Program Files\Java\jdk1.5.0_02\lib\dt.jar;C:\JavaServer\MySQLJDBC\mm.mysql-2.0.4-bin.jar;
ANT_HOME
g:\ant
二、eclipse编辑器:
1. 代码上下行移动:alt+arrow up alt+arrow down
2、行COPY:ctrl+alt+arrow up/down
3、字母大小写转换,选定代码:ctrl+shift+x/y
三、hibernate工具和插件
首先安装好JDK和ANT,然后下载相关的HIBERNATE库,以及要用到的JAVA jar文件;
HibernateTools-3.1.0.beta4.zip的使用小技巧
1、解压后把插件目录copy到ECLIPSE的安装目录下;
2、1、 创建cfg文件。Ctrl + N 打开新建窗口,创建一个hibernate configuration file(cfg.xml)
3、创建一个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">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/yxhservice2</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
</session-factory>
</hibernate-configuration>
4、在窗口-》打开透视图的其他选择hibernage console
在窗口-》显示透视图,选择hibernate configurations,新建一个配置;
可以通过Ctrl + N 带出如下窗体中选择Hibernate Console Configuration打开创建的界面。
在configuation file里找到第2部创建的文件,在mapping files添加相关数据库表的hbm.xml文件,这些文件就是你想创建JAVA文件的表MAP文件。
在classpath添加POJO和driver的jar 文件。这里只需加入mysql-connetor-java-x.x.x-bin.jar
5、在工具栏点击hibernate code generation...
在窗口里,点击NEW,新建一个配置。
main页,在console configuration里选择第4步创建的配置;
output directory里输入生成HBM.XML的POJO代码存放的路径;
在exporter页,选择GENERATE DOMAIN CODE,GENERATE DAO CODE,GENERATE MAPPINGS,等等。这些都设好以后你就可以通过 Run button来产生代码了。
6、使用Console
我们现在可以把刚才生成的java class和mapping文件加到console中来了。
打开Hibernate console透视图。
可以查询HQL,和显示hibernate entity model等等功能;
7、注意数据库兼容性;我用最新的MYSQL5就支持的不是很好。
Middlegen-Hibernate-r5的使用小技巧:
1、修改build.xml里面hibernate3.jar的路径
修改hibernate-extension 的TOOLS路径;
如下面配置;
<pathelement path="../hibernate-3.1/hibernate3.jar"/>
<fileset dir="../hibernate-3.1/lib">
<include name="*.jar"/>
</fileset>
<pathelement path="../hibernate-extensions-2.1.3/tools/hibernate-tools.jar"/>
<fileset dir="../hibernate-extensions-2.1.3/tools/lib">
<include name="*.jar"/>
</fileset>
2、定义生成xml文件的路径;
<property name="build.gen-src.dir" value="${build.dir}/gen-src"/>
3、定义使用的数据库配置,配置文件在config\database目录下:
4、如果使用hibernate3,那么生成的hbm.xml文件的内容需要作一下修改:
生成的hbm.xml文件头:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
修改为:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<!DOCTYPE project [
<!ENTITY database SYSTEM "file:./config/database/mysql.xml">
]>
附文件一:
build.xml文件内容:
<?xml version="1.0"?>
<!--
This is a testing platform for the middlegen hibernate plugin. It is a stripped
down version of the samples application. At the moment it is only concerned
with the generation of the mapping documents so that various tests etc can be
done on the generated package.
David Channon
The DOCTYPE declaration declares the location of product-specific parts of the
Ant build file. You can change the values of these declarations to point to
other available files. This gives you an opportunity to choose what database.
Note that this syntax has nothing to do with Ant or Middlegen. This
is the standard way defined by w3c to dynamically include external
files in an XML document. Any decent XML parser will do the include
transparently. Try to open this file in Internet Explorer and see
for yourself.
-->
<!DOCTYPE project [
<!ENTITY database SYSTEM "file:./config/database/mysql.xml">
]>
<project name="Middlegen Hibernate" default="all" basedir=".">
<!-- project name="Middlegen Hibernate" default="all" basedir="." -->
<property file="${basedir}/build.properties"/>
<property name="name" value="service2"/>
<!-- This was added because we were several people (in a course) deploying to same app server>
<property environment="env"/>
<property name="unique.name" value="${name}.${env.COMPUTERNAME}"/-->
<property name="gui" value="true"/>
<property name="unique.name" value="${name}"/>
<property name="appxml.src.file" value="${basedir}/src/application.xml"/>
<property name="lib.dir" value="${basedir}/lib"/>
<property name="src.dir" value="${basedir}/src"/>
<property name="java.src.dir" value="${src.dir}/java"/>
<property name="web.src.dir" value="${src.dir}/web"/>
<property name="build.dir" value="${basedir}/build"/>
<property name="build.java.dir" value="${build.dir}/java"/>
<property name="build.gen-src.dir" value="${build.dir}/gen-src"/>
<property name="build.classes.dir" value="${build.dir}/classes"/>
&database;
<!-- define the datasource.jndi.name in case the imported ejb file doesn't -->
<property name="datasource.jndi.name" value="${name}/datasource"/>
<path id="lib.class.path">
<pathelement path="${database.driver.classpath}"/>
<fileset dir="${lib.dir}">
<include name="*.jar"/>
</fileset>
<!-- The middlegen jars -->
<!--fileset dir="${basedir}/.."-->
<fileset dir="${basedir}/middlegen-lib">
<include name="*.jar"/>
</fileset>
<pathelement path="../hibernate-3.1/hibernate3.jar"/>
<fileset dir="../hibernate-3.1/lib">
<include name="*.jar"/>
</fileset>
<pathelement path="../hibernate-extensions-2.1.3/tools/hibernate-tools.jar"/>
<fileset dir="../hibernate-extensions-2.1.3/tools/lib">
<include name="*.jar"/>
</fileset>
</path>
<target name="init">
<available property="xdoclet1.2+" classname="xdoclet.modules.hibernate.HibernateDocletTask" classpathref="lib.class.path"/>
</target>
<!-- =================================================================== -->
<!-- Fails if XDoclet 1.2.x is not on classpath -->
<!-- =================================================================== -->
<target name="fail-if-no-xdoclet-1.2" unless="xdoclet1.2+">
<fail>
You must download several jar files before you can build Middlegen.
</fail>
</target>
<!-- =================================================================== -->
<!-- Create tables -->
<!-- =================================================================== -->
<target
name="create-tables"
depends="init,fail-if-no-xdoclet-1.2,check-driver-present,panic-if-driver-not-present"
description="Create tables"
>
<echo>Creating tables using URL ${database.url}</echo>
<sql
classpath="${database.driver.classpath}"
driver="${database.driver}"
url="${database.url}"
userid="${database.userid}"
password="${database.password}"
src="${database.script.file}"
print="true"
output="result.txt"
/>
</target>
<target name="check-driver-present">
<available file="${database.driver.file}" type="file" property="driver.present"/>
</target>
<target name="panic-if-driver-not-present" unless="driver.present">
<fail>
The JDBC driver you have specified by including one of the files in ${basedir}/config/database
doesn't exist. You have to download this driver separately and put it in ${database.driver.file}
Please make sure you're using a version that is equal or superior to the one we looked for.
If you name the driver jar file differently, please update the database.driver.file property
in the ${basedir}/config/database/xxx.xml file accordingly.
</fail>
</target>
<!-- =================================================================== -->
<!-- Run Middlegen -->
<!-- =================================================================== -->
<target
name="middlegen"
description="Run Middlegen"
unless="middlegen.skip"
depends="init,fail-if-no-xdoclet-1.2,check-driver-present,panic-if-driver-not-present"
>
<mkdir dir="${build.gen-src.dir}"/>
<echo message="Class path = ${basedir}"/>
<taskdef
name="middlegen"
classname="middlegen.MiddlegenTask"
classpathref="lib.class.path"
/>
<middlegen
appname="${name}"
prefsdir="${src.dir}"
gui="${gui}"
databaseurl="${database.url}"
initialContextFactory="${java.naming.factory.initial}"
providerURL="${java.naming.provider.url}"
datasourceJNDIName="${datasource.jndi.name}"
driver="${database.driver}"
username="${database.userid}"
password="${database.password}"
schema="${database.schema}"
catalog="${database.catalog}"
>
<!--
We can specify what tables we want Data generated for.
If none are specified, Data will be generated for all tables.
Comment out the <table> elements if you want to generate for all tables.
Also note that table names are CASE SENSITIVE for certain databases,
so on e.g. Oracle you should specify table names in upper case.
-->
<!--table generate="true" name="flights" pktable="flights_pk"/>
<table name="reservations"/-->
<!--
If you want m:n relations, they must be specified like this.
Note that tables declare in multiple locations must all have
the same value of the generate attribute.
-->
<!--many2many>
<tablea generate="true" name="persons"/>
<jointable name="reservations" generate="false"/>
<tableb generate="true" name="flights"/>
</many2many-->
<!-- Plugins - Only Hibernate Plugin has been included with this special distribution -->
<!--
If you want to generate XDoclet markup for hbm2java to include in the POJOs then
set genXDocletTags to true. Also, composite keys are generated as external classes which is
recommended. If you wish to keep them internal then set genIntergratedCompositeKeys to true.
Since r4 the ability to customise the selection of JavaTypes is now provided. The is a
recommended type mapper provided as shown. It is optional - if not provided then Middlegen
itself will select the Java mapping (as it did previously).
These settings are optional thus if they are not define here values default to false.
-->
<hibernate
destination="${build.gen-src.dir}"
package="com.yxh"
genXDocletTags="true"
genIntergratedCompositeKeys="false"
javaTypeMapper="middlegen.plugins.hibernate.HibernateJavaTypeMapper"
/>
</middlegen>
<mkdir dir="${build.classes.dir}"/>
</target>
<!-- =================================================================== -->
<!-- Compile business logic (hibernate) -->
<!-- =================================================================== -->
<target name="compile-hibernate" depends="middlegen" description="Compile hibernate Business Domain Model">
<javac
srcdir="${build.gen-src.dir}"
destdir="${build.classes.dir}"
classpathref="lib.class.path"
>
<include name="**/hibernate/**/*"/>
</javac>
</target>
<!-- =================================================================== -->
<!-- Run hbm2java depends="middlegen" -->
<!-- =================================================================== -->
<target name="hbm2java" description="Generate .java from .hbm files.">
<taskdef
name="hbm2java"
classname="net.sf.hibernate.tool.hbm2java.Hbm2JavaTask"
classpathref="lib.class.path"
/>
<hbm2java output="${build.gen-src.dir}">
<fileset dir="${build.gen-src.dir}">
<include name="**/*.hbm.xml"/>
</fileset>
</hbm2java>
</target>
<!-- =================================================================== -->
<!-- Build everything -->
<!-- =================================================================== -->
<target name="all" description="Build everything" depends="compile-hibernate"/>
<!-- =================================================================== -->
<!-- Clean everything -->
<!-- =================================================================== -->
<target name="clean" description="Clean all generated stuff">
<delete dir="${build.dir}"/>
</target>
<!-- =================================================================== -->
<!-- Brings up the hsqldb admin tool -->
<!-- =================================================================== -->
<target name="hsqldb-gui" description="Brings up the hsqldb admin tool">
<property name="database.urlparams" value="?user=${database.userid}&password=${database.password}"/>
<java
classname="org.hsqldb.util.DatabaseManager"
fork="yes"
classpath="${lib.dir}/hsqldb-1.7.1.jar;${database.driver.classpath}"
failonerror="true"
>
<arg value="-url"/>
<arg value="${database.url}${database.urlparams}"/>
<arg value="-driver"/>
<arg value="${database.driver}"/>
</java>
</target>
<!-- ==================================================================== -->
<!-- Validate the generated xml mapping documents -->
<!-- ==================================================================== -->
<target name="validate">
<xmlvalidate failonerror="no" lenient="no" warn="yes">
<fileset dir="${build.gen-src.dir}/airline/hibernate" includes="*.xml" />
</xmlvalidate>
</target>
</project>
附文件二
mysql.xml文件内容:
<!-- =================================================================== -->
<!-- ant properties/targets for mysql -->
<!-- note: this is not a proper xml file (there is no root element) -->
<!-- it is intended to be imported from a *real* xml file -->
<!-- =================================================================== -->
<property name="database.script.file" value="${src.dir}/sql/${name}-mysql.sql"/>
<property name="database.driver.file" value="${lib.dir}/mysql-connector-java-5.0.0-beta-bin.jar"/>
<property name="database.driver.classpath" value="${database.driver.file}"/>
<!-- property name="database.driver" value="org.gjt.mm.mysql.Driver"/ -->
<property name="database.driver" value="com.mysql.jdbc.Driver"/>
<property name="database.url" value="jdbc:mysql://localhost/yxhservice2"/>
<property name="database.userid" value="root"/>
<property name="database.password" value=""/>
<property name="database.schema" value=""/>
<property name="database.catalog" value=""/>
<property name="jboss.datasource.mapping" value="mySQL"/>
maven-2.0.3的使用技巧
1、下载maven,然后设置好系统环境变量和系统路径:
MAVEN_HOME
C:\JavaServer\maven-2.0.3
path
c:\ruby\bin;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\system32\WBEM;C:\Program Files\Common Files\Ulead Systems\MPEG;C:\Nokia\Tools\Nokia_Developers_Suite_for_J2ME_3_0\bin;C:\Nokia\Update_Manager\bin;C:\Program Files\SecureCRT;%JAVA_HOME%\bin;C:\Python24;C:\ruby\bin;C:\ruby;C:\Python24\bin;C:\JavaServer\mysql-5.0.16-win32;C:\JavaServer\mysql-5.0.16-win32\bin;D:\Dev-Cpp\MinGW\bin;%ANT_HOME%\BIN;C:\JavaServer\MySQL Server 5.0\bin;;C:\Program Files\cvsnt;C:\PROGRA~1\ATT\Graphviz\bin;%MAVEN_HOME%\bin
2、如果不修改也可以使用,如果需要个性化的设置,打开maven-2.0.3\conf下的setting.xml文件
修改本地库的存储路径:
<localRepository>C:\JavaServer\maven-2.0.3\Repository</localRepository>
3、在<profiles>节修改好路径:
<profile>
<id>tomcat-home</id>
<properties>
<tomcat.home>C:\JavaServer\apache-tomcat-5.5.14</tomcat.home>
</properties>
</profile>
4、在activeProfiles节增加
<activeProfile>tomcat-home</activeProfile>
5、设置其他的设置例如,proxy等等。