当前位置: 首页 > article >正文

工具MyBatis Generator(MBG)

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

工具地址这里找:

https://github.com/mybatis

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

<dependency>
    <groupId>org.mybatis.generator</groupId>
	<artifactId>mybatis-generator-core</artifactId>
	<version>1.3.7</version>
</dependency>

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

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

<?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>

    <!-- 数据库的驱动, JAR/ZIP文件的全路径,maven工程,驱动已经依赖了,没用-->
    <classPathEntry location="/Program Files/IBM/SQLLIB/java/db2java.zip"/>

    <!--targetRuntime用MyBatis3, 也就是默认的, 其他我基本不会用->
    <context id="DB2Tables" targetRuntime="MyBatis3">

        <commentGenerator>
            <!-- 去除自动生成的注释 -->
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <!--基础的数据库连接-->
        <jdbcConnection driverClass="COM.ibm.db2.jdbc.app.DB2Driver"
                        connectionURL="jdbc:db2:TEST"
                        userId="db2admin"
                        password="db2admin">
        </jdbcConnection>

        <!--Java类型解析器, 目前也就只有forceBigDecimals可以给你玩-->
        <javaTypeResolver>
            <!--当数据类型为DECIMAL或者NUMERIC的时候, 如果是true的话则总是使用java.math.BigDecimal-->
            <!--以下是false, 即默认值的情况-->
            <!--如果有小数或者decimal长度大于18, Java类型为BigDecimal-->
            <!--如果没有小数, 以及decimal长度为10至18, Java类型为Long-->
            <!--如果没有小数, 以及decimal长度为5至9, Java类型为Integer-->
            <!--如果没有小数, 以及decimal长度少于5, Java类型为Short-->
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>

        <!--Domain生成器-->
        <javaModelGenerator targetPackage="test.model" targetProject=".\src\main\java">
            <!--据说可以自动添加schema名, 可是我没用到过-->
            <property name="enableSubPackages" value="true"/>

            <!--生成全属性构造器, 没什么用, 如果有指定immutable元素的话这个会被忽略-->
            <property name="constructorBased" value="true"/>

            <!--生成不可变的domain, 这个我也很少用-->
            <property name="immutable" value="true"/>

            <!--每个Domain都继承这个bean-->
            <property name="rootClass" value="com.github.prontera.domain.base.BasicEntity"/>

            <!--当遇到String的时候setter是否会先trim()-->
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>

        <!--Mapping生成器-->
        <sqlMapGenerator targetPackage="test.xml" targetProject=".\src\main\java">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>

        <!--Mapper生成器, 当type为ANNOTATEDMAPPER时是带有@annotation的Mapper, MIXEDMAPPER是XML文件-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="test.dao" targetProject=".\src\main\java">
            <property name="enableSubPackages" value="true"/>

            <!--每个Mapper所继承的接口-->
            <property name="rootInterface" value="com.github.prontera.Mapper"/>
        </javaClientGenerator>

        <!--字段命名策略过程: <columnRenamingRule> >> property name="useActualColumnNames"-->
        <!--alias属性是个神器, 会为所有SQL都添加, 做关联的时候就非常方便了-->
        <!--至于什么Example, 全关了就是-->
        <table alias="ha" tableName="ALLTYPES" domainObjectName="Customer"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">

            <!--指定是否用数据库中真实的字段名, 而不是采用MBG转换后的驼峰-->
            <property name="useActualColumnNames" value="true"/>

            <!--自动集成改类-->
            <property name="rootClass" value="com.github.prontera.domain.base.HelloBasicClass"/>

            <!--Mapper自动继承的接口-->
            <property name="rootInterface" value="com.github.prontera.Mapper"/>

            <!--当遇到String的时候setter是否会先trim()-->
            <property name="trimStrings" value="true"/>

            <!--先进行columnRenamingRule, 再进行useActualColumnNames. 如果有columnOverride则忽略该配置-->
  <!--关于columnRenamingRule的具体例子 http://www.mybatis.org/generator/configreference/columnRenamingRule.html-->
            <columnRenamingRule searchString="^CUST_"      replaceString=""/>

            <!--顾名思义, 忽略某些列-->
            <ignoreColumn column="CREATE_TIME"/>

            <!--也是忽略数据列, 但是可以通过正则表达式, except子元素是可选的, 代表忽略除UPDATE_TIME外的列-->
            <ignoreColumnsByRegex pattern=".*_TIME$">
                <except column="UPDATE_TIME"/>
            </ignoreColumnsByRegex>
        </table>

    </context>
</generatorConfiguration>

Java的方法运行插件

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

http://www.kler.cn/a/565428.html

相关文章:

  • 【Java项目】基于Spring Boot的旅游管理系统
  • 纷析云:赋能企业财务数字化转型的开源解决方案
  • 苍穹外卖-阿里云OSS文件上传
  • Java语法基础知识点1
  • android studio 中止了一个已建立的连接
  • C++11相较于C++98的新特性介绍:列表初始化,右值引用与移动语义
  • Java和JavaScript的比较
  • MQ 笔记
  • 父子继承与转型
  • Redis 底层数据结构 —— SDS(简单动态字符串)
  • AI革命下的多元生态:DeepSeek、ChatGPT、XAI、文心一言与通义千问的行业渗透与场景重构
  • 解决各大浏览器中http地址无权限调用麦克风摄像头问题
  • 按键精灵安卓/ios脚本的连点器的坐标点获取教程
  • mysql怎样优化where like ‘%字符串%‘这种模糊匹配的慢sql
  • GB 44495-2024《汽车整车信息安全技术要求》标准解读|内容架构、测试内容、应对措施
  • 【语音编解码】常用的基于神经网络的语音编解码方案对比
  • 【BES2500x系列 -- RTX5操作系统】系统执行流程 -- 系统初始化 -- main函数 --(十一)
  • smolagents学习笔记系列(九)Examples - Orchestrate a multi-agent system
  • 边缘计算收益低的三大指标
  • android进阶面试题目