Maven項目使用MyBatis Generator插件生成POJO和Mapper代碼

0x0 前言

MyBatis Generator可以幫助我們根據數據庫表生成相應的實體類和Mapper,這大大減少了我們的代碼量,提升了開發效率。

在Eclipse下安裝一個MyBatis generator插件就就可以完美生成pojo和mapper;在idea下雖然也有相應的插件工具(功能也比較強大),但是如果我們想用原生的mybatis generator生成怎麼做呢?官方的MyBatis Generator使用教程提供了多種方法,其中一種就是Maven插件。

0x1 使用教程

一、首先,要在pom文件中配置一個maven mybatis generator插件:

<build>
        <plugins>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.5</version>
                <configuration>
                    <configurationFile>這裏放你的generator配置文件路徑</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>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>5.1.42</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>

    </build>

要注意的是,插件裏要依賴你的jdbc驅動jar包。

二、 使用插件:

這裏寫圖片描述

三、關於mybatis-generator配置文件
給個模板

<?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="context1">
        <plugin type="org.mybatis.generator.plugins.ToStringPlugin" />
        <commentGenerator>
            <!-- 這個元素用來去除指定生成的註釋中是否包含生成的日期 false:表示保護 -->
            <!-- 如果生成日期,會造成即使修改一個字段,整個實體類所有屬性都會發生變化,不利於版本控制,所以設置爲true -->
            <property name="suppressDate" value="true" />
            <!-- 是否去除自動生成的註釋 true:是 : false:否 -->
            <property name="suppressAllComments" value="true" />
        </commentGenerator>
        <!-- jdbc四大件 -->
        <jdbcConnection connectionURL="jdbc:mysql://localhost:3306/mall?useSSL=false"
                        driverClass="com.mysql.jdbc.Driver"
                        password="root"
                        userId="root" />
        <javaTypeResolver>
            <!-- This property is used to specify whether MyBatis Generator should
                force the use of java.math.BigDecimal for DECIMAL and NUMERIC fields, -->
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>
        <!-- 生成的pojo包名和路徑 -->
        <javaModelGenerator targetPackage="com.fly.pojo"
                            targetProject="src\main\java" >

        </javaModelGenerator>
        <!-- 映射文件 -->
        <sqlMapGenerator targetPackage="com.fly.dao"
                         targetProject="src\main\resources" />
        <!-- dao -->
        <javaClientGenerator targetPackage="com.fly.dao"
                             targetProject="src\main\java" type="XMLMAPPER" />
        <!-- 哪些表 -->
        <table schema="yycg" tableName="tb_user" domainObjectName="User" mapperName="UserDao"/>
        <table schema="yycg" tableName="tb_content" domainObjectName="Content" mapperName="ContentDao"/>
        <table schema="yycg" tableName="tb_content_category" domainObjectName="ContentCategory" mapperName="ContentCategoryDao"/>
        <table schema="yycg" tableName="tb_item" domainObjectName="Item" mapperName="ItemDao"/>
        <table schema="yycg" tableName="tb_item_cat" domainObjectName="ItemCat" mapperName="ItemCatDao"/>

    </context>
</generatorConfiguration>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章