Mybatis使用JNDI配置数据源

applicationContext.xml

<?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:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-3.2.xsd 
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
	<!-- sqlSessinFactory -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
	<!-- 加载mybatis的配置文件 -->
	<property name="configLocation" value="classpath:sqlMapConfig.xml" />
	<!-- 数据源 -->
	<property name="dataSource" ref="dataSource" />
	</bean>
	
	<!-- 加载配置文件 -->
	<!-- <context:property-placeholder location="classpath:db.properties"/>	 -->
	<!--数据源,使用DBCP-->
	<!-- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
	<property name="driverClassName" value="${jdbc.driver}" />
			<property name="url" value="${jdbc.url}" />
			<property name="username" value="${jdbc.username}" />
			<property name="password" value="${jdbc.password}" />
			<property name="maxActive" value="10" />
			<property name="maxIdle" value="5" />
	</bean> -->

	<!--数据源 使用JNDI配置 -->
	<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
		<property name="jndiName">
			<value>java:comp/env/jdbc/pending</value>
		</property>
	</bean>

	<!-- mapper批量扫描,从mapper包中扫描出mapper接口,自动创建代理对象并且在spring容器中注册 
		遵循规范:将mapper.java和mapper.xml映射文件名称保持一致,且在一个目录 中
		自动扫描出来的mapper的bean的id为mapper类名(首字母小写)
	-->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
	 <!-- 指定扫描的包名 如果扫描多个包,每个包中间使用半角逗号分隔   -->
		<property name="basePackage" value="com.test.mapper"/>
	
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
	</bean> 
 
 	<!-- mapper接口配置: MapperFactoryBean根据mapper接口生成代理对象  -->
	 <!--
	<bean id="AdminMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
		<property name="mapperInterface" value="com.test.mapper.AdminMapper"></property>
		<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
	</bean>
 	-->	
	
 	<!-- 不扫描包含Controller的文件 --> 
	<context:component-scan base-package="com.test">
		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
		<context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
	</context:component-scan>
	
</beans>

server.xml 

  <GlobalNamingResources>
    <!-- Editable user database that can also be used by
         UserDatabaseRealm to authenticate users
    -->
    <Resource auth="Container" driverClassName="oracle.jdbc.driver.OracleDriver" maxActive="100" maxIdle="30" maxWait="5000" name="jdbc/pending" password="password" type="javax.sql.DataSource" url="jdbc:oracle:thin:@192.168.1.152:1521:wps" username="system"/>
  </GlobalNamingResources>

context.xml

<ResourceLink global="jdbc/pending" name="jdbc/pending" type="javax.sql.DataSource"/> 

补充一下 写法太多太乱 

在WebContent/META-INF目录下新建一个context.xml

将server.xml中的<Resource>直接放到context.xml中即可

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值