1, first, hibernate-related jar packages are imported into the Lib directory;
2, Write JavaBean class
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, write the Xxx.hbm.xml file of the same name in this JavaBean directory
<?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" >
<class name= "Chen.can.Dao.TRegister" table= "T_register" >//note, remember to write the directory where JavaBean is located
<id name= "id" type= "Java.lang.Integer" >
<column name= "id"/>
<!--I did not set this field increment in MySQL, but it can still be set to increment---in Hibernate
<generator class= "Increment"/>
</id>
<property name= "UserName" type= "java.lang.String" >
<column name= "userName" length= "/>"
</property>
<property name= "userpwd" type= "java.lang.String" >
<column name= "userpwd" length= "/>"
</property>
<property name= "Sex" type= "java.lang.String" >
<column name= "Sex" length= "ten"/>
</property>
<property name= "Age" type= "Java.lang.Integer" >
<column name= "Age"/>
</property>
</class>
4, write hibernate.cfg.xml in src directory
<?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. -
<session-factory>
<property name= "Hibernate.dialect" >org.hibernate.dialect.MySQLDialect</property>
<!--driver name--
<property name= "Hibernate.connection.driver_class" >com.mysql.jdbc.Driver</property>
<!--database name--
<property name= "Hibernate.connection.url" >jdbc:mysql://localhost:3305/login</property>
<!--user name--
<property name= "Hibernate.connection.username" >root</property>
<!--password--
<property name= "Hibernate.connection.password" >000000</property>
<property name= "Show_sql" >true</property>
<mapping resource= "Chen/can/dao/tregister.hbm.xml"/>
</session-factory>
Hibernate's Environment configuration