一、异常描述
在eclipse中通过mybatis-generator生成dao、mappers及model代码时(步骤参考“通过mybatis-generator-core工具自...”页面),报“java.sql.SQLException: Cannot connect to database (possibly bad driver/URL combination)”异常,错误轨迹如下
java.sql.SQLException: Cannot connect to database (possibly bad driver/URL combination)@b@ at org.mybatis.generator.internal.db.ConnectionFactory.getConnection(ConnectionFactory.java:71)@b@ at org.mybatis.generator.config.Context.getConnection(Context.java:498)@b@ at org.mybatis.generator.config.Context.introspectTables(Context.java:408)@b@ at org.mybatis.generator.api.MyBatisGenerator.generate(MyBatisGenerator.java:221)@b@ at org.mybatis.generator.api.MyBatisGenerator.generate(MyBatisGenerator.java:132)@b@ at com.xwood.gen.GeneratorExecuter.generator(GeneratorExecuter.java:24)@b@ at com.xwood.gen.GeneratorExecuter.main(GeneratorExecuter.java:29)
二、解决办法
1.从异常描述信息直接可以看出是generator.xml的jdbcConnection配置文件配错了
2.关于jdbcConnection配置,mysql的配置应该如下,"connectionURL="jdbc:mysql://192.168.1.9:3306/test"
<jdbcConnection driverClass="com.mysql.jdbc.Driver" @b@ connectionURL="jdbc:mysql://192.168.1.9:3306/test" @b@ userId="root" @b@ password="123456"> @b@ </jdbcConnection>
3.详细generator.xml参考如下
<?xml version="1.0" encoding="UTF-8"?>@b@<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">@b@<generatorConfiguration>@b@ @b@ <!-- classPathEntry:数据库的JDBC驱动--> @b@ <classPathEntry location="C:\WS\mybatis-generator-core-1.3.1\lib\mysql-connector-java-3.2.0-alpha-bin.jar" /> @b@ @b@ <context id="DB2Tables" targetRuntime="MyBatis3">@b@ @b@ <!-- 去除自动生成的注释 -->@b@ <commentGenerator><property name="suppressAllComments" value="true" /></commentGenerator> @b@ @b@ <jdbcConnection driverClass="com.mysql.jdbc.Driver" @b@ connectionURL="jdbc:mysql://192.168.1.9:3306/test" @b@ @b@ userId="root" @b@ password="123456"> @b@ </jdbcConnection>@b@ @b@ <javaTypeResolver > @b@ <property name="forceBigDecimals" value="false" /> @b@ </javaTypeResolver>@b@ @b@ <!-- targetProject:自动生成model代码的位置 --> @b@ <javaModelGenerator targetPackage="com.xwood.gen.model" targetProject="C:\WS\NJ\project\xwood-project\JavaTest\src\"> @b@ <property name="enableSubPackages" value="true" /> @b@ <property name="trimStrings" value="true" /> @b@ </javaModelGenerator>@b@ @b@ <!-- targetProject:自动生成mappers代码的位置 --> @b@ <sqlMapGenerator targetPackage="com.xwood.gen.mappers" targetProject="C:\WS\NJ\project\xwood-project\JavaTest\src\"> @b@ <property name="enableSubPackages" value="true" /> @b@ </sqlMapGenerator>@b@ @b@ <!-- targetProject:自动生成dao代码的位置 --> @b@ <javaClientGenerator type="XMLMAPPER" targetPackage="com.xwood.gen.dao" targetProject="C:\WS\NJ\project\xwood-project\JavaTest\src\"> @b@ <property name="enableSubPackages" value="true" /> @b@ </javaClientGenerator>@b@ @b@ @b@ <!-- tableName:用于自动生成代码的数据库表;domainObjectName:对应于数据库表的javaBean类名-->@b@ <table tableName="guser" domainObjectName="Guser" >@b@ @b@ </table> @b@ @b@ </context> @b@ @b@</generatorConfiguration>
三、数据库oracle配置错误
原来错误配置如下,完整配置请参考“更多页”详情
<jdbcConnection driverClass="oracle.jdbc.OracleDriver" @b@ connectionURL="jdbc\:oracle\:thin\:@127.0.0.1\:1521\:oracle" @b@ userId="testa" @b@ password="123456"> @b@ </jdbcConnection>
应该修改为下
<jdbcConnection driverClass="oracle.jdbc.OracleDriver" @b@ connectionURL="jdbc:oracle:thin:@127.0.0.1:1521:oracle" @b@ userId="testa" @b@ password="123456"> @b@ </jdbcConnection>