• hibernate配置


    hibernate可以由xml配置和properties文件配置,这里讲一下properties配置。

    与xml配置相识,将hibernate.properties放在主程序下,系统会自动调用。properties、中书写属于cfg.xml的内容

    demo:

    hibernate.dialect=net.sf.hibernate.dialect.MySQLDialect
    hibernate.connection.driver_class=com.mysql.jdbc.Driver
    hibernate.connection.url=jdbc:mysql://localhost:3306/demo
    hibernate.connection.username=root
    hibernate.connection.password=123
    hibernate.show_sql=true

    这时候要取得数据库表的映射文件,需要这样:

    Configuration cfg=new Configuration().addClass("com.beans.User.class");

    这时才会加载User表的映射文件,然后可以取得sessionFactory:

    SessionFactory sessionFactory=cfg.buildSessionFactory();

    你也可以使用xml配置hibernate的配置文件

    在src目录下加入hibernate.cfg.xml,具体内容我就不多说了。

    当这样配置之后,获取SessionFactory:

    SessionFactory sessionFactory=new Configuration().configure().buildSessionFactory();

    其实,hibernate的配置文件命名为hibernate.cfg.xml只是一个默认,系统默认读取罢了。如果hibernate.cfg.xml命名为aaa.cfg.xml,怎么取读这个配置文件呢?

    SessionFactory sessionFactory=new Configuration().configure("aaa.cfg.xml").buildSessionFactory();

    注意:xml配置和properties配置可以同时使用,当这种情况的时候,xml配置中的配置会覆盖properties配置中的相同属性

  • 相关阅读:
    变量,基本数据类型
    编程语言分类,Python介绍、IDE集成开发环境,注释
    Django之Cookie,Session
    第三章
    第二章
    第一章
    php面向对象(文件操作)
    php面向对象(目录操作)
    php有关类和对象的相关知识2
    php有关类和对象的相关知识1
  • 原文地址:https://www.cnblogs.com/aigeileshei/p/5400598.html
Copyright © 2020-2023  润新知