您的位置:首页 > 编程语言 > Java开发

在eclipse中配置Hibernate

2016-03-21 14:52 519 查看
配置环境:win7+eclipse4.5.1+hibernate4.3+mysql5.7.6

首先下载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】,并安装好,安装的时候只需要选择hibernate中的相关插件就可以了



之后会要求eclipse重启,重启之后我们在src文件下创建hibernate.cfg.xml文件,如图



之后输入相关数据库链接的信息即可,这个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">abc123</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 指定连接池中的最大连接数 -->
<property name="hibernate.c3p0.max_size">20</property>
<!-- 指定连接池中最小连接数 -->
<property name="hibernate.c3p0.min_size">1</property>
<!-- 指定连接池中连接的超时时长 -->
<property name="hibernate.c3p0.timeout">5000</property>
<!-- 指定连接池里最大缓存多少个Statement对象 -->
<property name="hibernate.c3p0.max_statements">100</property>
<!-- 设置:根据需要自动创建(更新)数据库表 -->
<property name="hbm2ddl.auto">update</property>
<!-- 显示Hibernate持久化操作所生成的SQL -->
<property name="show_sql">true</property>
<!-- 将SQL脚本进行格式化后再输出 -->
<property name="hibernate.format_sql">true</property>
<!-- 罗列所有持久化类的类名 ,在这里就是我们的entity-->
<property name="current_session_context_class">thread</property>
<!-- 设置连接数据库的编码格式 -->
      <property name="connection.characterEncoding">utf8</property> 
<!-- 注意:mapping要放在property之后 -->
<mapping class="com.model.Person"/>

</session-factory>
</hibernate-configuration>


之后创建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.java

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();
}
}


之后运行addPerson就可以发现数据库中自动创建了一个Person

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: