technology-integration(三)---使用Mybatis-Generator生成器

Mybatis-Generator是什么

Mybatis-Generator是一款根据xml配置文件自动生成数据库对应的Java Bean(实体类)和Mybatis所需的Mapper接口以及Mapper.xml文件,这能很大程度上减少使用Mybatis框架的繁琐操作。

使用Mybatis-Generator生成相关文件

项目根目录下你可以找到一个名为mybatis-genrator的文件夹,将其下载下来,导入到你所创建项目的根目录(或者你电脑的固定路径也可以)。(可以在码云项目根目录下的SQL文件夹找到数据库脚本,导入到MySQL数据库中)

mybatis-genrator文件夹内容

在这里我们可以看到总共包含了四个文件,

  • generatorConfig.xml:这里面配置着数据库的连接信息,以及生成实体类、Mapper接口、Mapper.xml文件的相关设置
  • mybatis-generator-core-1.3.5.jar:生成文件的核心jar包
  • mysql-connector-java-5.146.jar:MySQL数据库的相关驱动,如果你使用的不是MySQL数据库只需要将驱动jar包换成对应数据库的驱动即可。
  • readme.txt:我会告诉你这个是专门用来复制命令的文件?
generatorConfig.xml
<generatorConfiguration>
    <!--指定特定数据库的jdbc驱动jar包的位置-->
    <classPathEntry location="mysql-connector-java-5.1.46.jar"/>

    <context id="default" targetRuntime="MyBatis3">
        <!-- optional,旨在创建class时,对注释进行控制 -->
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <!--jdbc的数据库连接 -->
        <jdbcConnection
                driverClass="com.mysql.jdbc.Driver"
                <!--修改成对应的数据库连接-->
                connectionURL="jdbc:mysql://127.0.0.1:3306/technology-integration"
                userId="root"
                password="123456">
        </jdbcConnection>

        <!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
            targetPackage     指定生成的model生成所在的包名
            targetProject     指定在该项目下所在的路径
        -->
        <javaModelGenerator targetPackage="com.viu.technology.po"
                            targetProject="../src/main/java">
            <!-- 是否允许子包,即targetPackage.schemaName.tableName -->
            <property name="enableSubPackages" value="true"/>
            <!-- 是否对model添加 构造函数 -->
            <property name="constructorBased" value="true"/>
            <!-- 是否对类CHAR类型的列的数据进行trim操作 -->
            <property name="trimStrings" value="false"/>
        </javaModelGenerator>

        <!--Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
        <sqlMapGenerator targetPackage="mapping"
                         targetProject="../src/main/resources">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>

        <!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
                type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
                type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
                type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口
        -->
        <javaClientGenerator targetPackage="com.viu.technology.mapper"
                             targetProject="../src/main/java" type="XMLMAPPER">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>

        <!--数据库表,需要工具生成哪个数据库表相关的文件就写多少次table
              tableName---表名
              domainObjectName---实体类类名
         -->
        <table tableName="t_product" domainObjectName="Product"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
        </table>
        <table tableName="t_message" domainObjectName="Message"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
        </table>
        <table tableName="t_order" domainObjectName="Order"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
        </table>
        <table tableName="t_product_statis" domainObjectName="ProductStatis"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
        </table>
        <table tableName="t_role" domainObjectName="Role"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
        </table>
        <table tableName="t_user" domainObjectName="User"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
        </table>

    </context>
</generatorConfiguration>

修改好generatorConfig.xml后,用cmd命令打开mybatis-generator文件夹的目录(可以直接在idea中使用alt+f12打开Terminal,相对来说更加方便),然后打开readme.txt把里面的命令复制到命令行中运行即可,命令 java -jar mybatis-generator-core-1.3.5.jar -configfile generatorConfig.xml -overwrite,看到命令行中有successfully代表执行成功


image.png

刷新一下项目,可以看到项目结构中多处红框中的packpage,以及xml配置所需要生成的对应内容


image.png

结束

mybatis-generator生成器的使用虽然比较简单,但每次创建新项目都需要修改generatorConfig.xml文件还是略微有点麻烦的。温馨提示,Spring JPA可以全自动生成,只需要在application.yml配置文件中配置一下即可。


更多文章请关注该 technology-integration全面解析专题

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1 Mybatis入门 1.1 单独使用jdbc编程问题总结 1.1.1 jdbc程序 上边使...
    哇哈哈E阅读 8,566评论 0 38
  • MyBatis Generator (MBG) 是由一个XML配置文件驱动的。这个配置文件中会声明以下内容: 如何...
    JSON_NULL阅读 18,983评论 3 10
  • 从事产品经理这个职业有一段时间了,个人觉得平时多思考与总结还是有必要的,尽管由于水平和经验的局限,目前能拿出手的干...
    Peter的沙洲阅读 3,101评论 0 2
  • 珍馐列筵情浓浓, 美酒漾杯意欣欣。 功名利禄皆幻梦, 笑把金樽酹古今。
    成都独行侠阅读 1,365评论 0 2
  • 每一个人的存在,都有自己的使命;正如每一棵树的存在,都有自己的价值。 一直以来,我都在逃避着自己来到这个世界的使命...
    最好好阅读 1,072评论 0 0