日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

mysql如何配置hbm.xml_配置数据库映射文件hbm.xml

發布時間:2025/4/16 数据库 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql如何配置hbm.xml_配置数据库映射文件hbm.xml 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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

hibernate的hbm.xml映射

guzz兼容hibernate的配置文件(xxx.hbm.xml),你可以使用hibernate映射工具生成或維護這些映射,guzz將可以直接解析。

使用hibernate映射文件時,某些guzz的特性在hibernate mapping dtd中沒有定義,文本編輯時不能自動提示,甚至會報錯,但不影響使用。

配置領域對象到系統中

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

1

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

屬性名

Required

作用

name

Required

business name。

file

Required

hbm.xml文件位置

dbgroup

Optional

對象存儲到的數據庫組。默認為default。

class

Optional

如果填寫,覆蓋hbm.xml中定義的域對象class

interpret

Optional

自定義查詢條件解析器。

一個典型的配置如下:

1

2

其中,每個領域對象的定義可以在guzz測試源代碼中找到。

guzz自己的映射定義

將hibernate hbm.xml的dtd定義頭,改成

1

既可。余下內容與hibernate類似。

mapping xml的root可以是,也可以是(便于使用成熟的hibernate工具)。guzz在解析時只從處開始讀取,root元素如何定義無關緊要。

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

class定義增加了3屬性:

businessName:默認的business name。域對象在guzz.xml中聲明時可以覆蓋此設置?!緂uzz1.3.1新特性】

dbGroup:對象默認存儲在的數據庫組。域對象在guzz.xml中聲明時可以覆蓋此設置。【guzz1.3.1新特性】

shadow:用于設置表分切和自定義表,使用方法在后面有詳細的介紹。

property定義增加了2個屬性:

null: 指定當數據庫值為空時,字段取何值。默認為java定義此類型的默認值。這個屬性一般在數據庫升級字段時使用,避免hibernate惱人的cglib....get..set錯誤。

loader: 指定字段的特殊讀取類。通過loader可以定義某些字段的特殊讀取策略,如從文件加載,或先讀緩存再讀數據庫,或從第三方系統讀取等。詳細介紹請參看:LazyLoad

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

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??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)

主鍵生成器的參數名稱和含義與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

2 seq_sometable_id

3

否則,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

3

4

5

6

7 seq_iub_id

8

9

10

11

12

13

14

15

16

17

類似ibatis的映射

在guzz.xml中進行定義,示例:

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

總結

以上是生活随笔為你收集整理的mysql如何配置hbm.xml_配置数据库映射文件hbm.xml的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。