Hibernate的简单流程
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="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/hibernate_demo02</property> <property name="connection.username">root</property> <property name="connection.password">123456</property> <!-- 方言 --> <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> <!-- 控制台显示SQL --> <property name="show_sql">true</property> <!-- 自动更新表结构 --> <property name="hbm2ddl.auto">update</property> <mapping resource="com/oracle/model/Person.hbm.xml"/> </session-factory> </hibernate-configuration>
倒数第三行的mapping标签为映射,映射model层中某一个类所对应的xml配置文件,并且采用地址的形式,resource=“../../../xxx.hbm.xml" (是斜杠不是点)
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=buildSessionFactory(); private static SessionFactory buildSessionFactory(){ Configuration configuration=new Configuration().configure(); //实例化配置文件 ServiceRegistry serviceRegistry=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 = id; } public String getRealName() { return realName; } public void setRealName(String realName) { this.realName = realName; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public Set<Car> getCars() { return cars; } public void setCars(Set<Car> cars) { this.cars = cars; } }
然后创建Person对应的xml配置文件Person.hbm.xml,该文件的路径必须被写入核心配置文件中的mapping标签中,否则将无法映射
<?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="com.oracle.model"> <class name="Person" table="t_person"> <id name="id" column="id"> <generator class="native"></generator> </id> <property name="realName" column="real_name" length="30"></property> <property name="sex" column="sex" length="10"></property> </class> </hibernate-mapping>
<hibernate-mapping>中package属性为映射所在的包名
<class>中name属性为映射的类名,table为该简单java类在数据库中对应的表名
<id>name属性为类中对应的变量名,column是对应数据库中的字段名,generator标签是生成策略,设置为native的意思是使该列成为主键并且自增
<property>为属性标签,用来映射类中变量对应数据库中的字段
5.测试
创建测试类
package com.oracle.service; 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.xml Configuration configuration=new Configuration().configure(); //实例化服务登记,,复制粘贴 ServiceRegistry serviceRegistry=new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build(); //创建Session工厂 SessionFactory sessionFactory=configuration.buildSessionFactory(serviceRegistry); Session session=sessionFactory.openSession(); //生成一个session session.beginTransaction(); //开启事务 //先运行程序,生产数据库表 session.getTransaction().commit(); //提交事务 session.close(); //关闭session sessionFactory.close(); //关闭session工厂 } }
运行之前必须现在SQLyog中创建hibernate.cfg.xml中对应的数据库,此例中为hibernate_demo02
然后直接运行程序,如果没有表,则会逆向由java实体自动创建一张数据库表