一、建立项目

二、右键项目,选择MyEclipse选项。

三、添加Hibernate库文件

a) 选择库文件

Hibernate – MyEclipse开发环境搭建_Hibernate

b) 添加配置文件,新建一个配置文件

Hibernate – MyEclipse开发环境搭建_Hibernate_02

c) 添加数据库连接,使用JDBC形势,提前建立完成数据库连接。

Hibernate – MyEclipse开发环境搭建_Hibernate_03

d) 添加SessionFoctory工厂类

Hibernate – MyEclipse开发环境搭建_Hibernate_04

四、添加显示sql语句的配置

a) 显示sql语句

Hibernate – MyEclipse开发环境搭建_Hibernate_05

b) 格式化显示sql语句

Hibernate – MyEclipse开发环境搭建_Hibernate_06

五、创建数据库表的SQL

代码:

drop table if exists user;

create table user(

id int primary key auto_increment,

userName varchar(30),

passWord varchar(30)

);

六、编写实体类

代码:

public class User implements Serializable {

private int id;

private String userName;

private String passWord;

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

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;

}

@Override

public String toString() {

return "User [id=" + id + ", passWord=" + passWord + ", userName="

+ userName + "]";

}

public User() {

super();

// TODO Auto-generated constructor stub

}

public User(int id, String userName, String passWord) {

super();

this.id = id;

this.userName = userName;

this.passWord = passWord;

}

}

七、编写影射文件User.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="com.www.hibernate.pojo.User">

<id name="id">

<generator class="native"></generator>

</id>

<property name="userName"></property>

<property name="passWord"></property>

</class>

</hibernate-mapping>

八、添加影射文件到Hibernate配置文件中

代码:

<?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">

<hibernate-configuration>

<session-factory>

<property name="dialect">

org.hibernate.dialect.MySQLDialect

</property>

<property name="connection.url">

jdbc:mysql://localhost:3306/hibernate

</property>

<property name="connection.username">root</property>

<property name="connection.password">root</property>

<property name="connection.driver_class">

com.mysql.jdbc.Driver

</property>

<property name="myeclipse.connection.profile">MySQL</property>

<property name="show_sql">true</property>

<property name="format_sql">true</property>

<mapping resource="com/www/hibernate/pojo/User.hbm.xml" />

</session-factory>

</hibernate-configuration>

九、测试

代码:

public class Test {

/**

* @param args

*/

public static void main(String[] args) {

addUser();

}

public static void addUser(){

Session session = HibernateSessionFactory.getSession();

Transaction tran =session.beginTransaction();

User user = new User();

user.setPassWord("passWord");

user.setUserName("userName");

session.save(user);

tran.commit();

session.close();

}

public static void create(){

Configuration cfg = new Configuration().configure();

SchemaExport export = new SchemaExport(cfg);

export.create(true, true);

}

}