配置數據庫映射文件hbm.xml

在數據庫映射方面,guzz支持相似hibernate的領域對象class-table映射。也支持ibatis相似的在guzz.xml中映射。java

hibernate的hbm.xml映射

guzz兼容hibernate的配置文件(xxx.hbm.xml),你可使用hibernate映射工具生成或維護這些映射,guzz將能夠直接解析。
使用hibernate映射文件時,某些guzz的特性在hibernate mapping dtd中沒有定義,文本編輯時不能自動提示,甚至會報錯,但不影響使用。

配置領域對象到系統中

對於hbm.xml配置的領域對象,每一個對象在guzz.xml中增長一個business來聲明:git

1 <business name="user" dbgroup="default" file="classpath:org/guzz/test/User.hbm.xml" />

每一個business標籤對應1個領域對象。business擁有5個屬性:sql

 

屬性名 Required 做用
name Required business name。
file Required hbm.xml文件位置
dbgroup Optional 對象存儲到的數據庫組。默認爲default。
class Optional 若是填寫,覆蓋hbm.xml中定義的域對象class
interpret Optional 自定義查詢條件解析器。

 

一個典型的配置以下:數據庫

1 <business name="user" dbgroup="mainDB" class="org.guzz.test.User" interpret="" file="classpath:org/guzz/test/User.hbm.xml" />
2 <business name="book" class="org.guzz.test.Book" file="classpath:org/guzz/test/Book.hbm.xml" />

其中,每一個領域對象的定義能夠在guzz測試源代碼中找到。緩存

guzz本身的映射定義

將hibernate hbm.xml的dtd定義頭,改爲
1 <!DOCTYPE guzz-mapping PUBLIC "-//GUZZ//GUZZ MAPPING DTD//EN" "http://www.guzz.org/dtd/guzz-mapping.dtd">
既可。餘下內容與hibernate相似。
mapping xml的root能夠是 <guzz-mapping>,也能夠是 <hibernate-mapping>(便於使用成熟的hibernate工具)。guzz在解析時只從 <class>處開始讀取,root元素如何定義可有可無。

guzz mapping相對hibernate mapping也有一些變化。oracle

class定義增長了3屬性:app

businessName:默認的business name。域對象在guzz.xml中聲明時能夠覆蓋此設置。【guzz1.3.1新特性】

 

dbGroup:對象默認存儲在的數據庫組。域對象在guzz.xml中聲明時能夠覆蓋此設置。【guzz1.3.1新特性】
shadow:用於設置表分切和自定義表,使用方法在後面有詳細的介紹。

 

property定義增長了2個屬性:dom

null: 指定當數據庫值爲空時,字段取何值。默認爲java定義此類型的默認值。這個屬性通常在數據庫升級字段時使用,避免hibernate惱人的cglib....get..set錯誤。
loader: 指定字段的特殊讀取類。經過loader能夠定義某些字段的特殊讀取策略,如從文件加載,或先讀緩存再讀數據庫,或從第三方系統讀取等。詳細介紹請參看:LazyLoad

type屬性支持定義日期格式:ide

type用於指定數據類型,如:int, string等。對於日期類型數據,通常有三種:datetime表示時間戳(日期+時間),date表示日期,time表示時間。對於這三種類型,在定義時能夠在類型後加"|"線,並跟上日期格式(參看DateFormat定義)完成格式定義。
例如:type="datetime|YYYY-MM-dd HH:mm:SS",type="date|MM-dd"。
傳入日期格式後,進行sql查詢時,對於日期字段,能夠傳入按照格式生成的日期字符串直接查詢,guzz將會自動按照格式轉成須要的數據類型。這種特性,在按照用戶輸入日期進行查詢或向guzz taglib傳入日期參數時,會比較方便。

枚舉類型支持:工具

guzz支持按照ordinal和value兩種方式映射枚舉類型。對於ordinal方式,數據庫字段須要定義爲int整數類型;對於value方式,數據庫字段須要定義爲能夠保存字符串的varchar等類型。

 

按照ordinal映射枚舉定義方式:type="enum.ordinal|枚舉類完整類名",如:type="enum.ordinal|com.company.EmployeeStatus"。
按照value映射枚舉定義方式:type="enum.string|枚舉類完整類名",如:type="enum.string|com.company.EmployeeStatus"。

主鍵生成(id generator)支持變化:

 

Generator hibernate guzz Description for guzz
increment × It generates identifiers of type long, short or int that are unique only when no other process is inserting data into the same table. It should not the used in the clustered environment.
identity It supports identity columns in DB2, MySQL, MS SQL Server, Sybase and HypersonicSQL. The returned identifier is of type long, short or int.
sequence The sequence generator uses a sequence in DB2, PostgreSQL, Oracle, SAP DB, McKoi or a generator in Interbase. The returned identifier is of type long, short or int
hilo The hilo generator uses a hi/lo algorithm to efficiently generate identifiers of type long, short or int, given a table and column (by default guzz_unique_key and next_hi respectively) as a source of hi values. The hi/lo algorithm generates identifiers that are unique only for a particular database. Do not use this generator with connections enlisted with JTA or with a user-supplied connection. 支持的參數:table, column, db_group, max_lo
seqhilo The seqhilo generator uses a hi/lo algorithm to efficiently generate identifiers of type long, short or int, given a named database sequence.
uuid The uuid generator uses a 128-bit UUID algorithm to generate identifiers of type string, unique within a network (the IP address is used). The UUID is encoded as a string of hexadecimal digits of length 32.
guid It uses a database-generated GUID string on MS SQL Server and MySQL.
native It picks identity, sequence or hilo depending upon the capabilities of the underlying database. Take it as an alias of the real identity generator used.
assigned lets the application to assign an identifier to the object before save() is called. This is the default strategy if no <generator> element is specified.
select × retrieves a primary key assigned by a database trigger by selecting the row by some unique key and retrieving the primary key value.
foreign × uses the identifier of another associated object. Usually used in conjunction with a <one-to-one> primary key association.
silent × The silent generator does nothing. Usually used when you need to manage the id in the database, for example with a trigger.
random × RandomIdGenerator that returns a string of length 32 (or the length you given in parameter:length). This string will consist of only a-z and 0-9, and is unable to predicate. 
Note: the length maybe a little shorter than the given length. 
Mapping parameters supported: length
hilo.multi × Works as hilo, but allows many Ids stored in a single table disguised by the primary key of each row. Mapping parameters supported: table, column, db_group, max_lo, pk_column_name, pk_column_value(requried, and must be a positive integer)

 

主鍵生成器的參數名稱和含義與hibernate相同,只有一個例外:guzz中hilo的默認表名稱爲guzz_unique_key,而不是hibernate_unique_key。
此外,在hilo,sequence,seqhilo生成器中,guzz還容許傳入db_group參數,用於指定從那個數據庫組獲取主鍵。若是應用須要保持全部數據庫組的id相互之間都不重複,能夠經過此參數指定全部表從1組數據庫生成主鍵。db_group參數默認爲null,表示從當前表所在的數據庫讀取主鍵。

id generator注意事項:

    • 1. oracle數據庫的native默認使用seqence generator(序列),對於序列須要經過參數sequence傳入數據庫sequence的名稱,如:
    • 1  <generator class="native">
      2                         <param name="sequence">seq_sometable_id</param>
      3                 </generator>

      不然,guzz默認使用名稱爲guzz_sequence的序列,請提早建立此序列,guzz並不會自動建立。建立語句:

    • 1 CREATE SEQUENCE guzz_sequence INCREMENT BY 1 START WITH 1
    • 2. Mysql和H2數據庫,native表示默認使用identify(數據庫中的自增ID)。param name="sequence"參數會自動忽略掉。
      一個hbm.xml映射文件的例子
    •  1 <?xml version="1.0"?>
       2 <!DOCTYPE guzz-mapping PUBLIC "-//GUZZ//GUZZ MAPPING DTD//EN" "http://www.guzz.org/dtd/guzz-mapping.dtd">
       3 <hibernate-mapping>
       4     <class name="org.guzz.service.core.impl.IncUpdateBusiness" table="tb_guzz_su" businessName="guzzSlowUpdate" dbGroup="logDB">
       5         <id name="id" type="bigint" column="gu_id">
       6                 <generator class="native">
       7                         <param name="sequence">seq_iub_id</param>
       8                 </generator>
       9         </id>
      10         <property name="dbGroup" type="string" column="gu_db_group" />
      11         <property name="tableName" type="string" column="gu_tab_name" />
      12         <property name="columnToUpdate" type="string" column="gu_inc_col" />
      13         <property name="pkColunName" type="string" column="gu_tab_pk_col" />
      14         <property name="pkValue" type="string" column="gu_tab_pk_val" />
      15         <property name="countToInc" type="int" column="gu_inc_count" />
      16     </class>
      17 </hibernate-mapping>

      相似ibatis的映射

      在guzz.xml中進行定義,示例:
    •  1  <sqlMap dbgroup="default">
       2         
       3                 <update id="updateUserFavCount" orm="userObjectMap">
       4                         update @@user set @favCount = favCount + 1
       5                 </update>
       6                 
       7                 <update id="updateUserFavCount2" orm="user">
       8                         update @@user set @favCount = favCount + 1
       9                 </update>
      10                 
      11                 <select id="selectUsers" orm="userObjectMap">
      12                         select @id, @name, @vip, @favCount from @@user
      13                 </select>
      14                 
      15                 <select id="listCommentsByName" orm="commentMap" >
      16                         select * from @@commentMap where @userName = :userName
      17                 </select>
      18                                 
      19                 <orm id="userObjectMap" class="org.guzz.test.UserModel">
      20                         <result property="id" column="pk"/>
      21                     <result property="name" column="userName"/>
      22                     <result property="favCount" column="FAV_COUNT"/>
      23                     <result property="vip" column="VIP_USER"/>
      24                 </orm>
      25                 
      26                 <orm id="commentMap" dbgroup="commentDB" class="org.guzz.test.Comment" table="TB_COMMENT" shadow="org.guzz.test.CommentShadowView">
      27                         <result property="id" column="id" type="int"/>
      28                     <result property="userId" column="userId" type="int"/>
      29                     <result property="userName" column="userName" type="string" />
      30                     <result property="createdTime" column="createdTime" type="datetime" />
      31                 </orm>
      32         </sqlMap>
相關文章
相關標籤/搜索