0.在Intellij IDEA创建maven项目
1. 在maven项目的pom.xml 添加mybatis-generator-maven-plugin 插件
xxx org.mybatis.generator mybatis-generator-maven-plugin 1.3.2 true true
2. 在maven项目下的src/main/resources 目录下建立名为 generatorConfig.xml的配置文件,作为mybatis-generator-maven-plugin 插件的执行目标,模板如下:
这里使用了外置的配置文件generator.properties,可以将一下属性配置到properties文件之中,增加配置的灵活性:
jdbc.driverLocation=D:\\maven\\com\\oracle\\ojdbc14\\10.2.0.4.0\\ojdbc14-10.2.0.4.0.jar jdbc.driverClass=oracle.jdbc.driver.OracleDriver jdbc.connectionURL=jdbc:oracle:thin:@//localhost:1521/XE jdbc.userId=LOUIS jdbc.password=123456
项目目录如下:
3. 在Intellij IDEA添加一个“Run运行”选项,使用maven运行mybatis-generator-maven-plugin插件 :
之后弹出运行配置框,为当前配置配置一个名称,这里其名为"generator",然后在 “Command line” 选项中输入“mybatis-generator:generate -e”
这里加了“-e ”选项是为了让该插件输出详细信息,这样可以帮助我们定位问题
如果添加成功,则会在run 选项中有“generator” 选项,如下:
点击运行,然后不出意外的话,会在控制台输出:
C:\Java\jdk1.7.0_71\bin\java -Dmaven.home=D:\software\apache-maven-3.0.5-bin -Dclassworlds.conf=D:\software\apache-maven-3.0.5-bin\bin\m2.conf -Didea.launcher.port=7533 "-Didea.launcher.bin.path=D:\applications\JetBrains\IntelliJ IDEA 14.0.2\bin" -Dfile.encoding=UTF-8 -classpath "D:\software\apache-maven-3.0.5-bin\boot\plexus-classworlds-2.4.jar;D:\applications\JetBrains\IntelliJ IDEA 14.0.2\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain org.codehaus.classworlds.Launcher -Didea.version=14.0.2 -s D:\software\apache-maven-3.0.5-bin\conf\settings.xml mybatis-generator:generate -e [INFO] Error stacktraces are turned on. [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building hometutor Maven Webapp 1.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- mybatis-generator-maven-plugin:1.3.2:generate (default-cli) @ hometutor --- [INFO] Connecting to the Database [INFO] Introspecting table louis.lession log4j:WARN No appenders could be found for logger (org.mybatis.generator.internal.db.DatabaseIntrospector). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. [INFO] Generating Example class for table LOUIS.LESSION [INFO] Generating Record class for table LOUIS.LESSION [INFO] Generating Mapper Interface for table LOUIS.LESSION [INFO] Generating SQL Map for table LOUIS.LESSION [INFO] Saving file LessionMapper.xml [INFO] Saving file LessionExample.java [INFO] Saving file Lession.java [INFO] Saving file LessionMapper.java [WARNING] Root class com.foo.louis.Hello cannot be loaded, checking for member overrides is disabled for this class [WARNING] Existing file E:\sources\tutor\src\main\java\org\louis\hometutor\po\Lession.java was overwritten [WARNING] Existing file E:\sources\tutor\src\main\java\com\foo\tourist\dao\LessionMapper.java was overwritten [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.334s [INFO] Finished at: Tue Jan 27 12:04:08 CST 2015 [INFO] Final Memory: 8M/107M [INFO] ------------------------------------------------------------------------ Process finished with exit code 0
看到BUILD SUCCESS,则大功告成,如果有错误的话,由于添加了-e 选项,会把具体的详细错误信息打印出来的,根据错误信息修改即可