当前位置: 移动技术网 > IT编程>开发语言>Java > eclipse下搭建hibernate5.0环境的步骤(图文)

eclipse下搭建hibernate5.0环境的步骤(图文)

2019年07月19日  | 移动技术网IT编程  | 我要评论

东方风神录ph,江泽惠,潘玮柏晒搞怪合照

本文介绍了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

点击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配置文件内容如下

<?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>
    
    <!-- 配置数据库方言 -->
    <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() {
  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("在广工毕业");
    n.settitle("毕业季");
    sess.save(n);
    transaction.commit();
    sess.close();
    
  }
}

经过测试成功

或者通过映射文件

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

测试验证成功。

整个工程架构如图:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网