在intellij idea中使用BuildGenerator类产生dao、model
2017-03-02 22:48
218 查看
继上篇在intellij idea中使用mybatis插件产生dao、model使用mybatis插件产生了dao、model以后,发现使用原生插件生成不了java类的文档注释,又google了一番,参考了网上的资源改造了一下,与之不同的是我使用的BuildGenerator生成的dao、model。
1、下载mybatis-generator-core-1.3.2源码,链接是我已经重命名过了的,导入idea工程里面;
2、主要增加了一个CommentGeneratorSupport类,具体可以参考源码;
3、接着把修改后的源码重新打包jar添加到maven本本仓库,新建maven命令,命名为mybatis-generator-core-support,选择当前项目目录为Working Directory,再Command line输入
Profile栏目里面输入
4、新建BuildGenerator类,内容如下
generatorConfig.xml我是放在跟类同一目录,以下是generatorConfig.xml内容
最后运行BuildGenerator类成功生成dao、model等文件。
1、下载mybatis-generator-core-1.3.2源码,链接是我已经重命名过了的,导入idea工程里面;
2、主要增加了一个CommentGeneratorSupport类,具体可以参考源码;
3、接着把修改后的源码重新打包jar添加到maven本本仓库,新建maven命令,命名为mybatis-generator-core-support,选择当前项目目录为Working Directory,再Command line输入
clean install -Dmaven.test.skip=true
Profile栏目里面输入
install
点击OK,然后再点击Run->Run 'mybatis-generator-core-support',有以下信息出现说明已经成功打包好jar包文件并且提交到maven本地仓库 正在生成/Users/terrence/Downloads/mybatis-generator-core-support/target/apidocs/deprecated-list.html... 正在构建所有类的索引... 正在生成/Users/terrence/Downloads/mybatis-generator-core-support/target/apidocs/allclasses-frame.html... 正在生成/Users/terrence/Downloads/mybatis-generator-core-support/target/apidocs/allclasses-noframe.html... 正在生成/Users/terrence/Downloads/mybatis-generator-core-support/target/apidocs/index.html... 正在生成/Users/terrence/Downloads/mybatis-generator-core-support/target/apidocs/overview-summary.html... 正在生成/Users/terrence/Downloads/mybatis-generator-core-support/target/apidocs/help-doc.html... 注: 可能覆盖将来的标准标记的定制标记: @mbggenerated。为了避免出现可能的覆盖, 请在定制标记名称中至少使用一个句点字符 (.)。 注: 找不到的定制标记: @mbggenerated 3 个警告 [WARNING] Javadoc Warnings [WARNING] /Users/terrence/Downloads/mybatis-generator-core-support/src/main/java/org/mybatis/generator/internal/CommentGeneratorSupport.java:20: 警告 - @描述:是未知标记。 [WARNING] /Users/terrence/Downloads/mybatis-generator-core-support/src/main/java/org/mybatis/generator/internal/CommentGeneratorSupport.java:20: 警告 - @作者:是未知标记。 [WARNING] /Users/terrence/Downloads/mybatis-generator-core-support/src/main/java/org/mybatis/generator/internal/CommentGeneratorSupport.java:20: 警告 - @创建时间:是未知标记。 [INFO] Building jar: /Users/terrence/Downloads/mybatis-generator-core-support/target/mybatis-generator-core-1.3.2-support-javadoc.jar [INFO] [INFO] --- maven-jar-plugin:2.3.2:jar (default-jar) @ mybatis-generator-core --- [INFO] Building jar: /Users/terrence/Downloads/mybatis-generator-core-support/target/mybatis-generator-core-1.3.2-support.jar [INFO] [INFO] --- maven-site-plugin:3.0:attach-descriptor (attach-descriptor) @ mybatis-generator-core --- [INFO] [INFO] --- maven-install-plugin:2.3.1:install (default-install) @ mybatis-generator-core --- [INFO] Installing /Users/terrence/Downloads/mybatis-generator-core-support/target/mybatis-generator-core-1.3.2-support.jar to /Users/terrence/.m2/repository/org/mybatis/generator/mybatis-generator-core/1.3.2-support/mybatis-generator-core-1.3.2-support.jar [INFO] Installing /Users/terrence/Downloads/mybatis-generator-core-support/pom.xml to /Users/terrence/.m2/repository/org/mybatis/generator/mybatis-generator-core/1.3.2-support/mybatis-generator-core-1.3.2-support.pom [INFO] Installing /Users/terrence/Downloads/mybatis-generator-core-support/target/mybatis-generator-core-1.3.2-support-javadoc.jar to /Users/terrence/.m2/repository/org/mybatis/generator/mybatis-generator-core/1.3.2-support/mybatis-generator-core-1.3.2-support-javadoc.jar [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 20.086 s [INFO] Finished at: 2017-03-02T22:12:03+08:00 [INFO] Final Memory: 33M/357M [INFO] ------------------------------------------------------------------------ [WARNING] The requested profile "install" could not be activated because it does not exist. Process finished with exit code 0
4、新建BuildGenerator类,内容如下
import org.mybatis.generator.api.MyBatisGenerator; import org.mybatis.generator.config.Configuration; import org.mybatis.generator.config.xml.ConfigurationParser; import org.mybatis.generator.exception.InvalidConfigurationException; import org.mybatis.generator.exception.XMLParserException; import org.mybatis.generator.internal.DefaultShellCallback; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; public class BuildGenerator { public static void main(String[] args) { try { List<String> warnings = new ArrayList<String>(); boolean overwrite = true; final InputStream is = BuildGenerator.class.getClassLoader(). getResourceAsStream("generatorConfig.xml"); ConfigurationParser cp = new ConfigurationParser(warnings); Configuration config = cp.parseConfiguration(is); DefaultShellCallback callback = new DefaultShellCallback(overwrite); MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings); myBatisGenerator.generate(null); } catch (SQLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } catch (InvalidConfigurationException e) { e.printStackTrace(); } catch (XMLParserException e) { e.printStackTrace(); } } }
generatorConfig.xml我是放在跟类同一目录,以下是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> <!--数据库驱动包路径 --> <classPathEntry location="/Users/terrence/.m2/repository/org/mybatis/generator/mybatis-generator-core/1.3.2-support/mybatis-generator-core-1.3.2-support.jar"/> <context id="DB2Tables" targetRuntime="MyBatis3"> <!--这里使用自定义的类CommentGeneratorSupport--> <commentGenerator type="org.mybatis.generator.internal.CommentGeneratorSupport"> <property name="javaFileEncoding" value="UTF-8"/> <!-- 是否去除自动生成的注释 true:是 : false:否 --> <property name="suppressAllComments" value="false" /> <property name="suppressDate" value="true" /> </commentGenerator> <!--数据库连接信息 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/xxx?useUnicode=true&characterEncoding=utf-8" userId="xxx" password="xxx"> </jdbcConnection> <!--生成的model 包路径 --> <javaModelGenerator targetPackage="com.xxx.pojo" targetProject="/Users/terrence/Documents/workplace/javaee/panda/项目根目录/src/main/java"> <property name="enableSubPackages" value="ture"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!--生成xml mapper文件 路径 --> <sqlMapGenerator targetPackage="src/main/resources/mybatis" targetProject="/Users/terrence/Documents/workplace/javaee/项目根目录"> <property name="enableSubPackages" value="ture"/> </sqlMapGenerator> <!-- 生成的Dao接口 的包路径 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.xxx.dao" targetProject="/Users/terrence/Documents/workplace/javaee/项目根目录/src/main/java"> <property name="enableSubPackages" value="ture"/> </javaClientGenerator> <!--账户 表--> <table schema="" tableName="TB_ACCOUNT" domainObjectName="Account" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> <!--权限表--> <table schema="" tableName="TB_PERMISSION" domainObjectName="Permission" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> <!--角色表--> <table schema="" tableName="TB_ROLE" domainObjectName="Role" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> </context> </generatorConfiguration>
最后运行BuildGenerator类成功生成dao、model等文件。
相关文章推荐
- 在intellij idea中使用mybatis插件产生dao、model
- 使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件
- 使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件
- 使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件
- 使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件
- 使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件
- 使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件
- 使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件(转)
- 使用Abator自动生成ibatis的DAO.Model.mapper.xml方法使用说明
- java笔记之使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件2种方法
- 使用Mybatis-Generator自动生成Dao、model和mapper.xml相关文件
- 使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件
- MyBatis 使用Generator自动生成Model , Dao, mapper
- 使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件
- 使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件
- 使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件
- 使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件(转)
- 使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件(转)
- 使用Mybatis-Generator自动生成Dao、Model、Mapping相关