zoukankan      html  css  js  c++  java
  • Hibernate配置流程

    操作数据库必须要设置数据库的连接属性:
    1. driver_class,url,username,password(hibernate.cfg.xml)
        2. 编写对象跟表之间的映射关系(类名.hbm.xml)
     
    为什么配置文件用xml?
       ①xml文件是可以通过规则文件提示的,
          xml的规则文件有两种:dtd 和 schema 文件
      任何提供xml文件的框架,基本都提供规则文件
       ③大部分框架的XML规则文件都是在核心包里面的
       ④Eclipse可以通过配置XML的规则文件生成配置文件的。而且配置了规则文件,可以实现配置文件的标签提示
     
    hibernate的配置流程图

    第一步 、 在SRC目录下新建一个hibernate.cfg.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "hibernate-configuration-3.0.dtd" >
    <hibernate-configuration>
      <session-factory>
      <property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property>
      <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/sms</property>
      <property name="hibernate.connection.username">root</property>
      <property name="hibernate.connection.password">1234</property>
      <property name="hibernate.show_sql">true</property>
      <property name="hibernate.formats_sql">true</property>
      <property name="hibernate.hbm2ddl.auto">update</property>
      <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
      <mapping resource="cn/gzsxt/hibernate/pojo/hbm/Student.hbm.xml"/>
     
      </session-factory>
    </hibernate-configuration>

    第二步  、

    public class HibernateUtils {
         
         public static final SessionFactory ssf = getSessionFactory();
         
         private static  SessionFactory  getSessionFactory() {
               
               //读取配置文件
               Configuration config = new Configuration().configure();
               //新建会话工厂
               SessionFactory sessionFactory = config.buildSessionFactory();
               
               return sessionFactory;
         }
         
         public static Session getSession() {
               Session session = ssf.openSession();
               return session;
         }
    }

  • 相关阅读:
    理解cookie和session机制
    http协议中connection头的作用
    [转]使用Wireshark来检测一次HTTP连接过程
    http协议学习系列
    xcode清空project list
    How To Use Git Source Control with Xcode in iOS 6
    iOS开发XCODE5 SVN配置 使用办法
    iOS申请证书,Certificates, Identifiers &Profiles 简介
    C#学习单向链表和接口 IList<T>
    halcon学习笔记——(7)HALCON标定后的二维测量
  • 原文地址:https://www.cnblogs.com/aknife/p/11334132.html
Copyright © 2011-2022 走看看