hibernate连接oracle数据库注意事项

时间:2022-02-16 15:04:32

1.     导入oracle-jar包(在oracle安装目录中找到jar包,推荐ojdbc14.jar)

 hibernate连接oracle数据库注意事项

 

2.     hibernate中修改配置文件,注意端口号为1521

<!-- Hibernate配置文件 -->

<hibernate-configuration>

    <session-factory>

       <!-- 数据库驱动 -->

       <propertyname="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>

       <!-- 数据库连接的URL -->

       <propertyname="connection.url">jdbc:oracle:thin:@localhost:1521:lianxi</property>

       <!-- 数据库连接用户名 -->

       <propertyname="connection.username">scott</property>

       <!-- 数据库连接密码 -->

       <propertyname="connection.password">tiger</property>

       <!-- Hibernate方言 -->

       <propertyname="dialect">org.hibernate.dialect.OracleDialect</property>

       <!-- 打印SQL语句 -->

       <propertyname="show_sql">true</property>

      

       <!-- 映射文件  -->

       <mappingresource="com/sichuang/oa/bean/User.hbm.xml"/>

      

 

 

3.      在spring配置文件中修改

<!-- 配置连接数据库的数据源 -->

           <beanid="dataSource"class="com.mchange.v2.c3p0.ComboPooledDataSource"

                  destroy-method="close">

                 <propertyname="driverClass"value="oracle.jdbc.driver.OracleDriver"/>

                 <propertyname="jdbcUrl"value="jdbc:oracle:thin:@localhost:1521:lianxi"/>

                 <propertyname="user"value="scott"/>

                 <propertyname="password"value="tiger"/>

           </bean>

            

             <!-- 配置SessionFactory --> 

           <beanid="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

                <propertyname="dataSource"ref="dataSource"/>

                <propertyname="mappingResources">

                  <list>

                    <value>com/sichuang/oa/bean/User.hbm.xml</value>

                   

                  </list>

                </property>

                <propertyname="hibernateProperties">

                  <value>

                      hibernate.dialect=org.hibernate.dialect.OracleDialect

                      hibernate.hbm2ddl.auto=update

                      hibernate.show_sql=false

                      hibernate.format_sql=false

                    </value>

               </property>

           </bean>

 

4.     在实体类的配置文件中修改,注意在oracle数据中id号不能自动增长,必修要创建“序列sequence”

create sequence SEQ_USER_ID;(建议的序列名:seq_数据库名_主键名)

 

配置:id主键       

<idname="id"unsaved-value="null">   

            <generatorclass="sequence">

              <paramname="sequence">SEQ_USER_ID(所创建的序列)</param>

            </generator>

        </id>