2021-5-09 Hibernate在eclipse的安装

2021-5-09

在eclipse中配置Hibernate

下载

首先下载hibernate,下载地址是http://hibernate.org/orm/,之后解压下载下来的安装包,在hibernate-release-4.3.11.Final\hibernate-release-4.3.11.Final\lib\required文件目录下的所有jar文件添加在User Libernate中在这里插入图片描述

安装管理插件

eclipse->【help】 ->【eclipse marketspace】中搜索hibernate找到【JBOSS Tools】,并安装。一路默认进行安装,避免出现路径错误。合理的使用插件可以减少配置工作量。
在这里插入图片描述
之后eclipse会重启,重启之后我们在src文件下创建hibernate.cfg.xml文件。
在这里插入图片描述
配置xml文件 我的配置代码如下。

<?xml version="1.0" encoding="UTF-8"?> com.mysql.jdbc.Driver abc123 jdbc:mysql://localhost:3306/test root org.hibernate.dialect.MySQLDialect
    <property name="hibernate.c3p0.max_size">20</property>
    
    <property name="hibernate.c3p0.min_size">1</property>
  
    <property name="hibernate.c3p0.timeout">5000</property>
    
    <property name="hibernate.c3p0.max_statements">100</property>
  
    <property name="hbm2ddl.auto">update</property>

    <property name="show_sql">true</property>
  
    <property name="hibernate.format_sql">true</property>
   
    <property name="current_session_context_class">thread</property>  

  <property name="connection.characterEncoding">utf8</property> 

    <mapping class="com.model.Person"/>
    
</session-factory>

之后创建Person.java,放在com.model包里面。

package com.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Person {
private long id;
private String username;
private String password;

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
public long getId() {
	return id;
}
public void setId(long id) {
	this.id = id;
}
public String getUsername() {
	return username;
}
public void setUsername(String username) {
	this.username = username;
}
public String getPassword() {
	return password;
}
public void setPassword(String password) {
	this.password = password;
}

}

写一个hibernate的工具类DBTool

package com.tool;

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
/**

  • 获取SessionFactory
  • @author Slience

*/
public class DBTool {

private static Configuration configuration = null;
private static StandardServiceRegistryBuilder builder = null;
private static StandardServiceRegistry registry = null;

public static SessionFactory getSessionFactory() {
	configuration = new Configuration().configure();
	builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
	registry = builder.build();
	return configuration.buildSessionFactory(registry);
}

}

测试类

package com.test;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;

import com.model.Person;
import com.tool.DBTool;

public class TestHibernate {

/**
 * 测试是否可以得到SessionFactory
 */
@Test
public void getSessionFactory() {
	System.out.println(DBTool.getSessionFactory());
}

@Test
public void addPerson() {
	Person person = new Person();
	Session session = DBTool.getSessionFactory().getCurrentSession();
	Transaction transaction = session.beginTransaction();
	session.save(person);
	transaction.commit();
}

}

运行后就可以发现数据库中自动创建了一个Person说明测试成功(使用navicat可视化数据库查看)
在这里插入图片描述

遇到的问题

1.一开始发现缺少了Database explorer 在Help=>Install New Software下搜索安装即可。
2.重启后遇见了这个
在这里插入图片描述
经查询是我的jdk版本低了 在更换高版本的jdk得到了解决。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值