快学快用MybatisGenerator使用教程

2 篇文章 0 订阅
1 篇文章 0 订阅

Mybatis Generator 配置详解:https://www.imooc.com/article/21444
Mybatis Generator官网文档:http://mybatis.org/generator/running/runningWithMaven.html。
重点关注官网文档中的:Running Mybatis Generator-with Maven的配置,以及XML Configuration Reference的内容。

一、摘要

Mybatis-Generator可以帮助我们自动生成很多结构化的代码,比如每张表对应的Entity、Mapper接口和Xml文件,可以省去很多繁琐的工作。所以重点是帮助自动生成,这样我们可以少些很多代码,同时自动生成的代码能够满足我们的基本需要,当然,自己手写entity和dao以及mappers也是可以的,用在实现一些复杂的业务逻辑时。

二、使用方法

1.porm.xml中添加对应插件

需要在porm.xml的最下面的plugins里添加mybatis-generator对应的插件。

<project ...>
     ...
     <build>
       ...
       <plugins>
        ...
        <plugin>
          <groupId>org.mybatis.generator</groupId>
          <artifactId>mybatis-generator-maven-plugin</artifactId>
          <version>1.4.0</version>
        </plugin>
        ...
      </plugins>
      ...
    </build>
    ...
  </project>

版本号根据实际情况来选择。

2.配置generatorConfig.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="C:\Users\DJ\Desktop\upload\mysql-connector-java-8.0.18.jar" />

    <context id="DB2Tables" targetRuntime="MyBatis3">
<!--        不再文件中追加xml-->
        <plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin"></plugin>

        <commentGenerator>
            <!--            生成文件时不会添加注释-->
            <property name="suppressAllComments" value="true"></property>
        </commentGenerator>

        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                        connectionURL="jdbc:mysql://127.0.0.1:3306/mall?serverTimezone=GMT%2B8&amp;useSSL=false&amp;characterEncoding=UTF-8"
                        userId="root"
                        password="123456dj">
        </jdbcConnection>

        <javaTypeResolver >
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>

        <javaModelGenerator targetPackage="com.dj.mall.pojo" targetProject="src\main\java">
            <property name="enableSubPackages" value="true" />
<!--            去掉内容的空格-->
            <property name="trimStrings" value="true" />
        </javaModelGenerator>

        <sqlMapGenerator targetPackage="mappers"  targetProject="src\main\resources">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>

        <javaClientGenerator type="XMLMAPPER" targetPackage="com.dj.mall.dao"  targetProject="src\main\java">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>

        <table tableName="mall_order" domainObjectName="Order" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false"  enableUpdateByExample="false"></table>

        <table tableName="mall_order_item" domainObjectName="OrderItem" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false"  enableUpdateByExample="false"></table>

    </context>
</generatorConfiguration>

下面我们呢依次来解释下里面模块的内容:

  1. <classPathEntry location="C:\Users\DJ\Desktop\upload\mysql-connector-java-8.0.18.jar" /> 表示我们的数据库驱动的路径,这里我们用的时mysql,所以去下载mysql的jar文件,然后随便放置在一个位置,然后在location的属性里写出它的绝对地址即可。
  2. <context id="DB2Tables" targetRuntime="MyBatis3"> 这是mybatis-generatore的一些配置的写入位置,里面写了相关的配置信息。
    在这里插入图片描述

需要注意的是:在context里可以写入的模块有上面图片中的内容,但是请一定要按照上述的顺序来排列对应模块。
3. <plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin"></plugin>这个配置表示当重新生成一边entiy,mapper等文件时覆盖当前文件里的内容,而不是追加在尾部,这样每一个文件的内容就只有一份,重新生成时不会重复内容。
4. 下面是在<commentGenerator>里的内容

<commentGenerator>
    <!--            生成文件时不会添加注释-->
    <property name="suppressAllComments" value="true"></property>
</commentGenerator>

用来让生成文件的注释去掉,这样看起来更加简洁些。
5. <jdbcConnection这个是用来配置连接数据库的一些东西。包括驱动,数据库的url,用户,密码等。
6. <javaModelGenerator targetPackage="com.dj.mall.pojo" targetProject="src\main\java">这个配置项是用来标识自动生成的entity(实体类,这里我起名叫pojo-president object Java object)的位置,前一个标识项目名,后一个标识绝对位置。
7. <sqlMapGenerator targetPackage="mappers" targetProject="src\main\resources">同理,标识生成的mapper.xml的文件的位置。
8. <javaClientGenerator type="XMLMAPPER" targetPackage="com.dj.mall.dao" targetProject="src\main\java"> 用来标识生成的dao文件的位置。
9. <table tableName="mall_order" domainObjectName="Order" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"></table> 表示自动生成哪个表的配置文件,这里对应mysql数据库中的mall_order表,其中domainObjectName=“Order” 表示我希望对应表生成的所有文件都以Order开头,省去了表名中的mall,enableCountByExample="false"等几个表示把dao中的几个无用的example方法在生成dao文件时不要添加进去。
10. 还有一点需要注意的是,需要在porm.xml中的mybatis-generator插件配置处添加

<configuration>
    <overwrite>true</overwrite>
</configuration>

它的作用是在我们重新生成对应的dao,mapper等文件时,不会重新去新建一个新文件,而是向原有的文件里去添加,配合3一起食用更加。

3.生成entity,dao,mapper

我这里是idea,只需要在终端里执行命令,idea里的terminal就是终端,打开它就行,同时因为我是maven项目,所以用命令:mvn mybatis-generator:generate即可。

最后会生成对应表的实体类,dao,以及mapper。

这里提一点:我们可以在idea里下载lombok和mybatis-plugin这两个插件,lombok是帮助我们省去enitty中的get和set方法,安装该插件后只需要在实体类上加上@data标记即可;而mybatis-plugin插件则是帮助我们快速管理dao和mapper文件的,安装后,在dao和mapper中的方法对应位置有小箭头,可以帮助我们快速查找其在mapper或者是dao中的位置。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis Generator 是一个代码生成器,它可以根据数据库表结构自动生成 MyBatis 的 Mapper 文件、Java 实体类和 XML 映射文件等代码。使用 MyBatis Generator 可以大大减少开发人员的工作量,提高开发效率。 下面是使用 MyBatis Generator 的步骤: 1. 配置 generatorConfig.xml 文件,该文件用于配置生成代码的详细信息,包括数据库连接信息、生成的文件类型、生成的表等。 2. 运行 MyBatis Generator,生成代码。 3. 在项目中引入生成的代码,例如 Mapper 接口、实体类等。 以下是一个简单的 generatorConfig.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="mysql-connector-java-5.1.39-bin.jar" /> <context id="MysqlTables" targetRuntime="MyBatis3"> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/mybatis" userId="root" password=""> </jdbcConnection> <javaTypeResolver > <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <javaModelGenerator targetPackage="com.mybatis.example.model" targetProject="src/main/java"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <sqlMapGenerator targetPackage="com.mybatis.example.mapper" targetProject="src/main/resources"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <javaClientGenerator type="XMLMAPPER" targetPackage="com.mybatis.example.mapper" targetProject="src/main/java"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <table tableName="user" domainObjectName="User" > </table> </context> </generatorConfiguration> ``` 这个示例配置文件会生成一个 UserMapper 接口、一个 User 实体类和一个 user.xml 文件。其中,User 实体类的包路径为 com.mybatis.example.model,UserMapper 接口和 user.xml 文件的包路径为 com.mybatis.example.mapper。 在运行 MyBatis Generator 之后,可以在项目中引入生成的代码,例如: ``` @Autowired private UserMapper userMapper; ``` 以上代码会注入一个 UserMapper 对象,可以通过该对象来操作数据库表。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值