一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - eclipse下搭建hibernate5.0環境的步驟(圖文)

eclipse下搭建hibernate5.0環境的步驟(圖文)

2021-05-04 12:14yumiaoxa Java教程

這篇文章主要介紹了eclipse下搭建hibernate5.0環境的步驟(圖文),小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

本文介紹了eclipse下搭建hibernate5.0環境的步驟,分享給大家,具體如下:

  1. hibernate引入的jar包:hibernate-release-5.0.12.final.zip
  2. 數據庫驅動:mysql-connector-java-5.1.46

二.安裝hibernate插件

打開eclipse,點擊help-->eclipse marketplace,如圖輸入:hibernate tools,再點擊goa按鈕,找到jboss tools

eclipse下搭建hibernate5.0環境的步驟(圖文)

點擊install安裝

eclipse下搭建hibernate5.0環境的步驟(圖文)

如圖選擇hibernate tools,點擊confrm安裝。安裝完成后重啟eclipse。

三. 創建工程

1.創建新項目hibernatedemo,在工程下建立lib文件夾。打開jar包的目錄,導入lib/required下的和數據庫的jar包,add to build path

eclipse下搭建hibernate5.0環境的步驟(圖文)

在src下新建文件

eclipse下搭建hibernate5.0環境的步驟(圖文)

點擊next,默認文件名,點擊next,如圖配置數據庫信息

eclipse下搭建hibernate5.0環境的步驟(圖文)

選擇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"/>

測試驗證成功。

整個工程架構如圖:

eclipse下搭建hibernate5.0環境的步驟(圖文)

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:http://www.cnblogs.com/yumiaoxia/p/9074904.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 人人九九精 | 撕开老师的丝袜白丝扒开粉嫩的小 | 国产精品视频在线观看 | 国产成人精品在线 | 亚洲国产韩国欧美在线不卡 | 公园暴露娇妻小说 | 高中生喷水喷浆 | 久久99r66热这里只有精品 | 7777奇米四色 | 亚洲欧美国产另类视频 | 免费全看男女拍拍拍的视频 | 精品国产欧美一区二区三区成人 | 女医学护士一级毛片 | 亚洲激情在线视频 | 天天爱天天做天天爽天天躁 | 嫩草影院永久在线一二三四 | 欧美色综合高清免费 | 国产成人欧美 | 情乱奶水欲 | 亚洲高清国产拍精品动图 | 国产日本欧美亚洲精品视 | 男人疯狂进女人下部视频动漫 | 欧美激烈精交gif动态图18p | 亚洲乱人伦在线 | 精品视频在线免费观看 | 日韩欧美色| 日韩福利一区 | 亚洲欧美一区二区三区在线观看 | 国产成人亚洲精品91专区高清 | 色婷婷六月天 | 日本国产成人精品视频 | 农村老妇1乱69系列小说 | 全日爱韩国视频在线观看 | 欧美一二区 | 久久伊人精品青青草原2021 | 91香蕉在线 | 91免费在线播放 | 午夜亚洲视频 | 视频在线播放 | 亚洲精品国产一区二区三区在 | 国产欧美精品一区二区三区–老狼 |