hibernate 全面学习 【hibernate 基本映射】

Hibernate基本映射:

类-->数据库表
普通属性--> 表字段

通过<class>标签映射到数据库表,通过<property>标签将普通属性映射到表字段
所谓普通属性,不包括自定义类,集合类和数组等。

实体类四条主要的规则:
* 实现一个默认的(即无参数的)构造方法(constructor)
* 提供一个标识属性(identifier property)(可选)
* 使用非final的类 (可选)
* 为持久化字段声明访问器(accessors)(可选)

<class>标签用来将一个类映射到数据库表,其name属性表示这个类的全路径,默认情况下,
映射到数据库表名,与类名相同。我们可以通过<class>标签的table属性更改表的名称

将class 节点中的schema 去掉。


<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.bjsxt.hibernate.User1" table="t_user1">
<id name="id">
<generator class="uuid"/>
</id>
<property name="name" length="20" unique="true"/>
<property name="password"/>
<property name="createTime"/>
<property name="expireTime"/>
</class>
</hibernate-mapping>

反转生成数据库表


package com.bjsxt.hibernate;

import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

public class ExportDB {

public static void main(String[] args) {

//读取配置文件
Configuration cfg = new Configuration().configure();

//创建SchemaExport对象
SchemaExport export = new SchemaExport(cfg);

//创建数据库表
export.create(true,true);
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值