eclipse下怎么搭建hibernate5.0環(huán)境-創(chuàng)新互聯(lián)

這篇文章給大家分享的是有關eclipse下怎么搭建hibernate5.0環(huán)境的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

專注于為中小企業(yè)提供成都網(wǎng)站設計、成都網(wǎng)站建設、外貿(mào)網(wǎng)站建設服務,電腦端+手機端+微信端的三站合一,更高效的管理,為中小企業(yè)肅寧免費做網(wǎng)站提供優(yōu)質(zhì)的服務。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了上千企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設實現(xiàn)規(guī)模擴充和轉(zhuǎ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按鈕,找到JBoss 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)容如下

<?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異常,在該文件中添加一些配置,如圖:

<?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() {
  return "News [id=" + id + ", title=" + title + ", content=" + content + "]";
}


}

編寫測試類:

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

<?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中配置

<mapping resource="com/gdut/app/entity/News.hbm.xml"/>

測試驗證成功。

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

eclipse下怎么搭建hibernate5.0環(huán)境

感謝各位的閱讀!關于“eclipse下怎么搭建hibernate5.0環(huán)境”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

網(wǎng)頁題目:eclipse下怎么搭建hibernate5.0環(huán)境-創(chuàng)新互聯(lián)
當前網(wǎng)址:http://bm7419.com/article42/dgooec.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、網(wǎng)站建設、動態(tài)網(wǎng)站、品牌網(wǎng)站制作、外貿(mào)建站、定制網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化