现在的位置: 首页 > 综合 > 正文

使用Maven和Mybatis开发时,遇到“java.sql.SQLException: No suitable driver found for http://maven.apache.org”

2013年08月28日 ⁄ 综合 ⁄ 共 4502字 ⁄ 字号 评论关闭

今天使用Maven和Mybatis,在Eclipse上开发j2ee的程序。本人刚学Mybatis,对Maven还算熟悉,看过书,用的时间不长。本想试下Mybatis的selectList,结果程序编译通过,运行时报错:

org.apache.ibatis.exceptions.PersistenceException: 
### Error opening session.  Cause: java.sql.SQLException: No suitable driver found for http://maven.
apache.org
### Cause: java.sql.SQLException: No suitable driver found for http://maven.apache.org
	at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:8)
	at org.apache.ibatis.session.defaults.DefaultSqlSessionFactory.openSessionFromDataSource(DefaultSql
SessionFactory.java:83)
	at org.apache.ibatis.session.defaults.DefaultSqlSessionFactory.openSession(DefaultSqlSessionFactory
.java:32)
	at databaseAdapter.CollegeAdapter.selectCollege(CollegeAdapter.java:28)
	at temp.RunSqlSession.main(RunSqlSession.java:30)
Caused by: java.sql.SQLException: No suitable driver found for http://maven.apache.org
	at java.sql.DriverManager.getConnection(DriverManager.java:602)
	at java.sql.DriverManager.getConnection(DriverManager.java:185)
	at org.apache.ibatis.datasource.unpooled.UnpooledDataSource.getConnection(UnpooledDataSource.java:6
4)
	at org.apache.ibatis.datasource.pooled.PooledDataSource.popConnection(PooledDataSource.java:349)
	at org.apache.ibatis.datasource.pooled.PooledDataSource.getConnection(PooledDataSource.java:55)
	at org.apache.ibatis.session.defaults.DefaultSqlSessionFactory.openSessionFromDataSource(DefaultSql
SessionFactory.java:73)
	... 3 more

郁闷!

之前见过SQLException报错,说ClassNotFound的或者链接打不开的,可就是没见过

 java.sql.SQLException: No suitable driver found for http://maven.apache.org

的,我重新把java代码看了一遍,并没有在java代码中发现http://maven.apache.org,使用Maven的mvn clean compile一遍,发现问题依旧,猛然心想,去target里看看Mybatis的configuration配置文件吧,结果找到原因了!

我的Mybatis的配置文件源码如下:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
	<properties>
	  <property name="url" value="jdbc:postgresql://localhost/mydb3"/>
	  <property name="driver" value="org.postgresql.Driver"/>
	  <property name="username" value="1234"/>
	  <property name="password" value="1234"/>
	</properties>
	
	<typeAliases>
		<typeAlias alias="College" type="pojo.College"/>	
	</typeAliases>

	<environments default="development">
	  <environment id="development">
	    <transactionManager type="JDBC"/>
	    <dataSource type="POOLED">
	      <property name="driver" value="${driver}"/>
	      <property name="url" value="${url}"/>
	      <property name="username" value="${username}"/>
	      <property name="password" value="${password}"/>
	    </dataSource>
	  </environment>
	</environments>
	
	<mappers>
	  <mapper resource="mybatis/mapper/CollegeMapper.xml"/>
	</mappers>
</configuration>

可经过Maven的mvn compile之后,变成了:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
	<properties>
	  <property name="url" value="jdbc:postgresql://localhost/mydb3"/>
	  <property name="driver" value="org.postgresql.Driver"/>
	  <property name="username" value="1234"/>
	  <property name="password" value="1234"/>
	</properties>
	
	<typeAliases>
		<typeAlias alias="College" type="pojo.College"/>	
	</typeAliases>

	<environments default="development">
	  <environment id="development">
	    <transactionManager type="JDBC"/>
	    <dataSource type="POOLED">
	      <property name="driver" value="${driver}"/>
	      <property name="url" value="http://maven.apache.org"/>
	      <property name="username" value="${username}"/>
	      <property name="password" value="${password}"/>
	    </dataSource>
	  </environment>
	</environments>
	
	<mappers>
	  <mapper resource="mybatis/mapper/CollegeMapper.xml"/>
	</mappers>
</configuration>

发现问题了吧?

是的,maven把Mybatis的${url}替换成http://maven.apache.org了!

为什么呢?

在maven的compile之前,有一个阶段(phase)是process-resources,这时resources插件会把src/main/resources里的xml用到的${XXX}变量替换成设定的值,而url值碰巧在项目pom文件中定义了:

<modelVersion>4.0.0</modelVersion>
<groupId>cn.edu.sdu.ise.leesonlog.lms4g</groupId>
<artifactId>lms4g-website</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>lms4g-website Maven Webapp</name>
<url>http://maven.apache.org</url>

又碰巧,我把Mybatis的配置文件放到src/main/resouces目录底下了……然后悲剧就发生了。

如何解决:

不让maven过滤resources目录下的Mybatis配置文件,及其mapper文件!

修改工程的POM文件:

<resources>
	<!-- 对resources目录进行过滤,主要是设置bin目录里的内容 -->
	<resource>
		<directory>${project.basedir}/src/main/resources</directory>
		<filtering>true</filtering>
		<excludes>
			<exclude>mybatis/**/*</exclude>
		</excludes>
	</resource>
	<!-- 不过滤resources/mybatis目录,否则容易破坏mybatis的一些xml配置 -->
	<resource>
		<directory>${project.basedir}/src/main/resources</directory>
		<filtering>false</filtering>
		<includes>
			<include>mybatis/**/*</include>
		</includes>
	</resource>
</resources>

让maven不过滤mybatis文件夹下的东西就可以了。

注意:我的mybatis的配置文件被我放到src/main/resouces/mybatis文件夹下了。

抱歉!评论已关闭.