1,首先把跟Hibernate相关的jar包导入到lib目录下;
2,写Javabean类
package chen.can.Dao;
public class TRegister implements java.io.Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
// Fields
private Integer id;
private String userName;
private String userPwd;
private String sex;
private Integer age;
// Constructors
/** default constructor */
public TRegister() {
}
/** minimal constructor */
public TRegister(Integer id) {
this.id = id;
}
/** full constructor */
public TRegister(Integer id, String userName, String userPwd, String sex, Integer age) {
this.id = id;
this.userName = userName;
this.userPwd = userPwd;
this.sex = sex;
this.age = age;
}
// Property accessors
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserName() {
return this.userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPwd() {
return this.userPwd;
}
public void setUserPwd(String userPwd) {
this.userPwd = userPwd;
}
public String getSex() {
return this.sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Integer getAge() {
return this.age;
}
public void setAge(Integer age) {
this.age = age;
}
}
3,在这个javabean目录下写相同名字的 ×××.hbm.xml文件
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="chen.can.Dao.TRegister" table="t_register">//注意下,这里千万记得写javabean所在的目录
<id name="id" type="java.lang.Integer">
<column name="id" />
<!-- 我在MYSQL中并没有设置该字段递增,但在Hibernate中依然可以设置为递增 -->
<generator class="increment" />
</id>
<property name="userName" type="java.lang.String">
<column name="userName" length="30" />
</property>
<property name="userPwd" type="java.lang.String">
<column name="userPwd" length="30" />
</property>
<property name="sex" type="java.lang.String">
<column name="sex" length="10" />
</property>
<property name="age" type="java.lang.Integer">
<column name="age" />
</property>
</class>
</hibernate-mapping>
4,在src目录下写hibernate.cfg.xml
<?xml version=‘1.0‘ encoding=‘UTF-8‘?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 驱动程序名 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- 数据库名称 -->
<property name="hibernate.connection.url">jdbc:mysql://localhost:3305/login</property>
<!-- 用户名 -->
<property name="hibernate.connection.username">root</property>
<!-- 密码 -->
<property name="hibernate.connection.password">000000</property>
<property name="show_sql">true</property>
<mapping resource="chen/can/Dao/TRegister.hbm.xml" />
</session-factory>
</hibernate-configuration>