映射文件
位置:与相应的模型类同包
命名:【该模型类名】.hbm.xml
例如:
案例:
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- DTD:用于获取提示信息 -->
<!-- hibernate的映射 -->
<hibernate-mapping>
<!-- name:该表模型的全路径 table:对应数据库名称
dynamic-insert:动态生成插入语句 dynamic-update:动态生成更新语句 -->
<class name="com.moli.hibernate.domain.User" table="t_user"
dynamic-insert="true" dynamic-update="true">
<!-- name:模型属性名 column:对应数据库字段名 -->
<id name="uid" column="id">
<!-- generator:id常用生成策略: -->
<!-- native:如果是mysql数据库,id会自动增长 -->
<!-- uuid:自动设置长字符串,注意类型需要String -->
<!-- assigned:手动设置值 -->
<generator class="native"></generator>
</id>
<!-- 如果模型的属性和数据库的列名一样,就不用写column -->
<property name="username"></property>
<property name="password"></property>
<!--
Date:
1.在property中type不写,数据库对应的类型 datetime【年月日时分秒】
2.type中data,数据库对应的类型 date【只有年月日】
3.type中time,数据库对应的类型 time【只有时分秒】
4.type中timestamp,数据库对应的类型 timestamp【时间戳】-感觉不太好用
如果要用时间戳,建议用long类型
-->
<property name="birthday" type="long"></property>
</class>
</hibernate-mapping>
与之相对应的JAVA模型类:
public class User
{
private int uid;
private String username;
private String password;
private Long birthday;
public Long getBirthday() {
return birthday;
}
public void setBirthday(Long birthday) {
this.birthday = birthday;
}
public int getUid()
{
return uid;
}
public void setUid(int uid)
{
this.uid = uid;
}
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username = username;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
public User(int uid, String username, String password)
{
super();
this.uid = uid;
this.username = username;
this.password = password;
}
public User()
{
super();
}
@Override
public String toString()
{
return "User [uid=" + uid + ", username=" + username + ", password="
+ password + "]";
}
}