springboot+mybatis使用Mybatis-Generator工具生成mapper、model、接口等文件

  • 由于我使用的是Mysql数据库,这里需要在准备一个连接mysql数据库的驱动jar包

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

  和Hibernate逆向生成一样,这里也需要一个配置文件:

<?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.46.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:3306/business" userId="root" password="root">
        </jdbcConnection>

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

         <!--生成Model类存放位置-->
        <javaModelGenerator targetPackage="com" targetProject="src">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>

        <!--生成映射文件存放位置-->
        <sqlMapGenerator targetPackage="com" targetProject="src">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>

         <!--生成Dao类存放位置-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com" targetProject="src">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>

         <!--生成对应表及类名-->
        <table tableName="user_info" domainObjectName="user_info" enableCountByExample="false" enableUpdateByExample="false" 
            enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" >
            <property name="useActualColumnNames" value="false"/>
            <generatedKey column="id" sqlStatement="MySql" identity="false"/>
        </table>
    </context>
</generatorConfiguration>

  上面的tableName和domainObjectName,分别为数据库表名,和实体名称,这两个为必填项,其余的可以自定义去选择(一般情况下均为false)

  • 使用方法

  在该目录按住Shift键,右键鼠标选择"在此处打开命令窗口",复制粘贴生成语句的文件代码即可。

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

  下载地址:https://download.csdn.net/download/haojuntu/13102664

  转载:https://www.cnblogs.com/zorro-y/p/5602471.html

原文地址:https://www.cnblogs.com/personblog/p/13955017.html