zoukankan      html  css  js  c++  java
  • 使用 MybatisGenerator 根据数据库自动生成 model、mapper 接口和 mapper.xml

    前言

    第一次使用 MybatisGenerator 踩了 N 个坑,必须写篇文章记录一下。

    准备工作

    1、准备好一个数据库,数据库中有若干表,表里有若干数据,这是炒菜的原材料,必须先准备好。

    2、使用 IDEA 创建一个新 SpringBoot 项目,并添加如下依赖:

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>2.1.4</version>
            </dependency>
            <dependency>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-core</artifactId>
                <version>1.4.0</version>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <scope>runtime</scope>
                <version>8.0.16</version>
            </dependency>
    

    注意:

    • mysql 依赖的版本要和本地安装的 mysql 版本一致,我这里是 8.0.16

    • mybatis-generator 依赖的版本要在 1.3.7 以上,不然会有坑,这里我使用最新版本 1.4.0

    3、创建 mapper包、model包、mg包。其中mapper包用来放自动生成的 mapper 接口和 mapper.xml;model包用来放自动生成的 model 类;mg包用来放与 MybatisGenerator 相关的东西。

    大致目录如下图:

    暂时忽略掉 controllerservice 包。

    4、修改一下 pom.xml 文件,在 <build>标签下,添加如下代码:

    <build>
            <resources>
                <resource>
                    <directory>src/main/java</directory>
                    <includes>
                        <include>**/*.xml</include>
                    </includes>
                </resource>
                <resource>
                    <directory>src/main/resources</directory>
                </resource>
            </resources>
             ...
    </build>
    

    5、修改启动类,在启动类上,添加一行 mapper 包扫描代码:

    @MapperScan(basePackages = "com.example.mybatisgeneratortest.mapper")
    

    开始炒菜

    mg包下新建一个generator.xml 文件,写入如下代码:

    <!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="simple" targetRuntime="MyBatis3Simple">
    
            <!-- 为模型生成序列化方法-->
            <plugin type="org.mybatis.generator.plugins.SerializablePlugin"/>
            <!-- 为生成的Java模型创建一个toString方法 -->
            <plugin type="org.mybatis.generator.plugins.ToStringPlugin"/>
    
            <!--生成mapper.xml时覆盖原文件-->
            <plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin" />
    
            <commentGenerator>
                <!--  关闭自动生成的注释  -->
                <property name="suppressAllComments" value="true" />
            </commentGenerator>
    
            <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                            connectionURL="jdbc:mysql://localhost:3306/vhr?serverTimezone=UTC&amp;useUnicode=true&amp;characterEncoding=UTF-8"
                            userId="root"
                            password="123456">
                <!--解决mysql驱动升级到8.0后不生成指定数据库代码的问题-->
                <property name="nullCatalogMeansCurrent" value="true"/>
            </jdbcConnection>
    
            <javaModelGenerator targetPackage="com.example.mybatisgeneratortest.model" targetProject="src/main/java"/>
    
            <sqlMapGenerator targetPackage="com.example.mybatisgeneratortest.mapper" targetProject="src/main/java"/>
    
            <javaClientGenerator type="XMLMAPPER" targetPackage="com.example.mybatisgeneratortest.mapper" targetProject="src/main/java"/>
    
            <table tableName="%">
                <!--生成的model实体的属性,使用实际的表列名作为实体类的属性名,满足驼峰命名法-->
                <property name="useActualColumnNames" value="true" />
            </table>
        </context>
    </generatorConfiguration>
    

    这是使用 MybatisGenerator 自动生成代码的配置文件。

    注意几个坑:

    • 要设置生成mapper.xml 时覆盖原文件,即代码<plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin" />

    • driverClass 使用 com.mysql.cj.jdbc.Driver 不要使用 druid

    • connectionURL 中的 &符号要替换成 &amp;

    • tableName="%" 表示数据库中的所有表都要自动生成 mapper 和 model

    • 最好是关掉自动生成的注释。

    mg包下新建一个Generator.class 类,如下代码:

    public class Generator {
        public static void main(String[] args) throws Exception {
            //MBG 执行过程中的警告信息
            List<String> warnings = new ArrayList<String>();
            //当生成的代码重复时,覆盖原代码
            boolean overwrite = true;
            //读取我们的 MBG 配置文件
            InputStream is = Generator.class.getResourceAsStream("generator.xml");
            ConfigurationParser cp = new ConfigurationParser(warnings);
            Configuration config = cp.parseConfiguration(is);
            is.close();
    
            DefaultShellCallback callback = new DefaultShellCallback(overwrite);
            //创建 MBG
            MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
            //执行生成代码
            myBatisGenerator.generate(null);
            //输出警告信息
            for (String warning : warnings) {
                System.out.println(warning);
            }
        }
    }
    

    这是 MybatisGenerator 的启动类,运行它,效果如下:

    检查成果

    参考资源

    https://segmentfault.com/a/1190000016525887

    https://www.cnblogs.com/esther-qing/p/8036899.html

    https://blog.csdn.net/Cgh_Baby/article/details/93165152

    http://mybatis.org/generator/

    每天学习一点点,每天进步一点点。

  • 相关阅读:
    Vue.js 转自http://zhuanlan.zhihu.com/evanyou/20302927
    Linux 下的终端
    图像热点(图像地图)
    网页多媒体 flash
    下拉列表
    <input> type 属性
    <form>表单
    表格结构
    简介
    <meta>标记
  • 原文地址:https://www.cnblogs.com/youcoding/p/14693096.html
Copyright © 2011-2022 走看看