本文介紹了eclipse下搭建hibernate5.0環境的步驟,分享給大家,具體如下:
- hibernate引入的jar包:hibernate-release-5.0.12.final.zip
- 數據庫驅動:mysql-connector-java-5.1.46
二.安裝hibernate插件
打開eclipse,點擊help-->eclipse marketplace,如圖輸入:hibernate tools,再點擊goa按鈕,找到jboss tools
點擊install安裝
如圖選擇hibernate tools,點擊confrm安裝。安裝完成后重啟eclipse。
三. 創建工程
1.創建新項目hibernatedemo,在工程下建立lib文件夾。打開jar包的目錄,導入lib/required下的和數據庫的jar包,add to build path
在src下新建文件
點擊next,默認文件名,點擊next,如圖配置數據庫信息
選擇utf-8編碼方式,點擊finish,生成的hibernate.cfg.xml配置文件內容如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<?xml version= "1.0" encoding= "utf-8" ?> <!doctype hibernate-configuration public "-//hibernate/hibernate configuration dtd 3.0//en" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd" > <hibernate-configuration> <session-factory> <property name= "hibernate.connection.driver_class" >com.mysql.jdbc.driver</property> <property name= "hibernate.connection.password" >a123</property> <property name= "hibernate.connection.url" >jdbc:mysql: //localhost:3306/tb_test</property> <property name= "hibernate.connection.username" >sherman</property> <property name= "hibernate.dialect" >org.hibernate.dialect.mysqldialect</property> </session-factory> </hibernate-configuration> |
注意,把 < session-factory name ="mysql" > 的name屬性去掉,否則報org.hibernate.engine.jndi.jndiexception異常,在該文件中添加一些配置,如圖:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
<?xml version= "1.0" encoding= "utf-8" ?> <!doctype hibernate-configuration public "-//hibernate/hibernate configuration dtd 3.0//en" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd" > <hibernate-configuration> <session-factory> <property name= "hibernate.connection.driver_class" >com.mysql.jdbc.driver</property> <property name= "hibernate.connection.password" >a123</property> <property name= "hibernate.connection.url" >jdbc:mysql: //localhost:3306/tb_test</property> <property name= "hibernate.connection.username" >sherman</property> <!-- 配置數據庫方言 --> <property name= "hibernate.dialect" >org.hibernate.dialect.mysql5dialect</property> <!-- 控制臺打印sql語句 --> <property name= "show_sql" > true </property> <!-- 格式化sql --> <property name= "format_sql" > true </property> <!--在啟動時根據配置更新數據庫 --> <property name= "hibernate.hbm2ddl.auto" >update</property> <!-- 配置連接池的連接數 --> <property name= "connection.pool_size" > 20 </property> <!-- 注冊實體映射類 --> <mapping class = "com.gdut.app.entity.news" /> </session-factory> </hibernate-configuration> |
在src下新建一個包com.gdut.app.entity,存放持久化類news,news類代碼如下
package com.gdut.app.entity;
import javax.persistence.entity;
import javax.persistence.generatedvalue;
import javax.persistence.generationtype;
import javax.persistence.id;
import javax.persistence.table;@entity
@table(name="news_info")
public class news {
@id
@generatedvalue(strategy=generationtype.identity)
private integer id;
private string title;
private string content;
public news() {
}
public news(integer id, string title, string content) {
this.id = id;
this.title = title;
this.content = content;
}
public integer getid() {
return id;
}
public void setid(integer id) {
this.id = id;
}
public string gettitle() {
return title;
}
public void settitle(string title) {
this.title = title;
}
public string getcontent() {
return content;
}
public void setcontent(string content) {
this.content = content;
}
@override
public string tostring() {
}
}
編寫測試類:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
package com.gdut.app.entity; import org.hibernate.session; import org.hibernate.sessionfactory; import org.hibernate.transaction; import org.hibernate.cfg.configuration; import org.junit.test; public class beantest { @test public void beantest() { // final standardserviceregistry serviceregistry = new standardserviceregistrybuilder() // .configure("hibernate.cfg.xml").build(); // // sessionfactory sf = new metadatasources(serviceregistry).buildmetadata().buildsessionfactory(); //兩種方式都可以獲取sessionfactory configuration cfg = new configuration().configure(); sessionfactory sf = cfg.buildsessionfactory(); session sess =sf.opensession(); transaction transaction = sess.begintransaction(); news n = new news(); n.setcontent( "在廣工畢業" ); n.settitle( "畢業季" ); sess.save(n); transaction.commit(); sess.close(); } } |
經過測試成功
或者通過映射文件
在com.gdut.app.entity包下簡歷一個news.hbm.xml映射配置文件,修改genarator的class屬性為active
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<?xml version= "1.0" ?> <!doctype hibernate-mapping public "-//hibernate/hibernate mapping dtd 3.0//en" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" > <!-- generated 2018 - 5 - 22 23 : 45 : 23 by hibernate tools 3.5 . 0 . final --> <hibernate-mapping> < class name= "com.gdut.app.entity.news" table= "news" > <id name= "id" type= "java.lang.integer" > <column name= "id" /> <generator class = "native" /> </id> <property name= "title" type= "java.lang.string" > <column name= "title" /> </property> <property name= "content" type= "java.lang.string" > <column name= "content" /> </property> </ class > </hibernate-mapping> |
在hibernate.cfg.xml中配置
1
|
<mapping resource= "com/gdut/app/entity/news.hbm.xml" /> |
測試驗證成功。
整個工程架構如圖:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://www.cnblogs.com/yumiaoxia/p/9074904.html