1.首先建立一个java项目或者jsp动态网页项目
将Hibernate4\Hibernate4\hibernate-release-4.3.11.Final\hibernate-release-4.3.11.Final\lib\required文件夹下的10个jar包导入项目中,再将mySQL的驱动包导入并Build Path
2.再将Hibernate的核心配置文件hibernat.cfg.xml导入src文件夹下
并修改配置文件
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&#61;"connection.driver_class">com.mysql.jdbc.Driverproperty><property name&#61;"connection.url">jdbc:mysql://localhost:3306/hibernate_demo02property><property name&#61;"connection.username">rootproperty><property name&#61;"connection.password">123456property><property name&#61;"dialect">org.hibernate.dialect.MySQL5Dialectproperty><property name&#61;"show_sql">trueproperty><property name&#61;"hbm2ddl.auto">updateproperty><mapping resource&#61;"com/oracle/model/Person.hbm.xml"/>session-factory>hibernate-configuration>
倒数第三行的mapping标签为映射&#xff0c;映射model层中某一个类所对应的xml配置文件&#xff0c;并且采用地址的形式&#xff0c;resource&#61;“../../../xxx.hbm.xml" &#xff08;是斜杠不是点&#xff09;
3.创建HibernateUtil
package com.oracle.util;import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;public class HibernateUtil {private static final SessionFactory sessionFactory&#61;buildSessionFactory();private static SessionFactory buildSessionFactory(){Configuration configuration&#61;new Configuration().configure(); //实例化配置文件ServiceRegistry serviceRegistry&#61;new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build(); //实例化服务登记return configuration.buildSessionFactory(serviceRegistry); //获取Session工厂
}public static SessionFactory getSessionFactory(){return sessionFactory;}
}
4.开始创建model层表结构
首先是一个Person类
package com.oracle.model;import java.util.HashSet;
import java.util.Set;public class Person {private Integer id;private String realName;private String sex;public Person() {super();}public Integer getId() {return id;}public void setId(Integer id) {this.id &#61; id;}public String getRealName() {return realName;}public void setRealName(String realName) {this.realName &#61; realName;}public String getSex() {return sex;}public void setSex(String sex) {this.sex &#61; sex;}public Set
}
然后创建Person对应的xml配置文件Person.hbm.xml&#xff0c;该文件的路径必须被写入核心配置文件中的mapping标签中&#xff0c;否则将无法映射
xml version&#61;"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&#61;"com.oracle.model"> <class name&#61;"Person" table&#61;"t_person"><id name&#61;"id" column&#61;"id"><generator class&#61;"native">generator>id><property name&#61;"realName" column&#61;"real_name" length&#61;"30">property><property name&#61;"sex" column&#61;"sex" length&#61;"10">property>class>
hibernate-mapping>
package com.oracle.service; 运行之前必须现在SQLyog中创建hibernate.cfg.xml中对应的数据库&#xff0c;此例中为hibernate_demo02 然后直接运行程序&#xff0c;如果没有表&#xff0c;则会逆向由java实体自动创建一张数据库表
5.测试
创建测试类
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;public class Demo {public static void main(String[] args) {//首先实例化核心配置文件hibernate.cfg.xmlConfiguration configuration&#61;new Configuration().configure();//实例化服务登记&#xff0c;&#xff0c;复制粘贴ServiceRegistry serviceRegistry&#61;new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build(); //创建Session工厂SessionFactory sessionFactory&#61;configuration.buildSessionFactory(serviceRegistry); Session session&#61;sessionFactory.openSession(); //生成一个sessionsession.beginTransaction(); //开启事务//先运行程序&#xff0c;生产数据库表
session.getTransaction().commit(); //提交事务session.close(); //关闭sessionsessionFactory.close(); //关闭session工厂
}}