hibernate原理和流程

来源:互联网 发布:淘宝联盟怎么刷流量 编辑:程序博客网 时间:2024/06/06 14:12


开发步骤:




创建配置对象Configuration读取配置文件hibernate.cfg.xml,获得配置对象Configuration创建sessionFactory读取对象关系映射文件,获得sessionFactory对象创建session对象(类似于JDBC的connection对象),获得session对象相对于创建数据库连接对象,开启事务,执行session的各种方法。




步骤:

1 创建hibernate配置文件

<session-factory>    <property name="connection.username">root</property>    <property name="connection.password">123456</property>    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>    <property name="connection.url">jdbc:mysql:///hibernate?useUnicode=true&characterEncoding=UTF-8</property>    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>      <property name="show_sql">true</property>   <property name="format_sql">true</property>   <property name="hbm2ddl.auto">create</property>       </session-factory>


2 创建持久化类

public class Students {// JavaBeans设计原则// 1 公有的类// 2 提供公有的不带参数的默认的构造函数// 3 属性私有// 4 属性setter/getter封装private int sid;private String sname;private String gender;private Date birthday;private String address;public Students() {}public Students(int sid, String sname, String gender, Date birthday, String address) {// super();this.sid = sid;this.sname = sname;this.gender = gender;this.birthday = birthday;this.address = address;}public int getSid() {return sid;}public void setSid(int sid) {this.sid = sid;}public String getSname() {return sname;}public void setSname(String sname) {this.sname = sname;}public String getGender() {return gender;}public void setGender(String gender) {this.gender = gender;}public Date getBirthday() {return birthday;}public void setBirthday(Date birthday) {this.birthday = birthday;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}@Overridepublic String toString() {return "Students [sid=" + sid + ", sname=" + sname + ", gender=" + gender + ", birthday=" + birthday+ ", address=" + address + "]";}}



3 创建对象--关系映射文件

利用create hibernate XML Mapping File,自动生成.hbm.xml文档

<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><!-- Generated 2016-2-24 13:39:23 by Hibernate Tools 3.5.0.Final --><hibernate-mapping>    <class name="hibernate_001.Students" table="STUDENTS">        <id name="sid" type="int">            <column name="SID" />            <generator class="assigned" />        </id>        <property name="sname" type="java.lang.String">            <column name="SNAME" />        </property>        <property name="gender" type="java.lang.String">            <column name="GENDER" />        </property>        <property name="birthday" type="java.util.Date">            <column name="BIRTHDAY" />        </property>        <property name="address" type="java.lang.String">            <column name="ADDRESS" />        </property>    </class></hibernate-mapping>


生成文档后,加入到hibernate配置文档中

<mapping resource="hibernate_001/Students.hbm.xml"/>



4 访问数据库

                //创建配置对象Configuration config = new Configuration().configure();//创建服务注册对象ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();//创建会话工厂sessionFactory = config.buildSessionFactory(serviceRegistry);//会话对象session = sessionFactory.openSession();//开启事务transaction = session.beginTransaction();





0 0