标签:
近期因为项目需要,调研了myBatis 3.x的使用,当然,顺便也就研究了一下使用Generator来通过逆向工程生成pojo,mapper等文件。使用这个工具之前,要先下载相关的jar包,我使用的是最新的mybatis-generator-core-1.3.2.jar。
下面将generatorConfig.xml列出来:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" > <generatorConfiguration > <context id="context1" > <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8" userId="root" password="" /> <javaModelGenerator targetPackage="com.lanx.test.domain" targetProject="/home/user/workspace/mybatis-web/src/main/java" /> <sqlMapGenerator targetPackage="com.lanx.test.mapper" targetProject="/home/user/workspace/mybatis-web/src/main/resources/" /> <javaClientGenerator targetPackage="com.lanx.test.mapper" type="XMLMAPPER" targetProject="/home/user/workspace/mybatis-web/src/main/java" /> <table domainObjectName="Person" tableName="PERSON" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false" > <property name="modelOnly" value="false"/> </table> </context> </generatorConfiguration>
我将这个xml文件放置在了项目根目录下,接下来参考的是一个类似官方的文档:
http://generator.sturgeon.mopaas.com/running/runningFromCmdLine.html
从命令行运行 MyBatis Generator
按照文档中所说,执行:
java -jar /home/user/backup/mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml 出现下列错误: Exception in thread "main" java.lang.RuntimeException: Exception getting JDBC Driver at org.mybatis.generator.internal.db.ConnectionFactory.getDriver(ConnectionFactory.java:85) at org.mybatis.generator.internal.db.ConnectionFactory.getConnection(ConnectionFactory.java:54) at org.mybatis.generator.config.Context.getConnection(Context.java:526) at org.mybatis.generator.config.Context.introspectTables(Context.java:436) at org.mybatis.generator.api.MyBatisGenerator.generate(MyBatisGenerator.java:222) at org.mybatis.generator.api.ShellRunner.main(ShellRunner.java:117) Caused by: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver at java.net.URLClassLoader$1.run(URLClassLoader.java:372) at java.net.URLClassLoader$1.run(URLClassLoader.java:361) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:360) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:344) at org.mybatis.generator.internal.ObjectFactory.internalClassForName(ObjectFactory.java:150) at org.mybatis.generator.internal.ObjectFactory.externalClassForName(ObjectFactory.java:121) at org.mybatis.generator.internal.db.ConnectionFactory.getDriver(ConnectionFactory.java:82) ... 5 more
其实在运行前就已经估计到了,显然并没有引入mysql的驱动程序jar包,修改后的语句如下:
java -cp /home/user/backup/mybatis-generator-core-1.3.2.jar:/home/user/backup/mysql-connector-java-5.1.36.jar org.mybatis.generator.api.ShellRunner -configfile generatorConfig.xml
运行后,顺利生成了pojo等文件,要注意generatorConfig.xml文件中javaModelGenerator等几个节点targetProject属性的配置,在这里我都写了绝对路径。实际上,如果此配置出错,书写了不存在的文件夹,会出现警告提示。
而使用java程序运行MyBatis Generator 1.3.x,直接copy此网站上给出的代码即可。
参考url如下:http://generator.sturgeon.mopaas.com/running/runningWithJava.html
最后,我的开发环境:ubuntu kylin + eclipse-Luna-SR2 + jdk8
从命令行及java程序运行MyBatis Generator 1.3.x生成MyBatis3.x代码
标签:
原文地址:http://my.oschina.net/ramboo/blog/506268