近期由於項目須要,調研了myBatis 3.x的使用,固然,順便也就研究了一下使用Generator來經過逆向工程生成pojo,mapper等文件。使用這個工具以前,要先下載相關的jar包,我使用的是最新的mybatis-generator-core-1.3.2.jar。
下面將generatorConfig.xml列出來:
html
<?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
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包,修改後的語句以下:
mysql
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
web