eclipse下使用meavn搭建SSM框架,使用junit4测试出现无法获取jdbc驱动类的异常。弄了好久,真心求助,谢谢。

qq_24807769 2016-03-05 12:52:28
以下是异常信息:
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class 'oracle.jdbc.driver.OracleDriver '
### The error may exist in file [D:\Program Files\java_work\graduation\target\classes\com\neusoft\mrdong\mapping\UserMapper.xml]
### The error may involve com.neusoft.mrdong.dao.UserMapper.selectByPrimaryKey
### The error occurred while executing a query
### Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class 'oracle.jdbc.driver.OracleDriver '
at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:75)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:371)
at com.sun.proxy.$Proxy17.selectOne(Unknown Source)
at org.mybatis.spring.SqlSessionTemplate.selectOne(SqlSessionTemplate.java:163)
at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:68)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:52)
at com.sun.proxy.$Proxy18.selectByPrimaryKey(Unknown Source)
at com.neusoft.mrdong.service.impl.UserServiceImpl.getUsers(UserServiceImpl.java:18)
at com.mrdong.test.dao.TestMyBatis.test1(TestMyBatis.java:25)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:232)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:175)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

--------------------------------------------------------------------------------------------------------------------------------------------
以下是spring-mybatis.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!-- 自动扫描 -->
<context:component-scan base-package="com.neusoft.mrdong" />
<!-- 引入配置文件 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties" />
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- 初始化连接大小 -->
<property name="initialSize" value="${jdbc.initialSize}"></property>
<!-- 连接池最大数量 -->
<property name="maxActive" value="${jdbc.maxActive}"></property>
<!-- 连接池最大空闲 -->
<property name="maxIdle" value="${jdbc.maxIdle}"></property>
<!-- 连接池最小空闲 -->
<property name="minIdle" value="${jdbc.minIdle}"></property>
<!-- 获取连接最大等待时间 -->
<property name="maxWait" value="${jdbc.maxWait}"></property>
</bean>

<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations" value="classpath:com/neusoft/mrdong/mapping/*.xml"></property>
</bean>

<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.neusoft.mrdong.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>

<!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>

--------------------------------------------------------------------------------------------------------------------------------------------
以下是UserMapper.xml配置信息:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.neusoft..UserMapper" >
<resultMap id="BaseResultMap" type="test.user_t.User" >
<id column="ID" property="id" jdbcType="DECIMAL" />
<result column="USER_NAME" property="userName" jdbcType="VARCHAR" />
<result column="PASSWORD" property="password" jdbcType="VARCHAR" />
<result column="AGE" property="age" jdbcType="DECIMAL" />
</resultMap>
<sql id="Base_Column_List" >
ID, USER_NAME, PASSWORD, AGE
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Long" >
select
<include refid="Base_Column_List" />
from USER_T
where ID = #{id,jdbcType=DECIMAL}
</select>
</mapper>

--------------------------------------------------------------------------------------------------------------------------------------------
以下是jdbc.properties配置信息:
jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@localhost:1521:xe
jdbc.username=EASYONE
jdbc.password=123456
jdbc.initialSize=0
jdbc.maxActive=20
jdbc.maxIdle=20
jdbc.minIdle=1
jdbc.maxWait=100



--------------------------------------------------------------------------------------------------------------------------------------------
以下是测试类信息:
package com.mrdong.test.dao;


import org.apache.log4j.Logger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.alibaba.fastjson.JSON;
import com.neusoft.mrdong.dto.User;
import com.neusoft.mrdong.service.IUserService;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring-mybatis.xml"})
public class TestMyBatis {
private static Logger logger = Logger.getLogger(TestMyBatis.class);
@Autowired
private IUserService userService = null;

@Test
public void test1() {

User user = userService.getUsers();
logger.info(JSON.toJSONString(user));
}
}

--------------------------------------------------------------------------------------------------------------------------------------------
自己弄了几天都没有解决,很苦恼,网上找了不少方法都没有好用,我觉得应该就是一个小地方出现问题,但是刚刚接触实在能力,经验都有限,无法找出出问题的原因。
求各位前辈抽出一点点时间帮帮我,万分感谢。
...全文
528 点赞 收藏 14
写回复
14 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
link151 2017-07-26

仔细看oracleDriver后面多了一个空格,'oracle.jdbc.driver.OracleDriver '
回复
qq_24807769 2016-03-13
引用 8 楼 zcsdnj的回复:
我之前也是这个错误,是jdbc.properties出错了,可能这里多个空格什么都看不出来的
是这个问题,非常感谢。真的很难找啊。。。
回复
qq_24807769 2016-03-13
引用 8 楼 zcsdnj的回复:
我之前也是这个错误,是jdbc.properties出错了,可能这里多个空格什么都看不出来的
是jdbc文件里吗?
回复
Intboy 2016-03-09
代码结构是怎么个?贴一下,是不是单元测试的时候没加载到jdbc.properties
回复
zcsdnj 2016-03-09
jdbc.properties这里你自己一个个打上去看看
回复
zcsdnj 2016-03-09
我之前也是这个错误,是jdbc.properties出错了,可能这里多个空格什么都看不出来的
回复
qq_24807769 2016-03-06
引用 4 楼 小白晒太阳的回复:
mysql 驱动包有吗 你把你的userService的代码也贴出来看看
驱动都在的。我在用其他方法搭一个试试。
回复
小白晒太阳 2016-03-05
http://download.csdn.net/download/alane1986/4033714 下个jar包
回复
小白晒太阳 2016-03-05
mysql 驱动包有吗 你把你的userService的代码也贴出来看看
回复
小白晒太阳 2016-03-05
哦,不好意思,看错了,你这个是oracle
回复
qq_24807769 2016-03-05
引用 1 楼 Tro_picana 的回复:
<mapper namespace="com.neusoft..UserMapper" >

多了一个点?com.neusoft.UserMapper


现在还是原来的错误,
回复
qq_24807769 2016-03-05
引用 1 楼 小白晒太阳的回复:
<mapper namespace="com.neusoft..UserMapper" > 多了一个点?com.neusoft.UserMapper
这个是我复制的失误,包括下的,还原错版本了。不好意思。我现在调试出现如下图的错误:
回复
小白晒太阳 2016-03-05
<mapper namespace="com.neusoft..UserMapper" > 多了一个点?com.neusoft.UserMapper
回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2016-03-05 12:52
社区公告
暂无公告