在eclipse使用hibernate java project

##1,hibernate jar##

{commons-collections-3.1.jar

dom4j-1.6.1.jar

hibernate-core.jar

hibernate-entitymanager.jar

javassist-3.9.0.GA.jar

jstl-1.2.jar

jta.jar

junit.jar

log4j.jar

mysql-connector-java-5.1.7-bin.jar

slf4j-api-1.7.2.jar

slf4j-log4j12-1.7.2.jar}

##2,eclipse##

{Eclipse Java EE IDE for Web Developers.

Version: Kepler Service Release 1

Build id: 20130919-0819}

##步骤##

1 新建java project [new>>java project]

2 把jar加到project里[选中项目添加 folder-命名lib]

   把上述jar复制粘贴过来,选中所有jar右键build path>>add to build path

3 新建application 类[程序入口 main函数]

4 新建Model类 

package abhibernate.Model;

public class user {
 private String name;
 private Integer id;
public String getName() {
	return name;
}
public void setName(String name) {
	this.name = name;
}
public Integer getId() {
	return id;
}
public void setId(Integer id) {
	this.id = id;
}
public void user()
{
	}
@Override
public String toString() {
	return "user [name=" + name + ", id=" + id + "]";
}
 
}

 5 新建Model类和数据表的映射文件*.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping SYSTEM "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- java对象映射数据库 -->
<hibernate-mapping package="abhibernate.Model">
	<class name="user" table="tab_user">
		<id name="id">
			<generator class="native" />
		</id>
		<property name="name" column="F_name"></property>
	</class>
	
</hibernate-mapping>

 6 新建hibernate的配置文件 [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="show_sql">true</property>
		<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="hibernate.connection.url">jdbc:mysql:///test</property>
		<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password"></property>
		<mapping resource="abhibernate/Model/user.hbm.xml"/>
	</session-factory>
</hibernate-configuration>

 notice:步骤6 <property name="hibernate.connection.url">jdbc:mysql:///test</property>中///就代表localhost,如果是远程数据库jdbc:mysql://ip:port/数据库名称

7 提供一个公共类 操作hibernate

package abhibernate.DAL;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class hibernateUtil {
	private static SessionFactory session_factory;
	static {
		try {
			session_factory = new Configuration().configure()
					.buildSessionFactory();
		} catch (Exception x) {
			x.printStackTrace();
		}

	}
	 public static Session currentSession()
	 {		  
		 return session_factory.openSession();		
	 }
	 public static void closeSession(Session session)
	 {
		 if(session!=null)
		 {
			 session.close();			 
		 }
		 
	 }
}

 8 完善你的程序入口

package abhibernate.app;

 

import abhibernate.DAL.userDB;
import abhibernate.Model.user;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class hello {
	public static void main(String[] args) {
                 Session session = hibernateUtil.currentSession();
		 Transaction tx = session.beginTransaction();
	                user ouer = new user();
		        ouer.setName("hibernate4");
			session.save(ouser);
			tx.commit();		 
			hibernateUtil.closeSession(session);
		
	 

	}
}

 9 至此你可以调试你的java project了

    以上代码中的package 可以自行设计

你可能感兴趣的:(eclipse,Hibernate,java project)