Mybatis-generator 简单入门

631 阅读1分钟

链接

官网:mybatis.org/generator/i…

简介

逆向工程代码生成器。

使用

下载 core 包

我使用的是gradle项目,就直接用项目下载了,记得完成后要删除引用

implementation group: 'org.mybatis.generator', name: 'mybatis-generator-core', version: '1.4.1'

找到项目中的jar包,打开路径 copy

image.png

放进你喜欢的文件夹中,我创建一个叫mybatis-generator文件夹

下载postgres jdbc驱动包

image.png 放入文件夹中

编辑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>
    <!-- <properties resource="mybatis-generator/generator.properties"/> -->
    <!--    连接数据库jar包的路径-->
    <classPathEntry location="C:/Users/shouyang.lv/Desktop/mybatis-generator/postgresql-42.4.0.jar"/>
    <context id="DB2Tables"  targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="false"/>
        </commentGenerator>

        <!--数据库连接参数 -->
        <jdbcConnection
                driverClass="org.postgresql.Driver"
                connectionURL="jdbc:postgresql://127.0.0.1:54322/postgres"
                userId="postgres"
                password="123456">
        </jdbcConnection>

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

        <!-- 实体类的包名和存放路径 -->
        <javaModelGenerator targetPackage="com.lv.testmp.entity" targetProject="source">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>

        <!-- 生成映射文件*.xml的位置-->
        <sqlMapGenerator targetPackage="resources" targetProject="source">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>

        <!-- 生成DAO的包名和位置 -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.lv.testmp.dao" targetProject="source">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>

        <!-- tableName:数据库中的表名或视图名;domainObjectName:生成的实体类的类名-->
        <table tableName="users" domainObjectName="User"
               enableCountByExample="false"
               enableUpdateByExample="false"
               enableDeleteByExample="false"
               enableSelectByExample="false"
               selectByExampleQueryId="false"/>
        <!--
                <table tableName="xxx" domainObjectName="xxx"
                       enableCountByExample="false"
                       enableUpdateByExample="false"
                       enableDeleteByExample="false"
                       enableSelectByExample="false"
                       selectByExampleQueryId="false"/>
                ...
                <table tableName="xxx" domainObjectName="xxx"
                       enableCountByExample="false"
                       enableUpdateByExample="false"
                       enableDeleteByExample="false"
                       enableSelectByExample="false"
                       selectByExampleQueryId="false"/>
        -->
    </context>
</generatorConfiguration>

创建一个接收生成代码的文件夹,并配置到xml中,我的叫source

执行代码生成命令

java -jar .\mybatis-generator-core-1.4.1.jar -configfile .\generatorConfig.xml -overwrite
MyBatis Generator finished successfully.

成功

image.png

注意

1.代码生成前要检查配置文件中的配置是否正确
2.只能生成已经存在数据库中的表

转载需注明出处,抄袭必究