Mybatis逆向工程,生成java、xml等文件

来源:互联网 发布:linux shell usleep 编辑:程序博客网 时间:2024/06/07 09:11

1.新建: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="xxxx(写绝对路径)/mysql-connector-java-5.1.34.jar" />
     <context id="DB2Tables"    targetRuntime="MyBatis3">


        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <!--数据库链接地址账号密码-->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1/xxx" userId="xxx" password="xxx">
        </jdbcConnection>
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <!--生成Model类存放位置-->
        <javaModelGenerator targetPackage="com.leige.domain" targetProject="src">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!--生成映射文件存放位置-->
        <sqlMapGenerator targetPackage="com.leige.domain" targetProject="src">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!--生成DaoMapper类存放位置-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.leige.domain" targetProject="src">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <!--生成对应表及类名,需要记住的一点是逆向工程无法生成关联关系,只能生成单表操作-->
        <table tableName="sys_user" 
            domainObjectName="sys_user" 
          ></table>
           <table tableName="bd_notice" 
            domainObjectName="bd_notice" 
          ></table>
    </context>
</generatorConfiguration>


2.

import java.io.File;


import java.util.ArrayList;
import java.util.List;




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;


public class MybatisGen {
public static void generator() throws Exception{
         List<String> warnings = new ArrayList<String>();
         boolean overwrite = true;
         //项目根路径不要有中文,我的有中文,所以使用绝对路径
         File configFile = new File("xxxx(绝地路径)/mybatis/generatorConfig.xml");
         ConfigurationParser cp = new ConfigurationParser(warnings);
         Configuration config = cp.parseConfiguration(configFile);
         DefaultShellCallback callback = new DefaultShellCallback(overwrite);
         MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
         myBatisGenerator.generate(null);
  }
  public static void main(String[] args) {
      try {
          generator();
          System.out.println("3123");
      } catch (Exception e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
  }
}


3.添加jar包

mybatis-generator-core-1.3.2