1.配置
到http://hibernate.org/orm/下载hibernate包然后解压
在eclipse中新建一个java project,如名为hibernate_test
再所建工程中新建一个文件夹libs
将\lib\required下的所有jar包,(另外数据库驱动的jar包,我使用mysql所以导入了mysql-connector-5.1.26-bin.jar,另外还有做测试用的junit包) 复制到libs文件夹下
将所有的jar包选中右键build path->add to path
再将\project\etc下的hibernate.cfg.xml和log4j.properties复制到src下
配置完成
2.
案例
<!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.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql:///test</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">959511586</property> <mapping resource="com/kotar/entity/Student.hbm.xml"/> </session-factory> </hibernate-configuration> |
1)在hibernate.cfg.xml中添加如下,配置数据库(红色部分是数据库中创建好的的schema)
2)定义持久化类
package com.kotar.entity; import java.io.Serializable; public class Student implements Serializable { /** * */ private static final long serialVersionUID = 1L; private int id; private String name; private int age; public Student() { // TODO Auto-generated constructor stub } 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; } } |
配置对象关系映射文件(Student.hbm.xml)持久化类和数据库表的对应关系
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.kotar.entity.Student" table="stu_tab"> <id name="id" column="stu_id"> <generator class="native"></generator> </id> <property name="name" column="stu_name"></property> <property name="age" column="stu_age"></property> </class> </hibernate-mapping> |
3)在hibernate.cfg.xml中注册对象关系映射文件
<mapping resource="com/kotar/entity/Student.hbm.xml"/>
4)编写测试类,对数据库操作
package com.kotar.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.Test;
import com.kotar.entity.Student;
public class StudentTest {
@Test
public void createTable() {
Configuration cfg=new Configuration().configure();
SchemaExport se=new SchemaExport(cfg);
se.create(true, true);
}
@Test
public void insert(){
Configuration cfg=new Configuration().configure();
StandardServiceRegistryBuilder ssrb=new StandardServiceRegistryBuilder().applySettings(cfg.getProperties());
StandardServiceRegistry serviceRegistry= ssrb.build();
SessionFactory sessionFactory=cfg.buildSessionFactory(serviceRegistry);
//获取Session
Session session=sessionFactory.openSession();
//获取事务
org.hibernate.Transaction transaction=session.beginTransaction();
//输入数据
Student student=new Student();
student.setAge(17);
student.setName("kotar");
try {
session.save(student);
transaction.commit();
} catch (Exception e) {
try {
transaction.rollback();
} catch (IllegalStateException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}finally {
session.clear();
}
}
}
先后用junit运行测试方法createTable()和insert()
结果如下