MBG 实战 零基础

** 如有错误,感谢指正**

如有错误,感谢指正,请私信博主,有辛苦红包,拜“一字之师”。

请根据目录寻找自己需要的段落

导语:本博客为个人整理Java学习记录帖,如有错误,感谢指正。系统学习,欢迎持续关注,后续陆陆续续更新
Java 交流qq群 383245788

本文旨在学习交流,个人crud学习心得

本文大量参考MBG官网:http://mybatis.org/generator/
MBG在做ssm小demo时可以用用,方便,MP也可以做,并且做的更好,MBG建议只做了解就行。

MBG简介

MyBatis Generator (MBG) 是 MyBatis 的代码生成器。它将为所有版本的 MyBatis 生成代码。它将内省一个数据库表(或多个表)并生成可用于访问表的工件。这减少了设置对象和配置文件以与数据库表交互的初始麻烦。MBG 试图对大量简单 CRUD(创建、检索、更新、删除)的数据库操作产生重大影响。您仍然需要为连接查询或存储过程编写 SQL 和对象代码。
MBG 生成不同风格和不同语言的代码,这取决于它的配置方式。例如,MBG 可以生成 Java 或 Kotlin 代码。MBG 可以生成与 MyBatis3 兼容的 XML - 尽管现在被认为是 MBG 的遗留用途。生成代码的较新样式不需要 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>
<context id="DB2Tables" targetRuntime="Mybatis3">
    <commentGenerator>
        <property name="suppressAllComments" value="true"/>
    </commentGenerator>
    <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                    connectionURL="jdbc:mysql://localhost:3306/ssm_mdsp"
                    userId="root"
                    password="root">
    </jdbcConnection>
    <javaTypeResolver>
        <property name="forceBigDecimals" value="false" />
    </javaTypeResolver>

    <!-- 指定javaBean生成的位置 -->
    <javaModelGenerator targetPackage="top.atluofu.pojo"
                        targetProject=".\src\main\java">
        <property name="enableSubPackages" value="true" />
        <property name="trimStrings" value="true" />
    </javaModelGenerator>

    <!--指定sql映射文件生成的位置 -->
    <sqlMapGenerator targetPackage="mapper" targetProject=".\src\main\resources">
        <property name="enableSubPackages" value="true" />
    </sqlMapGenerator>

    <!-- 指定dao接口生成的位置,mapper接口 -->
    <javaClientGenerator type="XMLMAPPER"
                         targetPackage="top.atluofu.mapper" targetProject=".\src\main\java">
        <property name="enableSubPackages" value="true" />
    </javaClientGenerator>


    <!-- table指定每个表的生成策略 -->
    <table tableName="order" domainObjectName="Order">

    </table>
    <table tableName="role" domainObjectName="Role">

    </table>
    <table tableName="trade" domainObjectName="Trade">

    </table>
    <table tableName="user" domainObjectName="User">

    </table>
</context>
</generatorConfiguration>

xml参考配置

  • classPathEntry: 指定驱动
    如<classPathEntry location="/Program Files/IBM/SQLLIB/java/db2java.zip" />
  • commentGenerator:评论器,生成注释,如表注释,列注释,时间戳等

启动类

使用xml配置从Java程序运行MBG

public class MBG {

    @Test
    public static void main(String[] args) throws XMLParserException, IOException, InvalidConfigurationException, SQLException, InterruptedException {
        List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;
        File configFile = new File("src/main/resources/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);
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

墨竹菊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值