欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2018年05月23日 10:18:06   作者:yumiaoxa  
這篇文章主要介紹了eclipse下搭建hibernate5.0環(huán)境的步驟(圖文),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

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

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

二.安裝hibernate插件

打開eclipse,點(diǎn)擊help-->eclipse marketplace,如圖輸入:Hibernate Tools,再點(diǎn)擊Goa按鈕,找到JBoss Tools

點(diǎn)擊install安裝

如圖選擇Hibernate Tools,點(diǎn)擊Confrm安裝。安裝完成后重啟eclipse。

三. 創(chuàng)建工程

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

在src下新建文件

點(diǎn)擊next,默認(rèn)文件名,點(diǎn)擊next,如圖配置數(shù)據(jù)庫(kù)信息

選擇UTF-8編碼方式,點(diǎn)擊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屬性去掉,否則報(bào)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ù)庫(kù)方言 -->
    <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
    <!-- 控制臺(tái)打印sql語(yǔ)句 -->
    <property name="show_sql">true</property>
    <!-- 格式化sql -->
    <property name="format_sql">true</property>
    <!--在啟動(dòng)時(shí)根據(jù)配置更新數(shù)據(jù)庫(kù) -->
    <property name="hibernate.hbm2ddl.auto">update</property>
    <!-- 配置連接池的連接數(shù) -->
    <property name="connection.pool_size">20</property>
    
    <!-- 注冊(cè)實(shí)體映射類 -->
    <mapping class="com.gdut.app.entity.News"/>
  </session-factory>
</hibernate-configuration>

在src下新建一個(gè)包c(diǎn)om.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 + "]";
}


}

編寫測(cè)試類:

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)過(guò)測(cè)試成功

或者通過(guò)映射文件

在com.gdut.app.entity包下簡(jiǎn)歷一個(gè)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"/>

測(cè)試驗(yàn)證成功。

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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java實(shí)現(xiàn)常用加密算法——單向加密算法MD5和SHA

    Java實(shí)現(xiàn)常用加密算法——單向加密算法MD5和SHA

    本篇文章主要介紹了Java實(shí)現(xiàn)常用加密算法——單向加密算法MD5和SHA,信息加密后數(shù)據(jù)更安全,需要的朋友可以參考下。
    2016-10-10
  • spring boot測(cè)試打包部署的方法

    spring boot測(cè)試打包部署的方法

    spring boot項(xiàng)目如何測(cè)試,如何部署,在生產(chǎn)中有什么好的部署方案嗎?這篇文章就來(lái)介紹一下spring boot 如何開發(fā)、調(diào)試、打包到最后的投產(chǎn)上線,感興趣的朋友一起看看吧
    2018-01-01
  • SpringBoot整合spring-data-jpa的方法

    SpringBoot整合spring-data-jpa的方法

    這篇文章主要介紹了SpringBoot整合spring-data-jpa的方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-06-06
  • logback.xml動(dòng)態(tài)配置程序路徑的操作

    logback.xml動(dòng)態(tài)配置程序路徑的操作

    這篇文章主要介紹了logback.xml動(dòng)態(tài)配置程序路徑的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-02-02
  • java實(shí)現(xiàn)2048小游戲

    java實(shí)現(xiàn)2048小游戲

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)2048小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-02-02
  • springboot項(xiàng)目配置多數(shù)據(jù)庫(kù)連接的示例詳解

    springboot項(xiàng)目配置多數(shù)據(jù)庫(kù)連接的示例詳解

    這篇文章主要介紹了springboot項(xiàng)目配置多數(shù)據(jù)庫(kù)連接的示例,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2023-12-12
  • 小項(xiàng)目改造快速引入 mybatis的流程分析

    小項(xiàng)目改造快速引入 mybatis的流程分析

    這篇文章主要介紹了小項(xiàng)目改造快速引入 mybatis,功能方面非常簡(jiǎn)單,考慮到開發(fā)速度,直接按 springboot 項(xiàng)目進(jìn)行開發(fā),依賴方面僅僅使用 spring-boot-starter-web, spring-boot-starter-jdbc, sqljdbc4, lombook,需要的朋友可以參考下
    2022-05-05
  • 獲取Java線程轉(zhuǎn)儲(chǔ)的常用方法(推薦)

    獲取Java線程轉(zhuǎn)儲(chǔ)的常用方法(推薦)

    這篇文章主要介紹了獲取Java線程轉(zhuǎn)儲(chǔ)的常用方法,本文給大家介紹的非常想詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-01-01
  • Java實(shí)現(xiàn)去重的方法詳解

    Java實(shí)現(xiàn)去重的方法詳解

    austin支持兩種去重的類型:N分鐘相同內(nèi)容達(dá)到N次去重和一天內(nèi)N次相同渠道頻次去重,這篇文章就來(lái)和大家講講這兩種去重的具體實(shí)現(xiàn),需要的可以參考一下
    2023-06-06
  • Springboot?MBean使用示例解析

    Springboot?MBean使用示例解析

    這篇文章主要為大家介紹了Springboot?MBean使用示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-06-06

最新評(píng)論