我是借助于 dbunit-maven-plugin 来使用 DbUnit 的,数据库是 MySql,在使用 mvn dbunit:export 指令时,总报错:
Embedded error: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'desc from unmi_test_table order by Id' at line 1
若是加上 -e 或 -X 参数,如 mvn dbunit:export -e 时可以看到更详细的异常信息:
Caused by: org.apache.maven.plugin.MojoExecutionException: Error executing export
at org.codehaus.mojo.dbunit.ExportMojo.execute(ExportMojo.java:136)
at org.apache.maven.plugin.DefaultPluginManager.executeMojo(DefaultPluginManager.java:490)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:694)
... 17 more
Caused by: org.dbunit.dataset.DataSetException: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an e
tax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'desc from unmi_tes
at line 1
at org.dbunit.database.DatabaseDataSet.getTable(DatabaseDataSet.java:316)
为了这个问题,见到这样的错误提示,第一反应就是怎么会生成 desc from unmi_test_table 这样的语句,按理它在取字段时应该就是用的 desc unmi_test_table 才对,为何中间插个 from 进来,几乎要去 step into 看在何处生成的这个 SQL,而且还以 order by Id 结尾,更是不可思议。
曾经反复的组合 dbunit-maven-plugin、DbUnit 和 MySql 驱动的版本,都故障依然。可是回到家里试了下 DbUnit 来 export 却是很顺利的,到公司同样的 pom.xml 确通不过。反复的思考,意识到该表中有个段名为 desc,那可是个 sql 指令,且在用 MySql Wordbench 建表时也有警告,但我还是不顾一切的赶鸭子上了轿。
于是把 desc 字段改成 description 后,mvn dbunit:export 成功。
同样的,MySql 的其他关键字也让它们强作字段名,在 export 也会报错,像 'key from unmi_test_table order by Id' 这样的错了。return 等其他关键字也不行,其实这点 Oracle 可没有这么温和,它就是禁止使用 desc 作为字段名。
另对于关键字,如 desc、key、return 等作了关键字时,查询语句要写成:
select `desc` from .............. /* 用斜撇号引起来 */
还有种情况,今天晚上意外而有幸的遇到,即使在表 unmi_test_table 中使用了 desc 作为字段名,如果你在 pom.xml 的 dbunit-maven-plugin 插件配置中指定了表明的话,像:
<tables>
<table>
<name>unmi_test_table</name>
</table>
</tables>
那么执行 mvn dbunit:export 时也能平平安安。
关于 dbunit-maven-plugin 详细的用法,请参考我前一篇日志:用 dbunit-maven-plugin 来管理你的测试数据
本文链接 http://unmi.cc/dbunit-export-from-exception, 来自 隔叶黄莺 Unmi Blog
[版权声明]本站内文章,如未特别注明,均系原创或翻译之作,本人 Unmi 保留一切权利。本站原创及译作未经本人许可,不得用于商业用途及传统媒体。网络媒体可随意转载,或以此为基础进行演译,但务必以链接形式注明原始出处和作者信息,否则属于侵权行为。另对本站转载他处文章,俱有说明,如有侵权请联系本人,本人将会在第一时间删除侵权文章。及此说明,重之之重。
相关文章: | 随机文章: |