IDEA中使用MyBatis Generator

2018/03/26 13:49
阅读数 56

1.使用

如下主要介绍在IDEA 中使用MyBatis Generator 更多想使用请参考官网: http://www.mybatis.org/generator/
1. 新建一个Maven 项目在POM中添加

 <build>
       <plugins>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.6</version>
                <configuration>
                  <!--配置文件的位置-->
                    <configurationFile>src/main/resources/generator/generatorConfig.xml</configurationFile>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
                <executions>
                    <execution>
                        <id>Generate MyBatis Artifacts</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.mybatis.generator</groupId>
                        <artifactId>mybatis-generator-core</artifactId>
                        <version>1.3.6</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
  1. 新建generatorConfig.xml 配置内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "mybatis-generator-config_1_0.dtd">

<generatorConfiguration>
    <!--本地数据库驱动-->
    <classPathEntry location="E:\.m2\repository\mysql\mysql-connector-java\6.0.6\mysql-connector-java-6.0.6.jar"/>
    <context id="MysqlTables" targetRuntime="MyBatis3">
        <!--去除注释  -->
        <commentGenerator>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/junli?useUnicode=true&amp;characterEncoding=utf-8&amp;useSSL=false&amp;useJDBCCompliantTimezoneShift=true&amp;useLegacyDatetimeCode=false&amp;serverTimezone=UTC"
                        userId="root"
                        password="123456">
        </jdbcConnection>

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

        <!--实体存放位置-->
        <javaModelGenerator targetPackage="com.junly.mybatis.beans"
                            targetProject="G:\learnworkspace\learn-mybatis\src\main\java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>

        <!--XML存放位置-->
        <sqlMapGenerator targetPackage="xml" targetProject="G:\learnworkspace\learn-mybatis\src\main\resources">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>

        <!--Mapper存放位置-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.junly.mybatis.mapper"
                             targetProject="G:\learnworkspace\learn-mybatis\\src\main\java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>

         <!--需要生成的表-->
        <table schema="junli" tableName="posts" domainObjectName="Posts">
            <property name="useActualColumnNames" value="false"/>
        </table>
        <table schema="junli" tableName="test" domainObjectName="Test">
            <property name="useActualColumnNames" value="false"/>
        </table>
        <table schema="junli" tableName="blog" domainObjectName="Blog">
            <property name="useActualColumnNames" value="false"/>
        </table>

        <table schema="junli" tableName="author" domainObjectName="Author">
            <property name="useActualColumnNames" value="false"/>
        </table>

    </context>
</generatorConfiguration>
  1. 生成代码
    在完成以上两步之后。就会在idea中看到:直接点击mybatis-generator:generate就可生成。
    这里写图片描述
展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部