当前位置:   article > 正文

MyBatisGenerator 自动生成java代码(反向工具)_java mgenerator

java mgenerator

1、引入jar:mybatis-generator-core-1.3.1-bundle.zip


2、编写主类,程序入口:

  1. package mybatis;
  2. import java.io.File;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import junit.framework.TestCase;
  6. import org.mybatis.generator.api.MyBatisGenerator;
  7. import org.mybatis.generator.config.Configuration;
  8. import org.mybatis.generator.config.xml.ConfigurationParser;
  9. import org.mybatis.generator.internal.DefaultShellCallback;
  10. /**
  11. * mybatis反向工具,可以使用此生成所需代码。
  12. *
  13. * @author dongjian
  14. * */
  15. public class MyBatisGeneratorTest extends TestCase {
  16. private String confFilePath = "generatorConfig.xml";
  17. public final void testGenerator() throws Exception {
  18. List<String> warnings = new ArrayList<String>();
  19. confFilePath = this.getClass().getResource(confFilePath).getFile();
  20. File configFile = new File(confFilePath);
  21. Configuration config = new ConfigurationParser(warnings).parseConfiguration(configFile);
  22. DefaultShellCallback callback = new DefaultShellCallback(true);
  23. MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
  24. myBatisGenerator.generate(null);
  25. }
  26. }

3、编写插件,用于生成hashCode和equals方法:

  1. package mybatis.plugin;
  2. import java.util.List;
  3. import org.mybatis.generator.api.IntrospectedColumn;
  4. import org.mybatis.generator.api.IntrospectedTable;
  5. import org.mybatis.generator.api.dom.java.TopLevelClass;
  6. import org.mybatis.generator.plugins.EqualsHashCodePlugin;
  7. /**
  8. * 生成器插件,用于生成Equals和HashCode方法
  9. *
  10. * @author dongjian
  11. *
  12. */
  13. public class MyEqualsHashCodePlugin extends EqualsHashCodePlugin {
  14. @Override
  15. public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass,
  16. IntrospectedTable introspectedTable) {
  17. return modelPrimaryKeyClassGenerated(topLevelClass, introspectedTable);
  18. }
  19. @Override
  20. public boolean modelPrimaryKeyClassGenerated(TopLevelClass topLevelClass,
  21. IntrospectedTable introspectedTable) {
  22. List<IntrospectedColumn> columns = introspectedTable.getPrimaryKeyColumns();
  23. if(columns == null || columns.size() == 0){
  24. columns = introspectedTable.getAllColumns();
  25. }
  26. generateEquals(topLevelClass, columns, introspectedTable);
  27. generateHashCode(topLevelClass, columns, introspectedTable);
  28. return true;
  29. }
  30. @Override
  31. public boolean modelRecordWithBLOBsClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
  32. return modelPrimaryKeyClassGenerated(topLevelClass, introspectedTable);
  33. }
  34. }

4、编写配置文件(generatorConfig.xml):

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE generatorConfiguration
  3. PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
  4. "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
  5. <generatorConfiguration>
  6. <context id="tables" targetRuntime="MyBatis3">
  7. <plugin type="mybatis.plugin.MyEqualsHashCodePlugin"></plugin>
  8. <plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin>
  9. <!-- import mapper -->
  10. <plugin type="org.mybatis.generator.plugins.MapperConfigPlugin">
  11. <property name="fileName" value="GeneratedMapperConfig.xml" />
  12. <property name="targetPackage" value="mapper" />
  13. <property name="targetProject" value="C:\code\resources\" />
  14. </plugin>
  15. <!-- 是否去除自动生成的注释 -->
  16. <commentGenerator>
  17. <property name="suppressAllComments" value="false" />
  18. <property name="suppressDate" value="false" />
  19. </commentGenerator>
  20. <!-- jdbc连接配置 -->
  21. <jdbcConnection driverClass="com.mysql.jdbc.Driver"
  22. connectionURL="jdbc:mysql://localhost:3306/dbname"
  23. userId="root" password="root">
  24. </jdbcConnection>
  25. <!-- java类型配置 -->
  26. <javaTypeResolver>
  27. <property name="forceBigDecimals" value="false" />
  28. </javaTypeResolver>
  29. <!-- generate Model -->
  30. <javaModelGenerator targetPackage="com.jd.spinsidesales.domain.po"
  31. targetProject="C:\code\java">
  32. <property name="enableSubPackages" value="true" />
  33. </javaModelGenerator>
  34. <!-- generate xml -->
  35. <sqlMapGenerator targetPackage="sqlmap.mybatis.mysql"
  36. targetProject="C:\code\resources\">
  37. <property name="enableSubPackages" value="true" />
  38. </sqlMapGenerator>
  39. <!-- generate dao -->
  40. <javaClientGenerator type="XMLMAPPER" targetPackage="com.jd.spinsidesales.dao"
  41. targetProject="C:\code\java">
  42. <property name="enableSubPackages" value="true" />
  43. </javaClientGenerator>
  44. <table tableName="approve_log" domainObjectName="ApproveLog"
  45. enableCountByExample="false" enableSelectByExample="false"
  46. enableDeleteByExample="false" enableUpdateByExample="false"></table>
  47. </context>
  48. </generatorConfiguration>

5、建立文件夹

C:\code\resources\

C:\code\java


6、使用Junit运行MyBatisGeneratorTest类,您当然可以使用main方法来运行,这无关紧要,运行之后就可以看见生成的java类了。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/58533
推荐阅读
相关标签
  

闽ICP备14008679号