在eclipse中配置Hibernate

配置环境: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中配置Hibernate_第1张图片

之后在eclipse->【help】 ->【eclipse marketspace】中搜索hibernate找到【JBOSS Tools】,并安装好,安装的时候只需要选择hibernate中的相关插件就可以了

在eclipse中配置Hibernate_第2张图片

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

在eclipse中配置Hibernate_第3张图片

之后输入相关数据库链接的信息即可,这个xml文件的内容我是这样设置的




    
        com.mysql.jdbc.Driver
        abc123
        jdbc:mysql://localhost:3306/test
        root
        org.hibernate.dialect.MySQLDialect
       
        20
        
        1
        
        5000
        
        100
        
        update
        
        true
        
        true
        
        thread  
	
      utf8 
    	    
        
        
    


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

在eclipse中配置Hibernate_第4张图片



你可能感兴趣的:(Hibernate)