Oracle 数据源的配置。在Spring环境中


第一种 采用JDBC配置文件。


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc"
	xmlns:ehcache="http://www.springmodules.org/schema/ehcache"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:flex="http://www.springframework.org/schema/flex"
	xsi:schemaLocation="       
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd       
      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd       
      http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd       
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd       
      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
      http://www.springframework.org/schema/flex
      http://www.springframework.org/schema/flex/spring-flex-1.0.xsd
      http://www.springmodules.org/schema/ehcache http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd"
	default-autowire="byName">
	
	
	<context:annotation-config />
	
	<!-- 导入属性配置文件 --> 
	<context:property-placeholder location="/WEB-INF/conf/jdbc.properties" />
	<!-- 数据库的配置 ,dbcp连接池 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
	    <property name="driverClassName">
	      <value>${jdbc.driverClassName}</value>  
	   </property>
	    <property name="url">
	      <value>${jdbc.url}</value>  
	   </property>
	    <property name="username">
	      <value>${jdbc.username}</value>  
	   </property>
	    <property name="password">
	      <value>${jdbc.password}</value>  
	   </property>
	   <property name="maxActive"><value>50</value></property>  
       <property name="maxIdle"><value>20</value></property> 
       <property name="maxWait"><value>5000</value></property>	   
	</bean>	

	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>

	<!-- 需要事务控制的业务方法配置 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="get*" read-only="true" />
			<tx:method name="find*" read-only="true" />
			<tx:method name="query*" read-only="true" />
			<tx:method name="select*" read-only="true" />
			<tx:method name="count*" read-only="true" />
			<tx:method name="*" rollback-for="Exception" />
		</tx:attributes>
	</tx:advice>

	<!-- 事务控制拦截器 -->
	<aop:config proxy-target-class="true">
		<aop:advisor
			pointcut="execution(* *..service.impl.*(..)) or execution(* *..service..*ServiceImpl.*(..))"
			advice-ref="txAdvice" />
	</aop:config>
	<bean id="sqlSessionFactory"
		class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="configLocation"
			value="/WEB-INF/conf/mybatis/mybatis-config.xml" />
		<property name="mapperLocations"
			value="classpath*:/org/topcheer/biz/sys/xml/*.xml" />
		<property name="dataSource" ref="dataSource" />
	</bean>

</beans>



第二种 : JNDI:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc"
	xmlns:ehcache="http://www.springmodules.org/schema/ehcache"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:flex="http://www.springframework.org/schema/flex"
	xsi:schemaLocation="       
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd       
      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd       
      http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd       
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd       
      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
      http://www.springframework.org/schema/flex
      http://www.springframework.org/schema/flex/spring-flex-1.0.xsd
      http://www.springmodules.org/schema/ehcache http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd"
	default-autowire="byName">
	
	
	<context:annotation-config />
	

	<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
		<property name="jndiName">
			<value>KINGHONG</value>
		</property>
	</bean>
	
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource">
			<ref bean="dataSource" />
		</property>
	</bean>
	
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>

	<!-- 需要事务控制的业务方法配置 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="get*" read-only="true" />
			<tx:method name="find*" read-only="true" />
			<tx:method name="query*" read-only="true" />
			<tx:method name="select*" read-only="true" />
			<tx:method name="count*" read-only="true" />
			<tx:method name="*" rollback-for="Exception" />
		</tx:attributes>
	</tx:advice>

	<!-- 事务控制拦截器 -->
	<aop:config proxy-target-class="true">
		<aop:advisor
			pointcut="execution(* *..service.impl.*(..)) or execution(* *..service..*ServiceImpl.*(..))"
			advice-ref="txAdvice" />
	</aop:config>
	<bean id="sqlSessionFactory"
		class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="configLocation"
			value="/WEB-INF/conf/mybatis/mybatis-config.xml" />
		<property name="mapperLocations"
			value="classpath*:/org/topcheer/biz/sys/xml/*.xml" />
		<property name="dataSource" ref="dataSource" />
	</bean>


	

</beans>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring框架配置Oracle数据源通常涉及以下几个步骤: 1. 添加依赖:首先,在你的项目添加Spring JDBC和Oracle JDBC驱动的依赖。对于Maven项目,可以在pom.xml文件加入如下内容: ```xml <dependency> <groupId>com.oracle.database.jdbc</groupId> <artifactId>ojdbc10</artifactId> <version>19.3.0.0</version> <!-- 根据实际版本替换 --> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> ``` 2. 配置application.properties或application.yml:在配置文件定义Oracle数据源的相关信息,例如URL、用户名、密码等。 ```properties spring.datasource.url=jdbc:oracle:thin:@//hostname:port/service_name spring.datasource.username=your_username spring.datasource.password=your_password spring.datasource.driver-class-name=com.oracle.jdbc.OracleDriver ``` 3. 创建数据源bean:在Spring配置,通过`@Bean`注解创建`JdbcTemplate`或`DataSource` bean,注入配置好的数据源。 ```java @Configuration public class DataSourceConfig { @Value("${spring.datasource.url}") private String url; @Value("${spring.datasource.username}") private String username; @Value("${spring.datasource.password}") private String password; @Bean public DataSource dataSource() { DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setUrl(url); dataSource.setUsername(username); dataSource.setPassword(password); return dataSource; } } ``` 4. 使用数据源:现在你可以通过@Autowired注解在其他服务或Repository层引用这个数据源,开始对Oracle数据库进行操作了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值