Hibernate – MyEclipse開發環境搭建

一、建立項目

二、右鍵項目,選擇MyEclipse選項。

三、添加Hibernate庫文件

a) 選擇庫文件

clip_image002

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

clip_image004

c) 添加數據庫連接,使用JDBC形勢,提前建立完成數據庫連接。

clip_image006

d) 添加SessionFoctory工廠類

clip_image008

四、添加顯示sql語句的配置

a) 顯示sql語句

clip_image010

b) 格式化顯示sql語句

clip_image012

五、創建數據庫表的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);

}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章