每个hibernate只会启动的时候引入一个文件,那就是:hibernate.cfg.xml
mapping需要我们在hibernate中引入,
<mapping resource="com/hibernate/test/hibernate_IP.xml"/>
<mapping class="com.hibernate.test.Student"/>
代码片段:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.hibernate.test">
<class name="IP_List" table="IP_LIST">
<id name="ip" column="Ip">
<generator class="native"></generator>
</id>
<property name="Status" column="Status"></property>
</class>
</hibernate-mapping>
class标签 对应的name为Java实体类 table为表名;
id为主键 主键自增策略:<generator class="native"></generator> native会根据不同数据库 采取不同的自增策略
<property>标签对应数据库中的字段 column
package com.hibernate.test;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="Student")
public class Student {
private int id;
private String name;
/**
* id
*
* @return the id
* @since CodingExample Ver(编码范例查看) 1.0
*/
@Id
@GeneratedValue
public int getId() {
return id;
}
/**
*
* @param id
* the id to set
*/
public void setId(int id) {
this.id = id;
}
/**
* name
*
* @return the name
* @since CodingExample Ver(编码范例查看) 1.0
*/
@Column(name="name")
public String getName() {
return name;
}
/**
*
* @param name
* the name to set
*/
public void setName(String name) {
this.name = name;
}
}
实体类添加注解:@Entity 对应的表为@Table
主键为@ID 自增策略配置:@GeneratedValue
@Column 注解对应为数据库中的字段