Mybatis generator

一、引入maven plugin

<build>
<plugins>
    <!--mybatis自动生成代码插件-->
    <plugin>
        <groupId>org.mybatis.generator</groupId>
        <artifactId>mybatis-generator-maven-plugin</artifactId>
        <version>1.3.7</version>
        <configuration>
            <configurationFile>src/main/resources/mybatis-generator-config.xml</configurationFile>
            <verbose>true</verbose>
            <overwrite>true</overwrite>
        </configuration>
    </plugin>
</plugins>
</build>

二、修改配置文件

新建配置文件放到src/main/resources/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">

<!-- mybatis自动生成配置参考: http://mybatis.org/generator/index.html-->

<generatorConfiguration>
    <!--导入属性配置-->
<!--    <properties resource="datasource.properties"/>-->

    <!--指定特定数据库的jdbc驱动jar包的位置 这里要修改自己本地的mysql驱动包-->
    <classPathEntry
            location="C:\repository\mysql\mysql-connector-java\5.1.44\mysql-connector-java-5.1.44.jar"/>

    <context id="default" targetRuntime="MyBatis3">

        <plugin type="org.mybatis.generator.plugins.FluentBuilderMethodsPlugin"></plugin>
        <plugin type="org.mybatis.generator.plugins.ToStringPlugin"></plugin>
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin>
<!--        This plugin will add a new version of the selectByExample method that accepts a RowBounds parameter. This supports the MyBatis RowBounds function where a returned result list can be limited in length, and a start position can be specified. This can be useful in paging applications.-->
        <plugin type="org.mybatis.generator.plugins.RowBoundsPlugin"></plugin>

        <!-- optional,旨在创建class时,对注释进行控制-->
        <commentGenerator>
            <!--是否生成注释-->
            <property name="suppressAllComments" value="true"/>
            <!-- 注释里是否显示代码生成日期 -->
            <property name="suppressDate" value="true"/>
            <!-- 显示数据库注释 -->
            <property name="addRemarkComments" value="true"/>
        </commentGenerator>

        <!--jdbc的数据库连接,直接写死也可以 -->
        <jdbcConnection
                driverClass="com.mysql.jdbc.Driver"
                connectionURL="jdbc:mysql://localhost:3306/xxx?useSSL=false"
                userId="xxx"
                password="xxx">
        </jdbcConnection>

        <!-- 非必需,类型处理器,在数据库类型和java类型之间的转换控制-->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>


        <!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
            targetPackage     指定生成的model生成所在的包名
            targetProject     指定在该项目下所在的路径
        -->
        <javaModelGenerator targetPackage="com.xxx.dao.model" targetProject="src/main/java">
            <!-- 是否允许子包,即targetPackage.schemaName.tableName -->
            <property name="enableSubPackages" value="false"/>
            <!-- 是否对model添加 构造函数 -->
            <property name="constructorBased" value="false"/>
            <!-- 建立的Model对象是否 不可改变  即生成的Model对象不会有 setter方法,只有构造方法 -->
            <property name="immutable" value="false"/>
            <!-- 设置是否在getter方法中,对String类型字段调用trim()方法 -->
            <property name="trimStrings" value="false"/>
        </javaModelGenerator>

        <!--mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
        <sqlMapGenerator targetPackage="com.xxx.dao.mapper" targetProject="src/main/resources">
            <property name="enableSubPackages" value="false"/>
        </sqlMapGenerator>

        <!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
                type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
                type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
                type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口
        -->

        <!-- targetPackage:mapper接口dao生成的位置 -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.xxx.dao" targetProject="src/main/java">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false"/>
        </javaClientGenerator>
        

        <table tableName="t_travel_node" domainObjectName="TravelNode"
               enableCountByExample="false"
               enableUpdateByExample="false"
               enableDeleteByExample="false"
               enableSelectByExample="false"
               selectByExampleQueryId="false"
        />
    </context>
</generatorConfiguration>

三、生成代码

执行插件命令

mvn mybatis-generator:generate
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容