eclipse下怎么搭建hibernate5.0环境-创新互联

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




  
    com.mysql.jdbc.Driver
    a123
    jdbc:mysql://localhost:3306/tb_test
    sherman
    org.hibernate.dialect.MySQLDialect
    
    
  

注意,把 < session-factory name ="MySQL" > 的name属性去掉,否则报org.hibernate.engine.jndi.JndiException异常,在该文件中添加一些配置,如图:




  
    com.mysql.jdbc.Driver
    a123
    jdbc:mysql://localhost:3306/tb_test
    sherman
    
    
    org.hibernate.dialect.MySQL5Dialect
    
    true
    
    true
    
    update
    
    20
    
    
    
  

在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





  
    
      
      
    
    
      
    
    
      
    
  

在hibernate.cfg.xml中配置

测试验证成功。

整个工程架构如图:

eclipse下怎么搭建hibernate5.0环境

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


网站标题:eclipse下怎么搭建hibernate5.0环境-创新互联
网站地址:http://myzitong.com/article/dgooec.html