`

mybatis-generator

 
阅读更多
项目中使用 经常会用到一个orm框架-mybatis,使用到mybatis,就避免不了使用mybatis-generator。

一、添加mybatis依赖:
<dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>${mybatis.version}</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>


插件依赖配置

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.7</version>
                <configuration>
                    <!-- 配置文件 -->
                    <configurationFile>src/main/resources/mybatis-generator.xml</configurationFile>
                    <!-- 允许移动和修改 -->
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
                <dependencies>
                    <!-- jdbc 依赖 -->
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>5.1.46</version>
                    </dependency>

                    <dependency>
                        <groupId>com.itfsw</groupId>
                        <artifactId>mybatis-generator-plugin</artifactId>
                        <version>1.3.8</version>
                    </dependency>

                </dependencies>
            </plugin>


        </plugins>
    </build>


二、xml,以及properties配置
<?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-dbinfo.properties"/>

    <context id="Mysql" targetRuntime="MyBatis3" defaultModelType="flat">
        <!-- Mapper 直接覆盖, 不合并 -->
        <plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin" />
        <!-- Mapper 添加 @Mapper 注解 -->
        <plugin type="org.mybatis.generator.plugins.MapperAnnotationPlugin" />
        <!-- DO 生成 toString 方法 -->
        <plugin type="org.mybatis.generator.plugins.ToStringPlugin" />

        <!-- 插件介绍地址: https://gitee.com/whan0216/mybatis-generator-plugin -->
        <!-- 数据Model链式构建插件 -->
        <plugin type="com.itfsw.mybatis.generator.plugins.ModelBuilderPlugin"/>
        <!-- 数据Model属性对应Column获取插件 -->
        <plugin type="com.itfsw.mybatis.generator.plugins.ModelColumnPlugin"/>

        <!-- 查询单条数据插件 -->
        <plugin type="com.itfsw.mybatis.generator.plugins.SelectOneByExamplePlugin"/>
        <!-- MySQL分页插件 -->
        <plugin type="com.itfsw.mybatis.generator.plugins.LimitPlugin">
            <!-- 通过配置startPage影响Example中的page方法开始分页的页码,默认分页从0开始 -->
            <property name="startPage" value="0"/>
        </plugin>

        <!-- 批量插入插件 -->
        <plugin type="com.itfsw.mybatis.generator.plugins.BatchInsertPlugin">
            <!--
            开启后可以实现官方插件根据属性是否为空决定是否插入该字段功能
            !需开启allowMultiQueries=true多条sql提交操作,所以不建议使用!插件默认不开启
            -->
            <property name="allowMultiQueries" value="true"/>
        </plugin>
        <!-- Example Criteria 增强插件 -->
        <plugin type="com.itfsw.mybatis.generator.plugins.ExampleEnhancedPlugin">
            <!-- 是否支持已经过时的andIf方法(推荐使用when代替),默认支持 -->
            <property name="enableAndIf" value="true"/>
        </plugin>

        <commentGenerator>
            <property name="suppressAllComments" value="false"/>
            <property name="suppressDate" value="true"/>
            <property name="addRemarkComments" value="true"/>
        </commentGenerator>

        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="${datasource.url}"
                        userId="${datasource.username}"
                        password="${datasource.password}">
        </jdbcConnection>

        <javaModelGenerator targetPackage="com.example.mybatistest.demo.generator.domain"
                            targetProject="src/main/java"/>

        <sqlMapGenerator targetPackage="generator.mapper" targetProject="src/main/resources"/>

        <javaClientGenerator targetPackage="com.example.mybatistest.demo.generator.dao"
                             targetProject="src/main/java"
                             type="XMLMAPPER"/>

        <!-- 生成哪个表的代码就添加哪个表的配置 -->

        <table tableName="user" domainObjectName="User" enableCountByExample="true"
               enableUpdateByExample="true" enableSelectByExample="true" enableDeleteByExample="true"/>

    </context>
</generatorConfiguration>



datasource.username=root
datasource.password=123456
datasource.url=jdbc:mysql://localhost:3306/test_db?useSSL=false&useUnicode=true&characterEncoding=utf8



idea maven plugin 中就会出现 mybatis-generator ,运行即可生成代码



分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics