• Mybatis代码自动生成插件使用



    1.配置pom.xml

    添加mybatis-generator-maven-plugin到pom.xml。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    <?xml version="1.0" encoding="UTF-8"?>
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        <parent>
            <artifactId>orm</artifactId>
            <groupId>com.cdai.demo</groupId>
            <version>1.0-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
     
        <artifactId>mybatis-generator</artifactId>
     
        <dependencies>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.9</version>
            </dependency>
        </dependencies>
     
        <build>
            <plugins>
                <plugin>
                    <groupId>org.mybatis.generator</groupId>
                    <artifactId>mybatis-generator-maven-plugin</artifactId>
                    <version>1.3.2</version>
                    <configuration>
                        <verbose>true</verbose>
                        <overwrite>true</overwrite>
                    </configuration>
                </plugin>
            </plugins>
        </build>
     
    </project>



    2.插件配置文件

    插件配置文件默认读取src/main/resources/generatorConfig.xml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE generatorConfiguration
            PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
     
    <generatorConfiguration>
        <classPathEntry location="C:Userscdai.m2 epositorymysqlmysql-connector-java5.1.9mysql-connector-java-5.1.9.jar" />
     
        <context id="Test" targetRuntime="MyBatis3">
     
            <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                            connectionURL="jdbc:mysql://192.168.1.100:3306/demo?generateSimpleParameterMetadata=true"
                            userId="root"
                            password="">
            </jdbcConnection>
     
            <javaTypeResolver >
                <property name="forceBigDecimals" value="false" />
            </javaTypeResolver>
     
            <!-- generate Model -->
            <javaModelGenerator targetPackage="com.cdai.demo.orm.mybatis" targetProject="D:ideaspacesdemoormmybatis-generatorsrcmainjava">
                <property name="enableSubPackages" value="true" />
                <property name="trimStrings" value="true" />
            </javaModelGenerator>
     
            <!-- generate xml -->
            <sqlMapGenerator targetPackage="com.cdai.demo.orm.mybatis"  targetProject="D:ideaspacesdemoormmybatis-generatorsrcmain esources">
                <property name="enableSubPackages" value="true" />
            </sqlMapGenerator>
     
            <!-- generate Mapper -->
            <javaClientGenerator type="XMLMAPPER" targetPackage="com.cdai.demo.orm.mybatis"  targetProject="D:ideaspacesdemoormmybatis-generatorsrcmainjava">
                <property name="enableSubPackages" value="true" />
            </javaClientGenerator>
     
            <table schema="" tableName="account" domainObjectName="Account"></table>
     
        </context>
     
    </generatorConfiguration>



    3.运行插件

    运行代码生成插件的Maven任务:


    生成代码:


  • 相关阅读:
    戏说程序猿之荒唐的需求
    戏说程序猿之过年--二叔,我真不会修电脑
    深入理解设计模式(17):迭代器模式
    深入理解设计模式(16):备忘录模式
    我的2018
    Java高级篇(一)——线程
    Java进阶篇(六)——Swing程序设计(下)
    Java进阶篇(六)——Swing程序设计(上)
    Java进阶篇(五)——Java的I/O技术
    Java进阶篇(四)——Java异常处理
  • 原文地址:https://www.cnblogs.com/xiaomaohai/p/6157702.html
Copyright © 2020-2023  润新知