配置数据库映射文件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 name="user" dbgroup="default" file="classpath:org/guzz/test/User.hbm.xml" />

每个business标签对应1个领域对象。business拥有5个属性:

属性名 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也有一些变化。

class定义增加了3属性:

businessName:默认的business name。域对象在guzz.xml中声明时可以覆盖此设置。【guzz1.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 <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>
原文地址:https://www.cnblogs.com/SuperBing/p/4963065.html