MyBatis-Generator 用法介绍

时间:2022-07-23
本文章向大家介绍MyBatis-Generator 用法介绍,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

  ”工欲善其事,必先利其器“,古人说的很对,虽然不能做一个单纯的”工具帝“,但是自己有合适的工具集真的很关键。以前认识一个做逆向工程的高手,有自己的“反马套装”,其实不外乎就是 OD 、 IDA 、SysinternalsSuite 等之类的工具组合,当然还有一些自己写的小工具。对于高手就是这样,有自己的工具,对于新手而言,也是要尝试和搜集各种适合自己在项目中可以快速完成工作的工具。泥瓦工、木匠有自己的贴身的工具,游戏高手也有自己专用的鼠标、键盘、显示器和鼠标垫,作为程序员(我是初级的)没有自己合适工具说的过去?

  MyBatis-Generator 是一款 MyBatis 代码生成的工具,它一共有2个文件,一个是 mybatis-generator-core-1.3.2.jar 代码生成的 JAR 文件,另外一个是用于配置代码生成的 generator.xml 的 XML 文件。

配置说明

  在进行代码生成之前,需要先对 generator.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="D:generatormysql-connector-java-5.1.18-bin.jar" />
    <context id="DB2Tables" targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressAllComments" value="true" />
        </commentGenerator>
        <!-- 数据库链接URL、用户名、密码 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/db_name" userId="root" password="123456">
        </jdbcConnection>
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>
        <!-- 生成模型的包名和位置 -->
        <javaModelGenerator targetPackage="package_name" targetProject="D:generatorsrc">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
        <!-- 生成的映射文件包名和位置 -->
        <sqlMapGenerator targetPackage="package_name" targetProject="D:generatorsrc">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>
        <!-- 生成DAO的包名和位置 -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="package_name" targetProject="D:generatorsrc">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>
        <!-- 要生成那些表(更改tableName和domainObjectName就可以) -->
        <table tableName="table1" domainObjectName="Table1" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
        <table tableName="table2" domainObjectName="Table2" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
        <table tableName="table3" domainObjectName="Table3" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
    </context>
</generatorConfiguration>

  上面的 XML 就是完整的 generitor.xml 文件的内容,需要修改的部分已经用注释进行了标注。

  第4行的注释可以看出,第5行的配置用于指定操作数据库的 JAR 包在本地的位置。

  第11行指定操作数据库的类名,数据库的地址、库名、数据库的账号和密码。

  第17行的 targetPackage 键指定生成实体类的包名,比如 org.test.model ,targetProject 指定生成的代码的路径。

  第22行的 targetPackage 键指定生成映射文件的包名,比如 org.test.mapper。

  第26行的 targetPackage 键指定生成数据访问接口的包名,比如 org.test.dao。

  第30行开始则是要进行代码生成的表名和实体类之间的对应关系,比如 tableName 键用于指定数据库中指定的表名, domainObjectName 用于指定数据库指定表名所对应的实体类(领域对象)。数据表和实体类映射后有一些相应的参数,比如 enableCountByExample 等会生成一些例子代码,这块省略。

代码生成

  将前面的配置文件配置完成后,使用命令即可将根据配置生成代码,命令如下:

java -jar mybatis-generator-core-1.3.2.jar -configfile generator.xml -overwrite

  执行上述命令后会根据配置生成相应的目录和文件。

生成的目录如下:

  srcorgtestmodel 、 srcorgtestmapper 等目录。

生成的文件如下:

  Table1.java 、 Table2.java 等实体类文件。

  Table1Mapper.xml 、 Table2Mapper.xml 等 XML 文件。

  Table1Mapper.java 、 Table2Mapper.java 等接口文件。

  生成的具体代码在这里就不具体的列举了。