zoukankan      html  css  js  c++  java
  • hibernate配置文件

    1、配置的方式:
    •     hibernate.properties
    •     hibernate.cfg.xml
    •     编码方式创建Configuration对象
        
        hibernate.properties 
        采用这种方式配置时:文件配置时可以参考hibernate发布包中projectetc下hibernate.properties文件,但该文件没有添加持久化类的方式,所以必须调用Configuration对象的addAnnotatedClass()或addPackage()添加持久化类。
      
     Configuration cfg = new Configuration().addAnnotatedClass(Item.class)
    .addAnnotatedClass(Bid.class);

       实际开发中一般不用,因为当类很多时,工作量会很大。

     
        hibernate.cfg.xml
        在配置文件里加了持久化类
               Configuration cfg = new Configuration().configure();
       
        编程方式加(很少用)
                // 实例化Configuration,不加载任何配置文件
            Configuration conf = new Configuration()
                // 通过addAnnotatedClass()方法添加持久化类
                .addAnnotatedClass(org.crazyit.app.domain.News.class)
                // 通过setProperty设置Hibernate的连接属性。
                .setProperty("hibernate.connection.driver_class"
                    , "com.mysql.jdbc.Driver")
                .setProperty("hibernate.hbm2ddl.auto" , "update");
     
    2、常用配置属性
         
      <!-- 指定连接数据库所用的驱动 -->
            <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
            <!-- 指定连接数据库的url,其中hibernate是本应用连接的数据库名 -->
            <property name="connection.url">jdbc:mysql://localhost/hibernate</property>
            <!-- 指定连接数据库的用户名 -->
            <property name="connection.username">root</property>
            <!-- 指定连接数据库的密码 -->
            <property name="connection.password">32147</property>
            <!-- 指定连接池里最大连接数 -->
            <property name="hibernate.c3p0.max_size">20</property>
            <!-- 指定连接池里最小连接数 -->
            <property name="hibernate.c3p0.min_size">1</property>
            <!-- 指定连接池里连接的超时时长 -->
            <property name="hibernate.c3p0.timeout">5000</property>
            <!-- 指定连接池里最大缓存多少个Statement对象 -->
            <property name="hibernate.c3p0.max_statements">100</property>
            <property name="hibernate.c3p0.idle_test_period">3000</property>
            <property name="hibernate.c3p0.acquire_increment">2</property>
            <property name="hibernate.c3p0.validate">true</property>
            <!-- 指定数据库方言 -->
            <property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
            <!-- 根据需要自动创建数据表 -->
            <property name="hbm2ddl.auto">update</property><!---->
            <!-- 显示Hibernate持久化操作所生成的SQL -->
            <property name="show_sql">true</property>
            <!-- 将SQL脚本进行格式化后再输出 -->
            <property name="hibernate.format_sql">true</property>
            <!-- 罗列所有持久化类的类名 -->
            <mapping class="org.crazyit.app.domain.News"/>
            
    View Code

    Hibernate乱码问题解决:  

    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/databaseName?useUnicode=true&amp;characterEncoding=UTF-8</property>

    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/databaseName?characterEncoding=UTF-8</property>

     
  • 相关阅读:
    IServiceBehavior, IOperationBehavior,IParameterInspector
    System.IO.Pipelines——高性能IO(三)
    System.IO.Pipelines——高性能IO(二)
    System.IO.Pipelines——高性能IO(一)
    背包问题 —— 四种解法解题
    波音,自动驾驶bug未修复,致346人丧生!5个月内两次坠毁!其中,包括8名中国公民
    2018年Java生态行业报告
    为什么大公司一定要使用DevOps?
    设计微服务的最佳实践
    Spring Boot面试题
  • 原文地址:https://www.cnblogs.com/goingforward/p/5750452.html
Copyright © 2011-2022 走看看