天天看點

Mybatis(3)使用eclipse、mybatis-generator-maven-plugin(1.3.6)插件逆向工程

我的環境

  • eclipse4.7
  • mybatis-generator-maven-plugin1.3.6
  • mysql5.1

逆向工程需要自己準備一張資料庫表

生成步驟

1.建立一個maven項目

maven的pom.xml檔案:

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>yu.lin.ru.mybatis</groupId>
    <artifactId>mybatis-generator</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.6</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>KenShrio</finalName>
        <defaultGoal>compile</defaultGoal>
        <plugins>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.6</version>
                <executions>
                    <execution>
                        <id>Generate MyBatis Artifacts</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
                <dependencies>
                    <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>5.1.46</version>
                    </dependency>
                </dependencies>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
           

2.在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">
<generatorConfiguration>

    <context id="mybatis-generator-1.3.6">

        <!--自動實作序列化接口 -->
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin" />

        <!--生成toString方法 -->
        <plugin type="org.mybatis.generator.plugins.ToStringPlugin" />

        <!--生成equals和hashCode方法 -->
        <plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin" />

        <commentGenerator>
            <!-- 去掉日期注釋 -->
            <property name="suppressDate" value="true" />
            <!-- 資料庫描述生成 -->
            <property name="addRemarkComments" value="true" />
        </commentGenerator>

        <jdbcConnection
            connectionURL="jdbc:mysql://127.0.0.1:3308/km?characterEncoding=UTF-8"
            driverClass="com.mysql.jdbc.Driver" password="password" userId="root" />

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

        <javaModelGenerator
            targetPackage="yu.lin.ru.knowledge.core.model" targetProject="target">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
        <sqlMapGenerator
            targetPackage="yu.lin.ru.knowledge.core.mapping" targetProject="target">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>
        <javaClientGenerator type="XMLMAPPER"
            targetPackage="yu.lin.ru.knowledge.core.dao" targetProject="target">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>
        <table tableName="sys_user" domainObjectName="SUser" />
    </context>
</generatorConfiguration>
           

3.使用指令生成檔案

//生成名稱
mybatis-generator:generate
           

生成指令截圖

Mybatis(3)使用eclipse、mybatis-generator-maven-plugin(1.3.6)插件逆向工程

4.成功生成截圖

Mybatis(3)使用eclipse、mybatis-generator-maven-plugin(1.3.6)插件逆向工程

繼續閱讀