eclipse 使用 mybatis generator 插件生成 MyBatis 逆向工程

版权声明:转载请标注哦!^v^ https://blog.csdn.net/qq_36474549/article/details/81476389

步骤:


1.新建Java工程,添加config包
2.在config包中添加generratorConfigure文件
3.在config包中新建properties包,并添加properties文件

完成后的目录:
这里写图片描述


配置文件的的代码:
generatorConfigure.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="properties/connection.properties" />

    <!-- 数据库驱动 -->
    <classPathEntry location="${config_location}" />

    <!-- 2.数据库JDBC的连接信息 -->
    <context id="DB2Tables" targetRuntime="MyBatis3">

        <commentGenerator>
            <!-- 是否生成注释代时间戳 -->
            <property name="suppressDate" value="true" />
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="true" />
        </commentGenerator>

        <!--数据库链接URL,用户名、密码 -->
        <jdbcConnection driverClass="${config_driver_class}"
            connectionURL="${config_connection_url}" userId="${config_userId}"
            password="${config_password}" />

        <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 
            和 NUMERIC 类型解析为java.math.BigDecimal -->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>

        <!-- 生成模型的包名和位置 -->
        <!-- 要修改1次 -->
        <!-- 生成实体类地址 -->
        <javaModelGenerator
            targetPackage="${config_model_generator}"
            targetProject="${config_targetProject}">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="true" />
            <!-- 从数据库返回的值被清理前后的空格 -->
            <property name="trimStrings" value="true" />
        </javaModelGenerator>

        <!-- 生成映射文件的包名和位置 -->
        <!-- 要修改1次 -->
        <!-- 生成mapper.xml文件 (Mapper接口) -->
        <sqlMapGenerator
            targetPackage="${config_map_generator}"
            targetProject="${config_targetProject}">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>

        <!-- 生成DAO的包名和位置 -->
        <!-- 要修改1次 -->
        <!-- 生成mapxml对应client,也就是接口dao -->
        <javaClientGenerator type="XMLMAPPER"
            targetPackage="${config_client_generator}"
            targetProject="${config_targetProject}">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>

        <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名 -->
        <!-- 每次生成都要修改 -->
        <table tableName="${config_table_name}"
            domainObjectName="${config_Object_name}"
            enableInsert="true" 
            enableDeleteByPrimaryKey="false" 
            enableSelectByPrimaryKey="false" 
            enableUpdateByPrimaryKey="false"  

            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false" >

            <!-- 设置实体类属性名称与数据表字段相同 -->
            <property name="useActualColumnNames" value="true" />
        </table>

    </context>
</generatorConfiguration>  

connection.properties文件

######################-修改一次-######################

## 引入jar包(本文章下面会说到),绝对位置
##G:\\A_DB\\mysql-connector-java-8.0.8-dmr.jar
##G:\\A_DB\\mysql-connector-java-5.1.7-bin.jar
config_location= G:\\A_DB\\mysql-connector-java-5.1.7-bin.jar


## 数据库连接
config_driver_class= com.mysql.jdbc.Driver
## 数据库地址
config_connection_url= jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
## 数据库名称
config_userId= root
## 数据库密码
config_password= root


######################-每次都要修改-######################

## 项目名
config_targetProject= XXX
## 实体类位置
config_model_generator= a.entity.inventory
## mapper文件位置(需复制)
config_map_generator= a.mybatis.inventory
## DAO接口位置
config_client_generator= a.service.inventory

##数据表名称
config_table_name1=
config_table_name2=
config_table_name3=
config_table_name4=
config_table_name5=
config_table_name6=
config_table_name7=

##实体类名称
config_Object_name1=
config_Object_name2=
config_Object_name3=
config_Object_name4=
config_Object_name5=
config_Object_name6=
config_Object_name7=



PROJECT_SCHEMA=riseuser-dao

db_driver=com.mysql.jdbc.Driver


辅助文件 table.xml
(用于快速复制表内配置到generatorConfigure.xml文件中)

<table tableName="${config_table_name1}"
        domainObjectName="${config_Object_name1}"
            enableInsert="true" 
            enableDeleteByPrimaryKey="false"        
            enableSelectByPrimaryKey="false"        
            enableUpdateByPrimaryKey="false"  

            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false" >

        <!-- 设置实体类属性名称与数据表字段相同 -->
        <property name="useActualColumnNames" value="true" />
    </table>

    <table tableName="${config_table_name2}"
        domainObjectName="${config_Object_name2}"       
            enableInsert="true" 
            enableDeleteByPrimaryKey="false"        
            enableSelectByPrimaryKey="false"        
            enableUpdateByPrimaryKey="false"  

            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false" >
        <!-- 设置实体类属性名称与数据表字段相同 -->
        <property name="useActualColumnNames" value="true" />
    </table>

    <table tableName="${config_table_name3}"
        domainObjectName="${config_Object_name3}" 
            enableInsert="true" 
            enableDeleteByPrimaryKey="false"        
            enableSelectByPrimaryKey="false"        
            enableUpdateByPrimaryKey="false"  

            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false" >

        <!-- 设置实体类属性名称与数据表字段相同 -->
        <property name="useActualColumnNames" value="true" />
    </table>

    <table tableName="${config_table_name4}"
        domainObjectName="${config_Object_name4}" 
            enableInsert="true" 
            enableDeleteByPrimaryKey="false"        
            enableSelectByPrimaryKey="false"        
            enableUpdateByPrimaryKey="false"  

            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false" >
        <!-- 设置实体类属性名称与数据表字段相同 -->
        <property name="useActualColumnNames" value="true" />
    </table>

    <table tableName="${config_table_name5}"
        domainObjectName="${config_Object_name5}" 
                    enableInsert="true" 
            enableDeleteByPrimaryKey="false"        
            enableSelectByPrimaryKey="false"        
            enableUpdateByPrimaryKey="false"  

            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false" >

        <!-- 设置实体类属性名称与数据表字段相同 -->
        <property name="useActualColumnNames" value="true" />
    </table>

    <table tableName="${config_table_name6}"
        domainObjectName="${config_Object_name6}" 
                    enableInsert="true" 
            enableDeleteByPrimaryKey="false"        
            enableSelectByPrimaryKey="false"        
            enableUpdateByPrimaryKey="false"  

            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false" >
        <!-- 设置实体类属性名称与数据表字段相同 -->
        <property name="useActualColumnNames" value="true" />
    </table>

    <table tableName="${config_table_name7}"
        domainObjectName="${config_Object_name7}" 
            enableInsert="true" 
            enableDeleteByPrimaryKey="false"        
            enableSelectByPrimaryKey="false"        
            enableUpdateByPrimaryKey="false"  

            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false" >

        <!-- 设置实体类属性名称与数据表字段相同 -->
        <property name="useActualColumnNames" value="true" />
    </table>

    <table tableName="${config_table_name8}"
        domainObjectName="${config_Object_name8}" 
            enableInsert="true" 
            enableDeleteByPrimaryKey="false"        
            enableSelectByPrimaryKey="false"        
            enableUpdateByPrimaryKey="false"  

            enableCountByExample="false" enableUpdateByExample="false"
            enableDeleteByExample="false" enableSelectByExample="false"
            selectByExampleQueryId="false" >
            </table>

最后右击generratorConfigure.xml文件—> Run As —> Run Mybatis Generator
即可在定义的项目中生成相应的 Mapper接口,java bean, dao 文件
这里写图片描述


生成的文件
这里写图片描述

项目源码:

猜你喜欢

转载自blog.csdn.net/qq_36474549/article/details/81476389