Fork me on GitHub

Mybatis Generator(逆向工程)

1.如何使用mybatis 的逆向工程,在百度上搜索mybatis Generator进入官网。英文不好的同学可以翻译成中文

2.在我们的项目中新建一个xml配置文件名字mbg.xml,在官网上找到mybatis Generator快速入门指南点击XML配置文件参考复制xml中的内容并粘贴在我们自己新建的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 =“/ Program Files / IBM / SQLLIB / java / db2java.zip”/>

  <context id =“DB2Tables”targetRuntime =“MyBatis3”>
    <jdbcConnection driverClass =“COM.ibm.db2.jdbc.app.DB2Driver”
        的ConnectionURL = “JDBC:DB2:TEST”
        用户id = “DB2ADMIN”
        密码= “DB2ADMIN”>
    </ jdbcConnection>

    <javaTypeResolver>
      <property name =“forceBigDecimals”value =“false”/>
    </ javaTypeResolver>

    <javaModelGenerator targetPackage =“test.model”targetProject =“\ MBGTestProject \ src”>
      <property name =“enableSubPackages”value =“true”/>
      <property name =“trimStrings”value =“true”/>
    </ javaModelGenerator>

    <sqlMapGenerator targetPackage =“test.xml”targetProject =“\ MBGTestProject \ src”>
      <property name =“enableSubPackages”value =“true”/>
    </ sqlMapGenerator>

    <javaClientGenerator type =“XMLMAPPER”targetPackage =“test.dao”targetProject =“\ MBGTestProject \ src”>
      <property name =“enableSubPackages”value =“true”/>
    </ javaClientGenerator>

    <table schema =“DB2ADMIN”tableName =“ALLTYPES”domainObjectName =“Customer”>
      <property name =“useActualColumnNames”value =“true”/>
      <generatedKey column =“ID”sqlStatement =“DB2”identity =“true”/>
      <columnOverride column =“DATE_FIELD”property =“startDate”/>
      <ignoreColumn column =“FRED”/>
      <columnOverride column =“LONG_VARCHAR_FIELD”jdbcType =“VARCHAR”/>
    </ TABLE>

  </上下文>
</ generatorConfiguration>

3.修改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="DB2Tables" targetRuntime="MyBatis3">
          <!-- 去掉生成文件中的注释 -->
          <commentGenerator>
          <property name="suppressAllComments" value="true"></property>
          </commentGenerator>
  <!-- 数据库连接配置 -->
    <jdbcConnection driverClass="com.mysql.jdbc.Driver"
        connectionURL="jdbc:mysql://localhost:3306/studentInfoManager"
        userId="root"
        password="admin">
    </jdbcConnection>

    <javaTypeResolver >
      <property name="forceBigDecimals" value="false" />
    </javaTypeResolver>
    <!-- 配置生成的实体类的存放路径-->
    <javaModelGenerator targetPackage="com.zhiyuan.bean" targetProject=".\src\main\java">
      <property name="enableSubPackages" value="true" />
      <property name="trimStrings" value="true" />
    </javaModelGenerator>
    <!-- 配置生成的mapper.xml存放路径 -->
    <sqlMapGenerator targetPackage="mapper"  targetProject=".\src\main\resources">
      <property name="enableSubPackages" value="true" />
    </sqlMapGenerator>
    <!-- 配置生成的接口路径 -->
    <javaClientGenerator type="XMLMAPPER" targetPackage="com.zhiyuan.dao"  targetProject=".\src\main\java">
      <property name="enableSubPackages" value="true" />
    </javaClientGenerator>
    <!-- 配置数据库中的表与对应的实体类 -->
    <table schema="DB2ADMIN" tableName="s_aattendance" domainObjectName="Saattendance" >
    </table>
    <table schema="DB2ADMIN" tableName="s_badbehavior" domainObjectName="Sbadbehavior" >
    </table>
     <table schema="DB2ADMIN" tableName="s_basicmessage" domainObjectName="Sbasicmessage" >
    </table>
     <table schema="DB2ADMIN" tableName="s_charge" domainObjectName="Scharge" >
    </table>
     <table schema="DB2ADMIN" tableName="s_charge_launch" domainObjectName="SchargeLaunch" >
    </table>
     <table schema="DB2ADMIN" tableName="s_class" domainObjectName="Sclass" >
    </table>
     <table schema="DB2ADMIN" tableName="s_honour" domainObjectName="Shonour" >
    </table>
     <table schema="DB2ADMIN" tableName="s_login" domainObjectName="Slogin" >
    </table>
     <table schema="DB2ADMIN" tableName="s_performance" domainObjectName="Sperformance" >
    </table>
     <table schema="DB2ADMIN" tableName="s_profession" domainObjectName="Sprofession" >
    </table>
     <table schema="DB2ADMIN" tableName="s_reg" domainObjectName="Sreg" >
    </table>
     <table schema="DB2ADMIN" tableName="s_work" domainObjectName="Swork" >
    </table>

  </context>
</generatorConfiguration>

4.编写我的运行程序

           List<String> warnings = new ArrayList<String>();
           boolean overwrite = true;
           File configFile = new File("mbg.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);

5.运行成功就可以了,查看你配置的路径下是否有包有mapper文件

posted @ 2019-01-03 19:19  云源  阅读(277)  评论(0)    收藏  举报
湖南云源网络科技有限公司 www. ysource.cn 版权所有   ICP证:湘ICP备18004366号
  邮箱:2536201485@qq.com   CSDN博客地址:https://blog.csdn.net/qq_32885621 有事您Q我