自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

使用Ant自動(dòng)化發(fā)開(kāi)Hibernate

開(kāi)發(fā) 后端 自動(dòng)化
我曾經(jīng)有一段時(shí)間見(jiàn)到代碼文件就有想吐的沖動(dòng),不過(guò)還好我還是挺過(guò)來(lái),嘛也就是像愛(ài)情麻木期一樣,怎么都無(wú)所謂了。直到最近了解到Ant這個(gè)工具,讓我看到了另一種發(fā)開(kāi)Hibernate應(yīng)用程序的方式,并不是說(shuō)這樣的開(kāi)發(fā)方式就一定好于純手工編寫,適合自己才是最好的,但我是一個(gè)比較愛(ài)偷懶的,自動(dòng)化構(gòu)建非常的吸引我。

在開(kāi)發(fā)使用Hibernate框架應(yīng)用程序中,無(wú)外乎要?jiǎng)?chuàng)建hibernate.cfg.xml,xxx.hbm.xml,pojos和databaseSchema這些文件,框架這東西剛開(kāi)始學(xué)會(huì)的時(shí)候,都是心情舒暢,熱情高漲,所有文件都是自己手寫不管這樣重復(fù)性的工作有多么的乏味,愛(ài)情都會(huì)由低潮期更別說(shuō)重復(fù)Coding了,久而久之在編寫一個(gè)個(gè)xml、pojo和schema的過(guò)程中,不由從心中產(chǎn)生一種不舒暢之氣。

我曾經(jīng)有一段時(shí)間見(jiàn)到代碼文件就有想吐的沖動(dòng),不過(guò)還好我還是挺過(guò)來(lái),嘛也就是像愛(ài)情麻木期一樣,怎么都無(wú)所謂了。直到最近了解到Ant這個(gè)工具,讓我看到了另一種發(fā)開(kāi)Hibernate應(yīng)用程序的方式,并不是說(shuō)這樣的開(kāi)發(fā)方式就一定好于純手工編寫,適合自己才是***的,但我是一個(gè)比較愛(ài)偷懶的,自動(dòng)化構(gòu)建非常的吸引我。開(kāi)頭字?jǐn)?shù)湊夠了就進(jìn)入正題了,首先直接上build.xml,這個(gè)文件是ant所需要的。

  1. <?xml version="1.0" encoding="UTF-8"?> 
  2.  
  3. <project name="hibernatetools" basedir="." default="useHibernateTool"> 
  4.       
  5.     <!-- 導(dǎo)入build.properties文件,該文件定義一些常量,這些常量也可以是用<property name="" value="">的形式定義 --> 
  6.     <property file="build.properties"></property> 
  7.       
  8.     <!-- 定義依賴包所在的位置,"${reference_libs_dir}"表示引用名為"reference_libs_dir"的常量 --> 
  9.     <path id="referencelib"> 
  10.         <fileset dir="${reference_libs_dir}"> 
  11.             <!-- 匹配模式,表示該目錄下所有jar文件 --> 
  12.             <include name="**/*.jar"/> 
  13.         </fileset> 
  14.     </path> 
  15.       
  16.     <!-- 自定義Ant Task,但是使用的自定義類為Hibernate為我們提供的 --> 
  17.     <taskdef name="hibernatetool" classname="org.hibernate.tool.ant.HibernateToolTask" classpathref="referencelib"></taskdef> 
  18.       
  19.     <!-- 使用hibernatetool --> 
  20.     <target name="useHibernateTool"> 
  21.         <hibernatetool> 
  22.             <!-- 指定資源搜索位置 --> 
  23.             <classpath> 
  24.                 <path location="${source_folder_dir}"></path> 
  25.                 <path location="bin"></path> 
  26.             </classpath> 
  27.               
  28.             <!-- 指定Hibernate的配置文件 --> 
  29.             <configuration configurationfile="${source_folder_dir}/hibernate.cfg.xml"/> 
  30.               
  31.             <!-- 根據(jù)hbm生成java代碼 --> 
  32.             <hbm2java jdk5="true" destdir="${source_folder_dir}"/> 
  33.             <!-- 根據(jù)hbm生成databaseSchema --> 
  34.             <hbm2ddl destdir="${database_schema_dir}" export="true" outputfilename="sqlStatement.sql" format="true"/> 
  35.         </hibernatetool> 
  36.     </target> 
  37. </project> 

build.properties文件:

  1. source_folder_dir=src  
  2. reference_libs_dir=referencelibs  
  3. database_schema_dir=dbschema 

這是項(xiàng)目最開(kāi)始的狀態(tài):

此時(shí)項(xiàng)目中僅僅只是定義了cfg和hbm文件,數(shù)據(jù)庫(kù)schema和pojo都沒(méi)有定義,"referencelibs"中存放的是該程序要成功需要的依賴庫(kù)文件。運(yùn)行Ant,此時(shí)項(xiàng)目的狀態(tài):

可以看到,我們的需要的pojo類和schema都生成好了,其實(shí)按照我的build.xml設(shè)置,數(shù)據(jù)庫(kù)已經(jīng)直接設(shè)置到了兩張表了。驗(yàn)證器正確性:

Customer.hbm.xml:

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
  3.     <hibernate-mapping> 
  4.         <class name="com.beliefbetrayal.hibernate.pojos.Customer" table="customer"> 
  5.             <!-- 主鍵設(shè)置 --> 
  6.             <id name="id" type="string"> 
  7.                 <column name="id"></column> 
  8.                 <generator class="uuid"></generator> 
  9.             </id> 
  10.             <!-- 屬性設(shè)置 --> 
  11.             <property name="username" column="username" type="string"></property> 
  12.             <property name="balance" column="balance" type="integer"></property> 
  13.               
  14.             <set name="orders" inverse="true" cascade="all"> 
  15.                 <key column="customer_id" ></key> 
  16.                 <one-to-many class="com.beliefbetrayal.hibernate.pojos.Order"/> 
  17.             </set> 
  18.         </class> 
  19.     </hibernate-mapping> 

Customer.java

  1. package com.beliefbetrayal.hibernate.pojos;  
  2. // Generated 2012-2-8 1:10:59 by Hibernate Tools 3.3.0.GA  
  3.  
  4. import java.util.HashSet;  
  5. import java.util.Set;  
  6.  
  7. /**  
  8.  * Customer generated by hbm2java  
  9.  */ 
  10. public class Customer  implements java.io.Serializable {  
  11.  
  12.      private String id;  
  13.      private String username;  
  14.      private Integer balance;  
  15.      private Set<Order> orders = new HashSet<Order>(0);  
  16.  
  17.     public Customer() {  
  18.     }  
  19.  
  20.     public Customer(String username, Integer balance, Set<Order> orders) {  
  21.        this.username = username;  
  22.        this.balance = balance;  
  23.        this.orders = orders;  
  24.     }  
  25.      
  26.     public String getId() {  
  27.         return this.id;  
  28.     }  
  29.       
  30.     public void setId(String id) {  
  31.         this.id = id;  
  32.     }  
  33.     public String getUsername() {  
  34.         return this.username;  
  35.     }  
  36.       
  37.     public void setUsername(String username) {  
  38.         this.username = username;  
  39.     }  
  40.     public Integer getBalance() {  
  41.         return this.balance;  
  42.     }  
  43.       
  44.     public void setBalance(Integer balance) {  
  45.         this.balance = balance;  
  46.     }  
  47.     public Set<Order> getOrders() {  
  48.         return this.orders;  
  49.     }  
  50.       
  51.     public void setOrders(Set<Order> orders) {  
  52.         this.orders = orders;  
  53.     }  

可以看到,我們借助Ant生成的POJO完全沒(méi)問(wèn)題。看看數(shù)據(jù)庫(kù)Schema:

  1. create table customer (  
  2.         id varchar(255) not null,  
  3.         username varchar(255),  
  4.         balance integer,  
  5.         primary key (id)  
  6.     );  
  7.  
  8.     create table orders (  
  9.         id varchar(255) not null,  
  10.         orderNumber varchar(255),  
  11.         cost integer,  
  12.         customer_id varchar(255),  
  13.         primary key (id)  
  14.     );  
  15.  
  16.     alter table orders   
  17.         add index FKC3DF62E51E0069B1 (customer_id),   
  18.         add constraint FKC3DF62E51E0069B1   
  19.         foreign key (customer_id)   
  20.         references customer (id); 

使用Ant自動(dòng)化開(kāi)發(fā)Hibernate需要hibernate-tools.jar包。我將項(xiàng)目整理了一下可以下載參考項(xiàng)目下載。

原文鏈接:http://www.cnblogs.com/beliefbetrayal/archive/2012/02/08/2342137.html

【編輯推薦】

  1. 精通Hibernate:通過(guò)Hibernate操縱對(duì)象
  2. 精通Hibernate:映射對(duì)象標(biāo)識(shí)符
  3. 精通Hibernate:映射一對(duì)多關(guān)聯(lián)關(guān)系
  4. 精通Hibernate:對(duì)象關(guān)系映射基礎(chǔ)
  5. 精通Hibernate:***個(gè)Hibernate應(yīng)用
責(zé)任編輯:林師授 來(lái)源: 信仰や欺騙的博客
相關(guān)推薦

2017-12-17 21:58:18

2022-03-30 09:43:19

jscodeshif自動(dòng)化重構(gòu)開(kāi)發(fā)

2024-11-21 15:24:49

2021-04-19 14:00:03

ExchangelibPython郵箱自動(dòng)化管理

2022-11-15 17:07:40

開(kāi)發(fā)自動(dòng)化前端

2009-12-15 17:43:04

Ruby自動(dòng)化驅(qū)動(dòng)

2024-09-13 15:32:18

2024-01-24 18:50:21

WebFTP服務(wù)器

2017-12-06 18:16:58

自動(dòng)化企業(yè)信息化

2018-07-13 06:46:35

數(shù)據(jù)中心自動(dòng)化微服務(wù)

2021-04-17 23:10:59

Python微軟Word

2018-12-03 08:46:36

Web瀏覽器SeleniumPython

2021-09-30 09:00:00

漏洞安全工具

2021-06-28 06:32:46

Tekton Kubernetes Clone

2025-02-06 14:59:08

2018-02-25 19:29:49

自動(dòng)化數(shù)字化IT

2021-10-13 10:06:49

自動(dòng)化IT安全

2010-12-06 09:59:58

2022-02-04 21:50:37

網(wǎng)絡(luò)安全自動(dòng)化

2020-04-29 11:28:54

智能自動(dòng)化機(jī)器人流程自動(dòng)化AI
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)