Mybatis Generator自动生成代码

Mybatis属于半自动ORM,在使用这个框架中,工作量最大的就是书写Mapping的映射文件,由于手动书写很容易出错,我们可以利用Mybatis-Generator来帮我们自动生成文件;

1、下载Mybatis Generator

关于Mybatis-Generator的下载可以到这个地址:https://github.com/mybatis/generator/releases
由于我使用的是Mysql数据库,这里需要在准备一个连接mysql数据库的驱动jar包
对下载的压缩包进行解压,解压后在lib目录下有三个jar文件,分别为:
mybatis-generator-core-1.3.5.jar、mybatis-generator-core-1.3.5-javadoc.jar、mybatis-generator-core-1.3.5-sources.jar

2、Mybatis Generator使用

2.1、generatorConfig.xml 配置详解

和Hibernate逆向生成一样,这里也需要一个配置文件:
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.0.8-bin.jar"/>
    <context id="DB2Tables"    targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <!--数据库链接地址账号密码-->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost/mymessages" userId="root" password="root">
        </jdbcConnection>
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <!--生成Model类存放位置-->
        <javaModelGenerator targetPackage="lcw.model" targetProject="src">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!--生成映射文件存放位置-->
        <sqlMapGenerator targetPackage="lcw.mapping" targetProject="src">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!--生成Dao类存放位置-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="lcw.dao" targetProject="src">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <!--生成对应表及类名-->
        <table tableName="message" domainObjectName="Messgae" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
    </context>
</generatorConfiguration>

需要修改文件配置的地方我都已经把注释标注出来了,这里的相关路径(如数据库驱动包,生成对应的相关文件位置可以自定义)不能带有中文。
上面配置文件中的:

<table tableName="message" domainObjectName="Messgae" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>

tableName和domainObjectName为必选项,分别代表数据库表名和生成的实力类名,其余的可以自定义去选择(一般情况下均为false)
注意事项:targetProject="src" 必须提前创建好;
附:mysql jdbc驱动下载地址https://dev.mysql.com/downloads/connector/j/5.0.html

2.2、使用

配置好之后如何来使用GeneratorConfig.xml,使用以下命令即可进行执行创建操作;

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

也可将上述命令写入txt文档,将后缀名改为.bat,保存后双击运行即可;
注意:格式为UTF-8
截止到这里,Mybatis Generator的初步使用到一段落;

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

推荐阅读更多精彩内容

  • 写在前面的话 当你开发时间长了,你会发现在ORM这一层其实有很多代码都不用写,可以交给一些插件代为生成。这样当你设...
    cjxz阅读 566评论 0 0
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,891评论 18 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,947评论 6 342
  • 今天天气很好,傍晚时分骑着自行车从外经贸到左家庄的时候路过了民族出版大楼。这栋大楼很显眼,是当初的大屋顶建筑,也就...
    招财小能手阅读 197评论 1 2
  • 《豆福传》,一个豆人追逐理想成功后在镇上卖豆腐的励志动画电影,适合小孩子看,因为它的道理很简单,可以说是俗套;也适...
    鳗鱼吉吉阅读 1,196评论 0 0