今天使用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.orgat org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:8)at org.apache.ibatis.session.defaults.DefaultSqlSessionFactory.openSessionFromDataSource(DefaultSqlSessionFactory.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.orgat 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:64)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(DefaultSqlSessionFactory.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檔案夾下了。