• 工具MyBatis Generator(MBG)


    MyBatis Generator(MBG),这是官方帮我们提供的一个自动生成代码的工具,前面的课程中,我们都是脑袋里想好,pojo有哪些属性,属性的类型是什么,对应的数据表中的字段名字是什么,匹配的类型是什么.....然后还要写接口xxxDao,以及它的实现配置文件xxxDao.xml等等都是手动自己操作,以前我们学习Hibernate的时候,感觉方便就是写好pojo启动服务器Hibernate会自动帮助我们生成对应的数据表,MyBatis也有类似的工具,MBG就是官方给我提供的这样的工具,但它和Hibernate有点不一样就是,Hibernate帮我们生成表,MBG帮我们根据表生成接口、pojo类和xml这些文件!方向是反的。

    要使用MBG首先要导jar包和建立一个XML配置文件

    1. <dependency>
    2. <groupId>org.mybatis.generatorgroupId>
    3. <artifactId>mybatis-generator-coreartifactId>
    4. <version>1.3.7version>
    5. dependency>

    以下元素就是MBG的最小配置

    元素指定如何连接数据库

    元素指定生成Model的目标package与目标project

    元素指定生成Mapping XML文件的目标package与目标project

    (Optionally)元素指定生成Mapper(即DAO)文件的目标package与目标project, 如果不指定这个元素就不会生成Mapper文件,至少一个table元素。

    下面是一个较为完整的示例, 可以保存下来按需修改

    1. "1.0" encoding="UTF-8"?>
    2. 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. <classPathEntry location="/Program Files/IBM/SQLLIB/java/db2java.zip"/>
    7. <property name="suppressAllComments" value="true"/>
    8. commentGenerator>
    9. <jdbcConnection driverClass="COM.ibm.db2.jdbc.app.DB2Driver"
    10. connectionURL="jdbc:db2:TEST"
    11. userId="db2admin"
    12. password="db2admin">
    13. jdbcConnection>
    14. <javaTypeResolver>
    15. <property name="forceBigDecimals" value="false"/>
    16. javaTypeResolver>
    17. <javaModelGenerator targetPackage="test.model" targetProject=".\src\main\java">
    18. <property name="enableSubPackages" value="true"/>
    19. <property name="constructorBased" value="true"/>
    20. <property name="immutable" value="true"/>
    21. <property name="rootClass" value="com.github.prontera.domain.base.BasicEntity"/>
    22. <property name="trimStrings" value="true"/>
    23. javaModelGenerator>
    24. <sqlMapGenerator targetPackage="test.xml" targetProject=".\src\main\java">
    25. <property name="enableSubPackages" value="true"/>
    26. sqlMapGenerator>
    27. <javaClientGenerator type="XMLMAPPER" targetPackage="test.dao" targetProject=".\src\main\java">
    28. <property name="enableSubPackages" value="true"/>
    29. <property name="rootInterface" value="com.github.prontera.Mapper"/>
    30. javaClientGenerator>
    31. <table alias="ha" tableName="ALLTYPES" domainObjectName="Customer"
    32. enableCountByExample="false" enableUpdateByExample="false"
    33. enableDeleteByExample="false" enableSelectByExample="false"
    34. selectByExampleQueryId="false">
    35. <property name="useActualColumnNames" value="true"/>
    36. <property name="rootClass" value="com.github.prontera.domain.base.HelloBasicClass"/>
    37. <property name="rootInterface" value="com.github.prontera.Mapper"/>
    38. <property name="trimStrings" value="true"/>
    39. <columnRenamingRule searchString="^CUST_" replaceString=""/>
    40. <ignoreColumn column="CREATE_TIME"/>
    41. <ignoreColumnsByRegex pattern=".*_TIME$">
    42. <except column="UPDATE_TIME"/>
    43. ignoreColumnsByRegex>
    44. table>
    45. context>
    46. generatorConfiguration>

    Java的方法运行插件

    1. List warnings = new ArrayList();
    2. boolean overwrite = true;
    3. File configFile = new File("generatorConfig.xml");
    4. ConfigurationParser cp = new ConfigurationParser(warnings);
    5. Configuration config = cp.parseConfiguration(configFile);
    6. DefaultShellCallback callback = new DefaultShellCallback(overwrite);
    7. MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
    8. myBatisGenerator.generate(null);

  • 相关阅读:
    ESP01S通过心知天气获取天气和时间信息
    GO语言开山篇(一):学习方向
    01-docker基础
    idea灰屏问题
    php初级教程八 Error
    【开源教程12】疯壳·开源编队无人机-串口(视觉数据获取)
    MYSQL
    初入算法(1)—— 进入算法世界
    通关GO语言21 网络编程:Go 语言如何玩转 RESTful API 服务?
    运放注意事项
  • 原文地址:https://blog.csdn.net/weixin_43227851/article/details/139457428