hibernate的基本配置和测试

靠,弄了半天,新手就是伤啊!!!!
下面贴出来我的配置过程和代码,供大家参考

首先要新建一个web工程,然后将mysql驱动复制到/myhibernate/WebRoot/WEB-INF/lib/mysql-connector-java-5.1.18-bin.jar中
然后就是添加hibernate支持,方法是在工程名上面右键->myeclipse->project factes->install hibernate Factes
这时会给你提示要创建一个包来放置这些库文件,然后一路确定就行了
然后就是配置文件,也就是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>

        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/test</property>
        <property name="connection.username">root</property>
        <property name="connection.password"></property>

        <!-- JDBC connection pool (use the built-in)
        <property name="connection.pool_size">1</property>
		 -->
        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>

        <!-- Enable Hibernate's automatic session context management 
        <property name="current_session_context_class">thread</property>
		-->
		
        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup
        <property name="hbm2ddl.auto">update</property>
		 -->
		 
     <!--   <mapping resource="com/bird/model/Student.hbm.xml"/> --> 
		<mapping class="com.bird.model.User"/>
    </session-factory>

</hibernate-configuration>

User.java

package com.bird.model;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class User {

	private int id;
	private String name;
	

	@Id
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	

}

TeacherTest.java

package com.bird.test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

import com.bird.model.User;


public class TeacherTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		User t = new User();
		t.setId(1223);
		t.setName("t1");
		
		
		Configuration cfg = new Configuration();
		cfg.configure();//读取配置文件
		
		ServiceRegistry serviceRegistry =new ServiceRegistryBuilder().
		applySettings(cfg.getProperties()).buildServiceRegistry();
		
		SessionFactory factory = cfg.configure().buildSessionFactory(serviceRegistry);
		
		Session session = factory.openSession();
		session.beginTransaction();
		session.save(t);
		session.getTransaction().commit();
		session.close();
		factory.close();
	}

}

发表评论

电子邮件地址不会被公开。 必填项已用*标注