java代码mybatis代码生成器插件

引言

mybatis-generator-maven-plugin、MyBatisPlus 对比。

1、mybatis-generator-maven-plugin插件深度更深,对mybatis提供的操作更仔细,不仅有简单的增删改查,还可以动态的使用where语句来操作。

2、MyBatisPlus插件主要提供了一些模板来操作,主要的模板有:velocity 模板引擎, 默认、freemarker 模板引擎、beetl 模板引擎等。生成一些常用的基于sql自动生成domain、controller、service、dao、mapper,只是框架模型,具体操作很少。

mybatis-generator-maven-plugin

官方文档:http://www.mybatis.org/generator/index.html

1、pom.xml中要引入的jar包

<!-- mybatis-->
       <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.1</version>
        </dependency>    

        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.3.7</version>
        </dependency>       



 <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
       
            <!-- mybatis generator 自动生成代码插件 -->
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.7</version>
                <configuration>
                    <configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile>
                    <!-- 是否覆盖 -->
                    <overwrite>true</overwrite>
                    <!--允许移动生成的文件 -->
                    <verbose>true</verbose>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>5.1.6</version>
                    </dependency>
                    <!--集成gen使用的通用mapper依赖-->
                    <dependency>
                        <groupId>tk.mybatis</groupId>
                        <artifactId>mapper</artifactId>
                        <version>4.1.5</version>
                    </dependency>
                </dependencies>
            </plugin>

        </plugins>
    </build>

2.配置默认文件 generatorConfig.xml 此文件放置在 src/main/resources 下

<?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-generator-maven-plugin插件配置-->
<generatorConfiguration>
    <!-- 导入配置文件 -->
   <!-- <properties resource="application.yml"/>-->

    <!-- defaultModelType="flat" 设置复合主键时不单独为主键创建实体 -->
    <!--<context id="MySql" defaultModelType="flat">-->

    <!-- 数据库驱动:选择你的本地硬盘上面的数据库驱动包-->
    <classPathEntry  location="C:\Users\Administrator\.m2\repository\mysql\mysql-connector-java\5.1.46\mysql-connector-java-5.1.46.jar"/>
    <context id="DB2Tables"  targetRuntime="MyBatis3">



        <!-- 自动识别数据库关键字,默认false,如果设置为true,根据SqlReservedWords中定义的关键字列表;
            一般保留默认值,遇到数据库关键字(Java关键字),使用columnOverride覆盖
         -->
        <property name="autoDelimitKeywords" value="false"/>
        <!-- 生成的Java文件的编码 -->
        <property name="javaFileEncoding" value="UTF-8"/>
        <!-- 格式化java代码 -->
        <property name="javaFormatter" value="org.mybatis.generator.api.dom.DefaultJavaFormatter"/>
        <!-- 格式化XML代码 -->
        <property name="xmlFormatter" value="org.mybatis.generator.api.dom.DefaultXmlFormatter"/>

       

        <!-- 生成的pojo,将implements Serializable -->
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin"/>
        <!--生成的java对象模型自动添加hashCode()和Equals()方法-->
        <plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin"/>
        <!--生成的java对象模型自动添加toString()方法-->
        <plugin type="org.mybatis.generator.plugins.ToStringPlugin"/>
        <!--直接生成映射注解-->
        <plugin type="org.mybatis.generator.plugins.MapperAnnotationPlugin"/>
        <!--开启支持内存分页   可生成 支持内存分布的方法及参数-->
        <plugin type="org.mybatis.generator.plugins.RowBoundsPlugin"/>


       <!--下面的插件需要把插件的jar包自己手动添加在本地mven环境中-->
       <!--使用自定义的分页插件需要搭乘jar包在,安装到仓库,在导入pom.xml中使用 -->
       <!-- 批量插入插件 -->
       <!-- <plugin type="com.itfsw.mybatis.generator.plugins.BatchInsertPlugin">
            &lt;!&ndash;
            开启后可以实现官方插件根据属性是否为空决定是否插入该字段功能
            !需开启allowMultiQueries=true多条sql提交操作,所以不建议使用!插件默认不开启
            &ndash;&gt;
            <property name="allowMultiQueries" value="false"/>
        </plugin>-->
        <!--批量更新-->
        <!--<plugin type="cc.bandaotixi.plugins.BatchUpdatePlugin"/>-->
        <!-- 生成一对一配置 -->
        <!--<plugin type="cc.bandaotixi.plugins.OneToOnePlugin"/>-->
        <!-- 生成一对多配置 -->
        <!--<plugin type="cc.bandaotixi.plugins.OneToManyPlugin"/>-->


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

        <commentGenerator>
            <!-- 将数据库中表的字段描述信息添加到注释 -->
            <property name="addRemarkComments" value="true"/>
            <!-- 注释里不添加日期 -->
            <property name="suppressDate" value="true"/>
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="false"/>
        </commentGenerator>

        <!--数据库链接URL,用户名、密码 -->
        <!-- 数据库连接,直接通过${}读取application.properties里的配置 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://192.168.0.18/meter_basis" userId="root" password="root"></jdbcConnection>
       <!-- <jdbcConnection
                driverClass="${spring.datasource.druid.driver-class-name}"
                connectionURL="${spring.datasource.druid.url}"
                userId="${spring.datasource.druid.username}"
                password="${spring.datasource.druid.password}">
        </jdbcConnection>-->

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

        <!-- 生成生成POJO对象模型的包名和位置-->
        <javaModelGenerator targetPackage="com.kmnfsw.generator.domain" targetProject="src/main/java">
        <!-- 在targetPackage的基础上,根据数据库的schema再生成一层package,最终生成的类放在这个package下,默认为false -->
            <property name="enableSubPackages" value="true"/>
            <!-- 设置是否在getter方法中,对String类型字段调用trim()方法 -->
            <property name="trimStrings" value="true"/>
            <!--  for MyBatis3/MyBatis3Simple
            自动为每一个生成的类创建一个构造方法,构造方法包含了所有的field;而不是使用setter; -->
            <property name="constructorBased" value="false"/>
        </javaModelGenerator>

        <!-- 生成mapper xml映射文件的包名和位置-->
        <sqlMapGenerator targetPackage="generator/mapper" targetProject="src/main/resources">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>

        <!-- 生成DAO的包名和位置-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.kmnfsw.generator.dao" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>

        <!-- table标签可以有多个,至少一个,tableName指定表名,可以使用_和%通配符 -->
        <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->
        <table tableName="channel_early_warning_type" domainObjectName="ChannelEarlyWarningtType" enableCountByExample="true" enableUpdateByExample="true" enableDeleteByExample="true" enableSelectByExample="true" selectByExampleQueryId="true"></table>
        <table tableName="channel_info" domainObjectName="ChannelInfo" enableCountByExample="true" enableUpdateByExample="true" enableDeleteByExample="true" enableSelectByExample="true" selectByExampleQueryId="true"></table>
        <table tableName="channel_link_notice" domainObjectName="ChannelLinkNotice" enableCountByExample="true" enableUpdateByExample="true" enableDeleteByExample="true" enableSelectByExample="true" selectByExampleQueryId="true"></table>
        <table tableName="channel_range_early_warning" domainObjectName="ChannelRangeEarlyWarning" enableCountByExample="true" enableUpdateByExample="true" enableDeleteByExample="true" enableSelectByExample="true" selectByExampleQueryId="true"></table>
        <table tableName="channel_switch_early_warning" domainObjectName="ChannelSwitchEarlyWarning" enableCountByExample="true" enableUpdateByExample="true" enableDeleteByExample="true" enableSelectByExample="true" selectByExampleQueryId="true"></table>
        <table tableName="channel_value_early_warning" domainObjectName="ChannelValueEarlyWarning" enableCountByExample="true" enableUpdateByExample="true" enableDeleteByExample="true" enableSelectByExample="true" selectByExampleQueryId="true"></table>
        <table tableName="device_info" domainObjectName="DeviceInfo" enableCountByExample="true" enableUpdateByExample="true" enableDeleteByExample="true" enableSelectByExample="true" selectByExampleQueryId="true"></table>
        <table tableName="equipment_info" domainObjectName="EquipmentInfo" enableCountByExample="true" enableUpdateByExample="true" enableDeleteByExample="true" enableSelectByExample="true" selectByExampleQueryId="true"></table>
        <table tableName="group_link_notice" domainObjectName="GroupLinkNotice" enableCountByExample="true" enableUpdateByExample="true" enableDeleteByExample="true" enableSelectByExample="true" selectByExampleQueryId="true"></table>
        <table tableName="inform_person_group_info" domainObjectName="InformPersonGroupInfo" enableCountByExample="true" enableUpdateByExample="true" enableDeleteByExample="true" enableSelectByExample="true" selectByExampleQueryId="true"></table>
        <table tableName="meter_info" domainObjectName="MeterInfo" enableCountByExample="true" enableUpdateByExample="true" enableDeleteByExample="true" enableSelectByExample="true" selectByExampleQueryId="true"></table>
        <table tableName="tag_group_info" domainObjectName="TagGroupInfo" enableCountByExample="true" enableUpdateByExample="true" enableDeleteByExample="true" enableSelectByExample="true" selectByExampleQueryId="true"></table>
    </context>
</generatorConfiguration>

4、在mevn中直接执行即可

MyBatisPlus 

这个插件的使用直接看这些篇博客即可,已测试,可以生成。

https://www.cnblogs.com/douJiangYouTiao888/p/9266727.html

扫描二维码关注公众号,回复: 10407909 查看本文章

https://blog.csdn.net/Neal_/article/details/86030791

官方文档:https://mp.baomidou.com/config/generator-config.html

发布了91 篇原创文章 · 获赞 20 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/qq_34227896/article/details/102724857