spring配置数据库

一.加载数据库驱动

1.利用hibernate.properties文件加载

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="locations">
    <list>
      <value>classpath:hibernate.properties</value>
    </list>
  </property>
</bean>

或者

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
  <property name="driverClassName" value="${hibernate.connection.driver_class}" />
  <property name="url" value="${hibernate.connection.url}" />
  <property name="username" value="${hibernate.connection.username}" />
  <property name="password" value="${hibernate.connection.password}" />
  <property name="initialSize" value="${hibernate.initialSize}" />
  <property name="maxActive" value="${hibernate.maxActive}" />
</bean>

hibernate.properties文件(oracle)

hibernate.connection.driver_class=oracle.jdbc.driver.OracleDriver
hibernate.connection.url=jdbc\:oracle\:thin\:@127.0.0.1\:1521/abc
hibernate.connection.username=root
hibernate.connection.password=123456

hibernate.properties文件(mysql)

hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.connection.url=jdbc\:mysql\://127.0.0.1\:3306/disc_shop?useUnicode\=true&characterEncoding\=utf8
hibernate.connection.username=root
hibernate.connection.password=123456


2.直接配置

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
<property name="username" value="roott"></property>
<property name="password" value="123456"></property>
<property name="url" value="jdbc:oracle:thin:@192.168.10.197:1521:abc"></property>
</bean>

 

 

二.配置sessionFactory对象

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <property name="hibernateProperties">
    <props>

      <!-- 配置数据库方言-->
      <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>

      <!-- 是否在控制台打印sql语句-->
      <prop key="hibernate.show_sql">true</prop>

      <!-- 是否按格式打印-->
      <prop key="hibernate.format_sql">true</prop>

      <!-- 数据库操作方式-->
      <prop key="hibernate.hbm2ddl.auto">update</prop>
      <!-- 如果是本地事务(jdbc) -->
      <prop key="hibernate.current_session_context_class">thread</prop>
    </props>
  </property>

 

  <!-- 加载hbm.xml文件      方法一-->
  <property name="mappingLocations">

    <list>
      <value>classpath:com/itany/usermgr/entity/Teacher.hbm.xml</value>
      <value>classpath:com/itany/usermgr/entity/Student.hbm.xml</value>
    </list>
  </property>

  <!--方法二 自动扫描hbm方式配置的hibernate文件和.hbm文件 -->
  <property name="mappingDirectoryLocations">
    <list>
      <value>classpath:com/itany/tds/pojo</value>
    </list>
  </property>


</bean>


转载于:https://www.cnblogs.com/JavaTWW/p/4681220.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值