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

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 17:28   2282   0

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


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

package mybatis;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import junit.framework.TestCase;

import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;

/**
 * mybatis反向工具,可以使用此生成所需代码。
 * 
 * @author dongjian
 * */
public class MyBatisGeneratorTest extends TestCase {
 
 private String confFilePath = "generatorConfig.xml";

 public final void testGenerator() throws Exception {
  
  List<String> warnings = new ArrayList<String>();
  
  confFilePath = this.getClass().getResource(confFilePath).getFile();
  
  File configFile = new File(confFilePath);
  
  Configuration config = new ConfigurationParser(warnings).parseConfiguration(configFile);
  
  DefaultShellCallback callback = new DefaultShellCallback(true);
  
  MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
  
  myBatisGenerator.generate(null);
 }
 
}

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

package mybatis.plugin;

import java.util.List;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.mybatis.generator.plugins.EqualsHashCodePlugin;

/**
 * 生成器插件,用于生成Equals和HashCode方法
 * 
 * @author dongjian
 *
 */
public class MyEqualsHashCodePlugin extends EqualsHashCodePlugin {
  @Override
     public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass,
             IntrospectedTable introspectedTable) {
    return modelPrimaryKeyClassGenerated(topLevelClass, introspectedTable);
     }

     @Override
     public boolean modelPrimaryKeyClassGenerated(TopLevelClass topLevelClass,
             IntrospectedTable introspectedTable) {
      List<IntrospectedColumn> columns = introspectedTable.getPrimaryKeyColumns();
      if(columns == null || columns.size() == 0){
       columns = introspectedTable.getAllColumns();
      }
      
         generateEquals(topLevelClass, columns, introspectedTable);
         generateHashCode(topLevelClass, columns, introspectedTable);

         return true;
     }

     @Override
     public boolean modelRecordWithBLOBsClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
         return modelPrimaryKeyClassGenerated(topLevelClass, introspectedTable);
     }
}

4、编写配置文件(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>

 <context id="tables" targetRuntime="MyBatis3">
        <plugin type="mybatis.plugin.MyEqualsHashCodePlugin"></plugin>
  <plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin>
  
  <!-- import mapper -->
  <plugin type="org.mybatis.generator.plugins.MapperConfigPlugin">
   <property name="fileName" value="GeneratedMapperConfig.xml" />
   <property name="targetPackage" value="mapper" />
   <property name="targetProject" value="C:\code\resources\" />
  </plugin>
  
  <!-- 是否去除自动生成的注释 -->
  <commentGenerator>
            <property name="suppressAllComments" value="false" />
            <property name="suppressDate" value="false" />
  </commentGenerator>
  
  <!-- jdbc连接配置 -->
  <jdbcConnection driverClass="com.mysql.jdbc.Driver" 
   connectionURL="jdbc:mysql://localhost:3306/dbname"
   userId="root" password="root">
  </jdbcConnection>
  
        <!-- java类型配置 -->
  <javaTypeResolver>
   <property name="forceBigDecimals" value="false" />
  </javaTypeResolver>

  <!-- generate Model -->
  <javaModelGenerator targetPackage="com.jd.spinsidesales.domain.po" 
   targetProject="C:\code\java">
   <property name="enableSubPackages" value="true" />
  </javaModelGenerator>

  <!-- generate xml -->
  <sqlMapGenerator targetPackage="sqlmap.mybatis.mysql" 
   targetProject="C:\code\resources\">
   <property name="enableSubPackages" value="true" />
  </sqlMapGenerator>

  <!-- generate dao -->
  <javaClientGenerator type="XMLMAPPER" targetPackage="com.jd.spinsidesales.dao" 
   targetProject="C:\code\java">
   <property name="enableSubPackages" value="true" />
  </javaClientGenerator>
  
  <table tableName="approve_log" domainObjectName="ApproveLog" 
   enableCountByExample="false" enableSelectByExample="false" 
   enableDeleteByExample="false" enableUpdateByExample="false"></table>
  
 </context>
</generatorConfiguration>  

5、建立文件夹

C:\code\resources\

C:\code\java


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

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP