Spring配置数据库固定代码

时间:2023-11-28 17:57:44
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" >
<property name="driverClassName">
<!--<value>com.microsoft.sqlserver.jdbc.SQLServerDriver</value>-->
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<!-- <value>jdbc:sqlserver://192.168.2.186:1433;DatabaseName=test2013</value> -->
<value>jdbc:mysql://127.0.0.1:3306/ssh4</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>root</value>
</property>
</bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 数据源的设定 -->
<property name="dataSource">
<ref local="dataSource"/>
</property>
<property name="mappingResources">
<list>
<value>com/zhangyue/bean/ZYCStudent.hbm.xml</value>
<value>com/zhangyue/bean/ZYCCourse.hbm.xml</value>
<value>com/zhangyue/bean/ZYCStudentCourse.hbm.xml</value>
<value>com/zhangyue/bean/ZYCStudentFile.hbm.xml</value>
</list>
</property> <property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
<!--org.hibernate.dialect.SQLServerDialect-->
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>

上面是MySQL版本(SQLServer版本):

下面是Oracle版本:

 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" >
 <property name="driverClassName">
 <value>oracle.jdbc.driver.OracleDriver</value>
 </property>
 <property name="url">
  <value>jdbc:oracle:thin:@192.168.1.21:1521:orcl</value>
 </property>
 <property name="username">
  <value>test</value>
 </property>
 <property name="password">
  <value>test</value> 
 </property>
 </bean>
 
 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
 <!-- 数据源的设定 -->
 <property name="dataSource">
 <ref local="dataSource"/>
 </property>
 <property name="mappingResources">
  <list>
   <value>com/test/bean/User.hbm.xml</value>
  </list>
 </property>
 
 <property name="hibernateProperties">
 <props>
  <prop key="hibernate.dialect">
   org.hibernate.dialect.OracleDialect
  </prop>
  <prop key="hibernate.show_sql">
   true
  </prop>
 </props>
 </property>
 </bean>