激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

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

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

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

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

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

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

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

  1. hibernate引入的jar包:hibernate-release-5.0.12.final.zip
  2. 數(shù)據(jù)庫驅(qū)動:mysql-connector-java-5.1.46

二.安裝hibernate插件

打開eclipse,點擊help-->eclipse marketplace,如圖輸入:hibernate tools,再點擊goa按鈕,找到j(luò)boss tools

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

點擊install安裝

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

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

三. 創(chuàng)建工程

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

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

在src下新建文件

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

點擊next,默認文件名,點擊next,如圖配置數(shù)據(jù)庫信息

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

選擇utf-8編碼方式,點擊finish,生成的hibernate.cfg.xml配置文件內(nèi)容如下

?
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>
    
    <!-- 配置數(shù)據(jù)庫方言 -->
    <property name="hibernate.dialect">org.hibernate.dialect.mysql5dialect</property>
    <!-- 控制臺打印sql語句 -->
    <property name="show_sql">true</property>
    <!-- 格式化sql -->
    <property name="format_sql">true</property>
    <!--在啟動時根據(jù)配置更新數(shù)據(jù)庫 -->
    <property name="hibernate.hbm2ddl.auto">update</property>
    <!-- 配置連接池的連接數(shù) -->
    <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("在廣工畢業(yè)");
    n.settitle("畢業(yè)季");
    sess.save(n);
    transaction.commit();
    sess.close();
    
  }
}

經(jīng)過測試成功

或者通過映射文件

在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"/>

測試驗證成功。

整個工程架構(gòu)如圖:

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

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

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产成人网| 一区二区三区在线观看免费视频 | 欧美a级大胆视频 | 手机黄色小视频 | 日本在线视频二区 | 羞羞的视频在线观看 | 国产精品剧情一区二区在线观看 | 日韩黄色成人 | 国产91小视频在线观看 | 免费毛片电影 | 日日碰日日操 | 欧美黑人xx | 好吊色欧美一区二区三区四区 | 成年人视频免费 | 亚洲精品动漫在线观看 | 久草在线观看资源 | 福利免费在线观看 | 九九精品在线播放 | 精品一区二区三区网站 | 精品一区二区久久久 | 一级做a爱性色毛片免费1 | 亚洲影视在线 | 成人黄色免费观看 | 99pron| 亚洲午夜天堂吃瓜在线 | 午夜a级片 | 久久在线免费视频 | 国产亚洲欧美视频 | 国产呻吟 | 国产一级毛片国语版 | 午夜在线视频一区二区三区 | 精品国产一区二区亚洲人成毛片 | 天天鲁在线视频免费观看 | 中文字幕专区高清在线观看 | 亚洲精品aⅴ中文字幕乱码 欧美囗交 | 国产91一区 | 二区精品视频 | 精品69人人人人 | 久色免费| 国内精品久久久久久久久久久久 | 嗯~啊~用力~高h |