hibernate框架的配置文件的基本配置

一、配置hibernate.cfg.xml

<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <!-- 
            连接数据库的配置
         -->
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql:///ssh</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">root</property>
        <!-- 
            配置数据库方言
         -->
         <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
         <!-- 
            hbm2ddl
            #hibernate.hbm2ddl.auto create-drop
            #hibernate.hbm2ddl.auto create
            #hibernate.hbm2ddl.auto update
            #hibernate.hbm2ddl.auto validate
          -->
          <!-- 
            可选配置
            hibernate.show_sql  true 在控制台上输出SQL语句
            hibernate.format_sql  true  格式化控制台输出的SQL语句
            hibernate.connection.autocommit  true 事务是否自动提交
            hibernate.hbm2ddl.auto  create/create-drop/update/validate
            * create            :每次执行的时候,创建一个新的表.(如果以前有该表,将该表删除重新创建.) 一般测试的时候的使用.
            * create-drop   :每次执行的时候,创建一个新的表,程序执行结束后将这个表,删除掉了.  一般测试的时候使用.
            * update            :如果数据库中没有表,创建一个新的表,如果有了,直接使用这个表.可以更新表的结构.
            * validate      :会使用原有的表.完成校验.校验映射文件与表中配置的字段是否一致.不一致报错.
         -->
         <property name="hibernate.hbm2ddl.auto">update</property>

         <property name="hibernate.show_sql">true</property>
         <property name="hibernate.format_sql">true</property>

         <!-- 
         线程绑定的session
          -->
         <property name="hibernate.current_session_context_class">thread</property>
         <!-- 
            数据库连接池配置
          -->
          <!-- C3P0连接池设定-->
            <!-- 使用c3po连接池  配置连接池提供的供应商-->
            <property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider                                                                                                                                        </property>
            <!--在连接池中可用的数据库连接的最少数目 -->
            <property name="c3p0.min_size">5</property>
            <!--在连接池中所有数据库连接的最大数目  -->
            <property name="c3p0.max_size">20</property>
            <!--设定数据库连接的过期时间,以秒为单位,
            如果连接池中的某个数据库连接处于空闲状态的时间超过了timeout时间,就会从连接池中清除 -->
            <property name="c3p0.timeout">120</property>
             <!--每3000秒检查所有连接池中的空闲连接 以秒为单位-->
            <property name="c3p0.idle_test_period">3000</property>
          <!-- 
            配置映射文件
           -->
         <mapping resource="com/baidu/pojo/User.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

二、配置映射文件User.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <!-- 
        class标签name:对应实体类的全路径
        class标签table:对应的数据库表名
        id标签name:类的id作为数据库的主键
        generator标签class:native代表生成策略
        *identity       :自动增长.适合 short int long...采用数据库的自动增长机制.不适合于Oracle数据库.
        * sequence  :序列.适用于 short int long ... 应用在Oracle上 .
        * uuid      :适用于字符串类型的主键.采用随机的字符串作为主键.
        * native        :本地策略.底层数据库不同.自动选择适用identity 还是 sequence.
        * assigned      :Hibernate框架不维护主键,主键由程序自动生成.
        * foreign       :主键的外来的.(应用在多表一对一的关系.)

        property标签name:映射类中的普通属性 name:类中的属性名称
     -->
    <class name="com.baidu.pojo.User" table="user" >
        <id name="id">
            <generator class="native"></generator>
        </id>
        <property name="name"></property>
    </class>
</hibernate-mapping>

猜你喜欢

转载自blog.csdn.net/chenbingbing111/article/details/81037470