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;
     }
}

 

 

 

 

 

 

 

 

 

 

posted @ 2019-08-11 10:31  ___mouM  阅读(329)  评论(0编辑  收藏  举报