標籤:map ext content cto false entity port esc classname
首先。我一定要吐槽下,這個Ant管理部署項目的工具。以及hibernate剛剛學習,匯入我這一個簡單的問題整了一天多。實在效率有點低下。
在這兩天中,①瞭解了Ant。知道了在Ant中非常靈活的步驟項目的情況,知道了build.xml檔案的一些基本寫法。②還學習了在MyEclipse這種整合工具中完畢項目部署。
③以及在這兩種情況中。利用hibernateTools中的hbm2java和hbm2ddl工具,依據對象關係對應檔,自己主動產生POJO以及SQL檔案(就是資料表)。
一,在web項目中,利用這兩個工具產生POJO和SQL
1。 項目的結構:
2,項目須要匯入的jar包:
①freemarker.jar一定要匯入進來。不然會報異常
② hibernate-tools.jar。提供hbm2java和hbm2ddl工具,利用這兩個工具,能夠通過對應檔產生POJO和sql。
③log4j.jar
④mysql-connector-java.jar.利用別的資料庫驅動也是能夠的。
⑤slf4j相關的三個jar包最好全匯入進來。相關的瞭解與學習,能夠自己查官方文檔。
⑥剩下的所有jar包,是hibernate中lib/required的所有jar。
3。hibernate初始化所用的配置
(設定檔方式,或者xml方式,兩者都能夠。可是通常選擇xml更通用更方面)
在本demo中,這兩個檔案都用啦。詳細看兩個檔案中面內容:
hibernate.properties(資料庫相關資訊)
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"><hibernate-configuration><session-factory><!-- 提示產生SessionFactory執行個體所要載入的對象關係對應檔 --><mapping resource="java/User.hbm.xml"/><!-- 特別提示,這裡是對應檔的全然限定名 --><mapping resource="java/Address.hbm.xml"/></session-factory></hibernate-configuration>
4,對象關係對應檔hbm.xml
hibernateTools中的兩個工具,正是參照這兩個檔案產生POJO和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"><hibernate-mapping package="java">
<!-- 特別注意這裡,這裡指示POJO對象所在的包,利用hbm2java產生POJO時,會在java檔案頭加一個package 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="128"/><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="128"/><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></hibernate-mapping>
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"><hibernate-mapping package="java"><class name="Address" table="address" dynamic-insert="false"dynamic-update="false"><id name="addressId" type="integer"><generator class="identity"/></id><property name="country" type="string" length="128"/><property name="city" type="string" length="128"/><property name="zipcode" type="string" length="12"/><many-to-one name="user" class="User" column="userId" cascade="none"outer-join="auto" update="true" insert="true" access="property"not-null="true"/></class></hibernate-mapping>
5,build.xml檔案
(這個檔案是ant指令碼,編寫hbm2ddl和hbm2java任務,能夠更靈活的的管理項目)
<?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" /><!-- 注意這裡源碼包位置指定的是根位置,原因以下 --><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"/> <!--類路徑--><path id="classpath"><pathelement location="${classes.dir}" /><fileset dir="${lib.dir}"><include name="**/*.jar" /></fileset></path><!--準備檔案夾--><target name="prepare"><mkdir dir="${classes.dir}" /></target><target name="prepare-shcema-dir"><mkdir dir="${schema.dir}" /></target><!--刪除檔案夾,用於又一次編譯--><target name="clean" description="刪除全部編譯後的檔案"><delete dir="${classes.dir}" /></target><!-- 編譯src.java中的源檔案 --><target name="compile.java" depends="prepare" description="編譯java源碼"><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><!-- 編譯全部的源檔案(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" /><hbm2java destdir="${src.java}"><!-- 特別注意:destdir指產生的源碼放在${src.java}檔案夾也就是src下,而為何不直接指定在src/java下呢?--><!--經測試,發現利用hbm2java通過對象關係對應檔產生POJO時,指定的package屬性會在POJO中加上檔案所在包,也就是package XXX;--><!--假設不指定這個package屬性時,則不會加上package XXX;可是事一定要把POJO放於某個包下,所以這裡的package一定要指定; --><!--也因此,在這裡src.java指定的值僅僅能是src根資料夾,不然指定的話會反覆了包,產生的POJO會在錯誤的相似AA/AA反覆層名的包下 --><configuration configurationfile="${classes.dir}/hibernate.cfg.xml" /><!--產生java類--><hbm2java /></hbm2java></target><!-- create ddl form *.hbm.xml --><target name="hbm2ddl" depends="prepare-shcema-dir" description="Generate DB schema from the O/R mapping files"><taskdef name="hbm2ddl" classname="org.hibernate.tool.ant.HibernateToolTask" classpathref="classpath" /><hbm2ddl destdir="${schema.dir}"><configuration configurationfile="${classes.dir}/hibernate.cfg.xml" /><!--export="true"建立資料庫schema export="false"產生sql檔案--><hbm2ddl export="true" console="true" create="true" update="false" drop="false" outputfilename="db.sql" /></hbm2ddl></target></project>
6。執行的過程圖
7,執行的結果圖:
如。POJO和SQL都已經成功產生。
------------------------------------------------------------------------------------------------------------------------------------------
在此特別感謝別的前輩的分享,從他的分享以及我的摸索中。讓我知道了怎樣在一個web項目中利用hibernateTools
的兩個工具產生須要的東西。當然這僅僅是入了門,提升還須要興許的學習與實踐
------------------------------------------------------------------------------------------------------------------------------------------
二,在java項目中,利用這兩個工具產生POJO和SQL
這個方面。前面一篇博文中已經轉載。
注意事項幾乎相同,僅僅是個別細節的配置須要注意。在此不反覆。
在MyEclipse的web項目/java項目中,使用Hibernate-tools中的hbm2java和hbm2ddl工具,依據hbm檔案自己主動產生pojo和資料庫指令碼