Hibernate测试手记
数据库MySQL,测试类Student
1,下载Hibernate
http://sourceforge.net/projects/hibernate/files/hibernate3/3.6.7.Final/
从sourceforge的下载统计来看,这个包应该是最通用的。
2,下载SLF4J
http://www.slf4j.org/download.html
3,
1)在Eclipse创建引入Hibernate的基础Library
注意:slf4j-nop在slf4j的解压目录下,hibernate-jpa-2.0在hibernate解压目录lib/jpa下(对于3.6版,这包必须引入,否则会报错javax persistence EntityListeners),其余处hibernate3.jar以外的包都在lib/required目录下。
2)引入JDBC,同时做好做好数据库准备。
4,编写hibernate配置文件hibernate.cfg.xml放于src目录下
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<!--这里没有写对会报Could not parse configuration: /hibernate.cfg.xml错误-->
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">creatbox</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.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="tk/creatbox/hibernate/model/Student.hbm.xml" />
</session-factory>
</hibernate-configuration>
5,测试准备
1),实体类准备Student.java
package tk.creatbox.hibernate.model;
public class Student {
private int id;
private String name;
private int age;
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;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
2),关联文件准备
Student.hbm.xml(一般和实体类放在同一目录下)
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="tk.creatbox.hibernate.model">
<class name="Student">
<id name="id"></id>
<property name="name"></property>
<property name="age"></property>
</class>
</hibernate-mapping>
6,编写测试类,完成测试
测试类:StudentTest
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import tk.creatbox.hibernate.model.Student;
public class StudentTest {
/**
* @param args
*/
public static void main(String[] args) {
Student s = new Student();
s.setId(1);
s.setName("s1");
s.setAge(1);
Configuration cfg = new Configuration();
SessionFactory sf = cfg.configure().buildSessionFactory();
Session session = sf.openSession();
session.beginTransaction();
session.save(s);
session.getTransaction().commit();
session.close();
sf.close();
}
}