eclipse下怎么搭建hibernate5.0环境

发布时间:2021-02-20 12:32:09 作者:小新
来源:亿速云 阅读:152

这篇文章给大家分享的是有关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配置文件内容如下

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

测试验证成功。

整个工程架构如图:

eclipse下怎么搭建hibernate5.0环境

感谢各位的阅读!关于“eclipse下怎么搭建hibernate5.0环境”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

推荐阅读:
  1. 在eclipse上面搭建hbase环境
  2. Eclipse下如何搭建Hadoop2.7.0开发环境

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

eclipse hibernate5.0

上一篇:如何搭建vue2单元测试环境

下一篇:centos上怎么快速搭建ghost博客

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》