在数据库映射方面,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中声明时可以覆盖此设置。【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  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相关推荐

  1. 使用oracle数据库和MySQL数据库时hibernate的映射文件.hbm.xml的不同

    假设是使用oracle数据库.那么hibernate的映射文件.hbm.xml例如以下: <id name="xuehao" column="xuehao" ...

  2. Hibernate - 对象关系映射文件(*.hbm.xml)详解

    对象关系映射文件,即POJO 类和数据库的映射文件*.hbm.xml(映射文件的扩展名为 .hbm.xml). POJO 类和关系数据库之间的映射可以用一个XML文档来定义. 通过 POJO 类的数据 ...

  3. hibernate mysql 配置文件_hibernate 框架的配置文件和映射文件以及详解

    从官网下载的压缩包其实有很多有用的东西. 最重要的就是jar包.这个不用说. 第二个是帮助文档:如图,各种语言,格式半本的.很贴心. 第三个.源代码. 第四个就是本文的重点,配置文件的标准文档. 下面 ...

  4. Mybatis+mysql动态分页查询数据案例——配置映射文件(HouseDaoMapper.xml)

    <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "- ...

  5. 高校教室预约使用管理系统(PHP+Mysql)毕业论文+项目源码+数据库sql文件

    目 录 第一章 绪论 5 1.1 选题目的和意义 5 1.2 系统介绍 5 1.3 本系统使用的方法和语言 6 1.3.1 统一建模语言UML 6 1.3.2 .PHP 6 第二章 系统分析 9 2. ...

  6. 实体类Entity与接口映射文件Mapper.xml之间的联系

    现在的初略理解:实体类中的属性都一一对应了数据库表中的属性,而一般在mapper.xml内会用到baseMap,将所有数据库表内用到的属性都和实体类绑定起来,一般而言,实体类中的属性不能乱加,必须数据 ...

  7. hibernate mysql 自动建表_配置hibernate根据实体类自动建表功能

    Hibernate支持自动建表,在开发阶段很方便,可以保证hbm与数据库表结构的自动同步. 如何使用呢?很简单,只要在hibernate.cfg.xml里加上如下代码 Xml代码update upda ...

  8. 快捷配置Hibernate实现对数据库的配置(构建通道)

    在基于MVC设计模式的JAVA WEB应用中,Hibernate可以作为模型层/数据访问层.它通过配置文件(hibernate.properties或hibernate.cfg.xml)和映射文件(* ...

  9. 配置MyBatis映射文件以及核心配置文件模板

    提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录 前言 一.db.properties模板 二.映射文件mapper.xml模板 三.---------MyBatis核心文件 ...

最新文章

  1. 业余时间用哪里,哪里就有发展的可能
  2. 《OpenCV3编程入门》学习笔记8 图像轮廓与图像分割修复(五)分水岭算法(watershed algorithm)
  3. Android视图绘制逻辑与思想(一):attachToRoot到底有什么作用?
  4. Csharp+Asp.net系列教程(四)
  5. 如何安装微信支付证书到服务器,微信支付如何安装操作证书?
  6. 【python】 调用selenium中 ChromeDriver不匹配的问题
  7. java project mybatis,Java使用Mybatis
  8. CSS:公共样式(自用)
  9. OpenGL基础知识的理解
  10. 快速远程访问内网的摄像头【内网穿透】
  11. dpkg制作deb包详解
  12. 社科院与杜兰大学金融管理硕士项目—努力的你,值得拥有更好的人生
  13. ​历经 33 天,终于拿到了心仪的 Offer
  14. 能够威慑苹果的,可能只有荣耀了
  15. Cannot Resolve method subscribe(anonymous org.reactivestreams.Subscriber 已解决
  16. python + snownlp 正负面分析
  17. 微信数据包解析 php,微信交互数据包分析 - 新手入门及其它(软件) - 恩山无线论坛 - Powered by Discuz!...
  18. C语言小游戏-五子棋
  19. vs code中的platformIO插件,完成Arduino的程序编写,导入,安装开发板管理库
  20. 豆瓣评分8.9,这本Python经典入门书重磅升级

热门文章

  1. 程序员修炼之道-笔记
  2. 用YII实现多重查询(基于tag)
  3. 【CentOS7-Python系列】之一【VMwareWorkstation安装CentOS7】
  4. 12864液晶显示实验
  5. Rackspace进中国,坚决“不掉价”
  6. Oracle创建dblink和同义词小记
  7. 职业经理人应该如何处理如下问题
  8. 基于DotNet构件技术的企业级敏捷软件开发平台 - AgileEAS.NET - 文章汇总及学习指南...
  9. 第三章 学习ICE 3.0--Slice语言
  10. 【正一专栏】今晚国足能上演奇迹吗?