Mybatis Generator 代码生成器

Mybatis Generator 是 MyBatis 官方提供的一个代码生成工具, 使用方法比较简单,这里介绍一种简单粗暴的方法。

1.在resources文件夹下新建mybatis-generator文件夹,文件夹内容为所需的jar包和配置文件:

 

 2.下面附上配置文件详情:

mybatis-generator-config.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>
    <!-- 指定数据连接驱动jar地址 -->
    <classPathEntry location="mysql-connector-java-5.1.35-bin.jar"/>
    <!-- 一个数据库一个context -->
    <context id="BuildingTables" targetRuntime="MyBatis3Simple" defaultModelType="flat">

        <property name="autoDelimitKeywords" value="true"/>
        <property name="beginningDelimiter" value="`"/>
        <property name="endingDelimiter" value="`"/>

        <!-- defaultModelType="hierarchical" -->
        <property name="javaFileEncoding" value="UTF-8"/>
        <property name="suppressTypeWarnings" value="true"/>

        <!-- 序列化 -->
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin"/>
        <!--<plugin type="org.mybatis.generator.plugins.CaseInsensitiveLikePlugin"/>
        <plugin type="org.mybatis.generator.plugins.RowBoundsPlugin"/>
        <plugin type="org.mybatis.generator.plugins.ToStringPlugin"/>
        <plugin type="org.mybatis.generator.plugins.CachePlugin"/>-->

        <plugin type="org.mybatis.generator.plugins.RenameExampleClassPlugin">
            <property name="searchString" value="Example$"/>
            <property name="replaceString" value="Criteria"/>
        </plugin>

        <commentGenerator type="com.hxkj.mybatis.config.MyCommentGenerator">
            <property name="javaFileEncoding" value="UTF-8"/>
            <property name="suppressAllComments" value="false"/>
            <property name="suppressDate" value="true"/>
        </commentGenerator>
        <!-- 注释 -->

        <!-- jdbc连接 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://192.168.8.11:3306/organization?useUnicode=true&amp;characterEncoding=utf-8"
                        userId="gfg" password="gfg"/>
        <!-- 类型转换 -->
        <javaTypeResolver>
            <!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) -->
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>

        <!-- 生成实体类地址 -->
        <javaModelGenerator targetPackage="com.gfg.quality.entity" targetProject="../../java">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="true"/>
        </javaModelGenerator>

        <!-- 生成mapxml文件 -->
        <sqlMapGenerator targetPackage="mybatis.mapper.quality" targetProject="../../resources">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>

        <!-- 生成mapxml对应mapper接口 -->
        <javaClientGenerator targetPackage="com.gfg.quality.dao" targetProject="../../java" type="XMLMAPPER">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>

        <!-- tableName:用于自动生成代码的数据库表;domainObjectName:对应于数据库表的javaBean类名 -->
        <table tableName="user" enableInsert="false" enableDeleteByPrimaryKey="false" enableSelectByPrimaryKey="false"/>
    </context>
</generatorConfiguration>
复制代码

 

run.bat 脚本详情:

java -jar mybatis-generator-core-1.3.5.jar -configfile mybatis-generator-config.xml -overwrite
pause

 

jar包:

链接:https://pan.baidu.com/s/17z_QgjiD-lBdni80PnH2ng?pwd=sne9
提取码:sne9

 

3.执行run.bat,执行后可以发现代码生成成功了。(当然也可以通过配置pom文件,使用插件生成的方式,两种方法都差不多)

 

posted @   高富贵  阅读(489)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
点击右上角即可分享
微信分享提示