Intellij IDEA 14中使用MyBatis-generator 自动生成MyBatis代码
参考: http://blog.****.net/sunny243788557/article/details/45166397
Intellij IDEA 14 作为Java IDE 神器,接触后发现,非常好用,对它爱不释手,打算离开eclipse和myeclipse,投入Intellij IDEA的怀抱。
然而在使用的过程中会发现Intellij IDEA也有一些不尽如意的地方,难免会有些不爽:Intellij IDEA 的插件库远不及eclipse的丰富。 mybatis-generator在eclipse中有专门的插件,而没有开发出Intellij IDEA能够使用的插件。
不过不用灰心,如果你的项目是使用maven组织的,那么我们可以在Intellij IDEA中使用 mybatis-generator-maven-plugin插件来完成MyBatis model 和Mapper文件的自动生成。
STEP 0.在Intellij IDEA创建maven项目(本过程比较简单,略)
STEP 1. 在maven项目的pom.xml 添加mybatis-generator-maven-plugin 插件
- <build>
- <finalName>xxx</finalName>
- <plugins>
- <plugin>
- <groupId>org.mybatis.generator</groupId>
- <artifactId>mybatis-generator-maven-plugin</artifactId>
- <version>1.3.2</version>
- <configuration>
- <verbose>true</verbose>
- <overwrite>true</overwrite>
- </configuration>
- </plugin>
- </plugins>
- </build>
STEP 2. 在maven项目下的src/main/resources 目录下建立名为 generatorConfig.xml的配置文件,作为mybatis-generator-maven-plugin 插件的执行目标,模板如下:(标红的需要替换为自己项目的包或者表名)
<?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> <!--导入属性配置 --> <properties resource="generator.properties"></properties> <!--指定特定数据库的jdbc驱动jar包的位置 --> <classPathEntry location="${jdbc.driverLocation}"/> <context id="default" targetRuntime="MyBatis3"> <!-- optional,旨在创建class时,对注释进行控制 --> <commentGenerator> <property name="suppressDate" value="true"/> <property name="suppressAllComments" value="true"/> </commentGenerator> <!--jdbc的数据库连接 --> <jdbcConnection driverClass="${jdbc.driverClass}" connectionURL="${jdbc.connectionURL}" userId="${jdbc.userId}" password="${jdbc.password}"> </jdbcConnection> <!-- 非必需,类型处理器,在数据库类型和java类型之间的转换控制--> <javaTypeResolver > <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类 targetPackage 指定生成的model生成所在的包名 targetProject 指定在该项目下所在的路径 --> <javaModelGenerator targetPackage="com.neolix.vehicle.model" targetProject="src/main/java(java包如果没有的话,需要创建java包)"> <!-- 是否对model添加 构造函数 --> <property name="constructorBased" value="true"/> <!-- 是否允许子包,即targetPackage.schemaName.tableName --> <property name="enableSubPackages" value="false"/> <!-- 建立的Model对象是否 不可改变 即生成的Model对象不会有 setter方法,只有构造方法 --> <property name="immutable" value="true"/> <!-- 给Model添加一个父类 可以不需要 --> <property name="rootClass" value="com.neolix.vehicle.model.BaseEntity"/> <!-- 是否对类CHAR类型的列的数据进行trim操作 --> <property name="trimStrings" value="true"/> </javaModelGenerator> <!--Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 --> <sqlMapGenerator targetPackage="com.neolix.vehicle.mapper" targetProject="src/main/java"> <property name="enableSubPackages" value="false"/> </sqlMapGenerator> <!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码 type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象 type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象 type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口 --> <javaClientGenerator targetPackage="com.neolix.vehicle.dao" targetProject="src/main/java" type="MIXEDMAPPER"> <property name="enableSubPackages" value=""/> <!-- 定义Maper.java 源代码中的ByExample() 方法的可视性,可选的值有: public; private; protected; default 注意:如果 targetRuntime="MyBatis3",此参数被忽略 --> <property name="exampleMethodVisibility" value=""/> <!-- 方法名计数器 Important note: this property is ignored if the target runtime is MyBatis3. --> <property name="methodNameCalculator" value=""/> <!-- 为生成的接口添加父接口 --> <property name="rootInterface" value=""/> </javaClientGenerator> <!--对应数据库表 mysql可以加入主键自增 字段命名 忽略某字段等--> <table schema="vehicle_network" tableName="park" domainObjectName="Park" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"> <property name="useActualColumnNames" value="false"/> <generatedKey column="ID" sqlStatement="DB2" identity="true" /> <columnOverride column="DATE_FIELD" property="startDate" /> <ignoreColumn column="FRED" /> <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" /> </table> </context> </generatorConfiguration>
这里使用了外置的配置文件generator.properties,可以将一下属性配置到properties文件之中,增加配置的灵活性:
jdbc.driverLocation=D:\\mysql-connector-java-5.1.38.jar jdbc.driverClass=com.mysql.jdbc.Driver jdbc.connectionURL=jdbc:mysql://181.92.228.140:3306/vehicle_network jdbc.userId=test jdbc.password=123456项目目录如下:
STEP 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 选项,会把具体的详细错误信息打印出来的,根据错误信息修改即可
相关推荐
- 使用Mybatis-Generator自动生成相关文件1、相关文件
- IntelliJ IDEA的自动生成注释及一些常用配置,简单快捷键的使用
- SSM框架通过mybatis-generator自动生成代码
- SSM框架通过mybatis-generator自动生成代码
- 在IDEA中利用MyBatis-generator逆向生成MyBatis代码
- Maven插件-mybatis-generator(mybatis自动生成实体代码的插件)
- 使用mybatis-generator-core自动生成代码所遇到的问题
- IDEA中配置mybatis-generator的自动生成
- Idea配置mybatis-generator自动生成代码
- IntelliJ IDEA相关小技巧和插件 | Mybatis自动生成工具
- IntelliJ IDEA常用方法(1)
- mybatis generator 自动生成代码插件