mysql如何配置hbm.xml_配置数据库映射文件hbm.xml
在數(shù)據(jù)庫映射方面,guzz支持類似hibernate的領(lǐng)域?qū)ο骳lass-table映射。也支持ibatis類似的在guzz.xml中映射。
hibernate的hbm.xml映射
guzz兼容hibernate的配置文件(xxx.hbm.xml),你可以使用hibernate映射工具生成或維護(hù)這些映射,guzz將可以直接解析。
使用hibernate映射文件時,某些guzz的特性在hibernate mapping dtd中沒有定義,文本編輯時不能自動提示,甚至?xí)箦e,但不影響使用。
配置領(lǐng)域?qū)ο蟮较到y(tǒng)中
對于hbm.xml配置的領(lǐng)域?qū)ο?#xff0c;每個對象在guzz.xml中增加一個business來聲明:
1
每個business標(biāo)簽對應(yīng)1個領(lǐng)域?qū)ο蟆usiness擁有5個屬性:
屬性名
Required
作用
name
Required
business name。
file
Required
hbm.xml文件位置
dbgroup
Optional
對象存儲到的數(shù)據(jù)庫組。默認(rèn)為default。
class
Optional
如果填寫,覆蓋hbm.xml中定義的域?qū)ο骳lass
interpret
Optional
自定義查詢條件解析器。
一個典型的配置如下:
1
2
其中,每個領(lǐng)域?qū)ο蟮亩x可以在guzz測試源代碼中找到。
guzz自己的映射定義
將hibernate hbm.xml的dtd定義頭,改成
1
既可。余下內(nèi)容與hibernate類似。
mapping xml的root可以是,也可以是(便于使用成熟的hibernate工具)。guzz在解析時只從處開始讀取,root元素如何定義無關(guān)緊要。
guzz mapping相對hibernate mapping也有一些變化。
class定義增加了3屬性:
businessName:默認(rèn)的business name。域?qū)ο笤趃uzz.xml中聲明時可以覆蓋此設(shè)置。【guzz1.3.1新特性】
dbGroup:對象默認(rèn)存儲在的數(shù)據(jù)庫組。域?qū)ο笤趃uzz.xml中聲明時可以覆蓋此設(shè)置。【guzz1.3.1新特性】
shadow:用于設(shè)置表分切和自定義表,使用方法在后面有詳細(xì)的介紹。
property定義增加了2個屬性:
null: 指定當(dāng)數(shù)據(jù)庫值為空時,字段取何值。默認(rèn)為java定義此類型的默認(rèn)值。這個屬性一般在數(shù)據(jù)庫升級字段時使用,避免hibernate惱人的cglib....get..set錯誤。
loader: 指定字段的特殊讀取類。通過loader可以定義某些字段的特殊讀取策略,如從文件加載,或先讀緩存再讀數(shù)據(jù)庫,或從第三方系統(tǒng)讀取等。詳細(xì)介紹請參看:LazyLoad
type屬性支持定義日期格式:
type用于指定數(shù)據(jù)類型,如:int, string等。對于日期類型數(shù)據(jù),一般有三種:datetime表示時間戳(日期+時間),date表示日期,time表示時間。對于這三種類型,在定義時可以在類型后加"|"線,并跟上日期格式(參看DateFormat定義)完成格式定義。
例如:type="datetime|YYYY-MM-dd HH:mm:SS",type="date|MM-dd"。
傳入日期格式后,進(jìn)行sql查詢時,對于日期字段,可以傳入按照格式生成的日期字符串直接查詢,guzz將會自動按照格式轉(zhuǎn)成需要的數(shù)據(jù)類型。這種特性,在按照用戶輸入日期進(jìn)行查詢或向guzz taglib傳入日期參數(shù)時,會比較方便。
枚舉類型支持:
guzz支持按照ordinal和value兩種方式映射枚舉類型。對于ordinal方式,數(shù)據(jù)庫字段需要定義為int整數(shù)類型;對于value方式,數(shù)據(jù)庫字段需要定義為可以保存字符串的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. 支持的參數(shù):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??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??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
×
√
A?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)
主鍵生成器的參數(shù)名稱和含義與hibernate相同,只有一個例外:guzz中hilo的默認(rèn)表名稱為guzz_unique_key,而不是hibernate_unique_key。
此外,在hilo,sequence,seqhilo生成器中,guzz還允許傳入db_group參數(shù),用于指定從那個數(shù)據(jù)庫組獲取主鍵。如果應(yīng)用需要保持所有數(shù)據(jù)庫組的id相互之間都不重復(fù),可以通過此參數(shù)指定所有表從1組數(shù)據(jù)庫生成主鍵。db_group參數(shù)默認(rèn)為null,表示從當(dāng)前表所在的數(shù)據(jù)庫讀取主鍵。
id generator注意事項:
1. oracle數(shù)據(jù)庫的native默認(rèn)使用seqence generator(序列),對于序列需要通過參數(shù)sequence傳入數(shù)據(jù)庫sequence的名稱,如:
1
2 seq_sometable_id
3
否則,guzz默認(rèn)使用名稱為guzz_sequence的序列,請?zhí)崆皠?chuàng)建此序列,guzz并不會自動創(chuàng)建。創(chuàng)建語句:
1 CREATE SEQUENCE guzz_sequence INCREMENT BY 1 START WITH 1
2. Mysql和H2數(shù)據(jù)庫,native表示默認(rèn)使用identify(數(shù)據(jù)庫中的自增ID)。param name="sequence"參數(shù)會自動忽略掉。
一個hbm.xml映射文件的例子
1 <?xml version="1.0"?>
2
3
4
5
6
7 seq_iub_id
8
9
10
11
12
13
14
15
16
17
類似ibatis的映射
在guzz.xml中進(jìn)行定義,示例:
1
2
3
4 update @@user set @favCount = favCount + 15
6
7
8 update @@user set @favCount = favCount + 19
10
11
12 select @id, @name, @vip, @favCount from @@user13
14
15
16 select * from @@commentMap where @userName = :userName17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
總結(jié)
以上是生活随笔為你收集整理的mysql如何配置hbm.xml_配置数据库映射文件hbm.xml的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: img文件解包工具_使用tar归档命令,
- 下一篇: handlerinterceptorad