MyBatis Generator在Maven聚合項目中的最佳實踐

參考

http://mybatis.org/generator/running/runningWithMaven.html

https://github.com/GuoGuiRong/mybatis-generator-lombok-plugin

 

配置

在配置文件mybatis-generator.xml中新增一張數據庫表配置(shop_map)

<?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="DB2Tables" targetRuntime="MyBatis3">
        <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1:3306/bigscreen"
                        userId="root" password="root">
        </jdbcConnection>
        <javaModelGenerator targetPackage="com.dataintel.xa.dataobject" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <sqlMapGenerator targetPackage="mapping" targetProject="src/main/resources">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.dataintel.xa.dao" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>

        <table tableName="shop_map" domainObjectName="ShopMapDO" enableCountByExample="false"
               enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false"></table>
    </context>
</generatorConfiguration>

在父項目的pom中配置Mybatis插件

    <build>
        <pluginManagement>
            <plugins>
                <!--mybatis自動生成器-->
                <plugin>
                    <groupId>org.mybatis.generator</groupId>
                    <artifactId>mybatis-generator-maven-plugin</artifactId>
                    <version>1.3.5</version>
                    <dependencies>
                        <dependency>
                            <groupId>org.mybatis.generator</groupId>
                            <artifactId>mybatis-generator-core</artifactId>
                            <version>1.3.5</version>
                        </dependency>
                        <dependency>
                            <groupId>mysql</groupId>
                            <artifactId>mysql-connector-java</artifactId>
                            <version>5.1.41</version>
                        </dependency>
                    </dependencies>
                    <executions>
                        <execution>
                            <id>mybatis generator</id>
                            <phase>package</phase>
                            <goals>
                                <goal>generate</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <!--允許移動生成的文件-->
                        <verbose>true</verbose>
                        <!--允許自動覆蓋-->
                        <overwrite>true</overwrite>
                        <configurationFile>
                            src/main/resources/mybatis-generator.xml
                        </configurationFile>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

然後在子項目中引用父項目的插件依賴

    <build>
        <plugins>
            <!-- mybatis自動生成器 -->
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

這個時候你就會發現子項目的Maven中多了一個插件mybatis-generator:generate

運行

雙擊運行(BUILD SUCCESS)

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building web 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- mybatis-generator-maven-plugin:1.3.5:generate (default-cli) @ web ---
[INFO] Connecting to the Database
Fri Sep 27 16:34:02 CST 2019 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
[INFO] Introspecting table shop_map
[INFO] Generating Record class for table shop_map
[INFO] Generating Mapper Interface for table shop_map
[INFO] Generating SQL Map for table shop_map
[INFO] Saving file ShopMapDOMapper.xml
[INFO] Saving file ShopMapDO.java
[INFO] Saving file ShopMapDOMapper.java
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.289 s
[INFO] Finished at: 2019-09-27T16:34:02+08:00
[INFO] Final Memory: 16M/150M
[INFO] ------------------------------------------------------------------------

然後我們就可以去查看相關的目標文件是否自動生成,dao層文件如下:

package com.dataintel.xa.dao;

import com.dataintel.xa.dataobject.ShopMapDO;

public interface ShopMapDOMapper {
    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table shop_map
     *
     * @mbg.generated Fri Sep 27 16:34:02 CST 2019
     */
    int deleteByPrimaryKey(Integer id);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table shop_map
     *
     * @mbg.generated Fri Sep 27 16:34:02 CST 2019
     */
    int insert(ShopMapDO record);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table shop_map
     *
     * @mbg.generated Fri Sep 27 16:34:02 CST 2019
     */
    int insertSelective(ShopMapDO record);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table shop_map
     *
     * @mbg.generated Fri Sep 27 16:34:02 CST 2019
     */
    ShopMapDO selectByPrimaryKey(Integer id);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table shop_map
     *
     * @mbg.generated Fri Sep 27 16:34:02 CST 2019
     */
    int updateByPrimaryKeySelective(ShopMapDO record);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table shop_map
     *
     * @mbg.generated Fri Sep 27 16:34:02 CST 2019
     */
    int updateByPrimaryKey(ShopMapDO record);
}

dataobject層文件如下: 

package com.dataintel.xa.dataobject;

public class ShopMapDO {
    /**
     *
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column shop_map.id
     *
     * @mbg.generated Fri Sep 27 16:34:02 CST 2019
     */
    private Integer id;

    /**
     *
     * This field was generated by MyBatis Generator.
     * This field corresponds to the database column shop_map.name
     *
     * @mbg.generated Fri Sep 27 16:34:02 CST 2019
     */
    private String name;

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column shop_map.id
     *
     * @return the value of shop_map.id
     *
     * @mbg.generated Fri Sep 27 16:34:02 CST 2019
     */
    public Integer getId() {
        return id;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column shop_map.id
     *
     * @param id the value for shop_map.id
     *
     * @mbg.generated Fri Sep 27 16:34:02 CST 2019
     */
    public void setId(Integer id) {
        this.id = id;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method returns the value of the database column shop_map.name
     *
     * @return the value of shop_map.name
     *
     * @mbg.generated Fri Sep 27 16:34:02 CST 2019
     */
    public String getName() {
        return name;
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method sets the value of the database column shop_map.name
     *
     * @param name the value for shop_map.name
     *
     * @mbg.generated Fri Sep 27 16:34:02 CST 2019
     */
    public void setName(String name) {
        this.name = name == null ? null : name.trim();
    }
}

Mapper層文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.dataintel.xa.dao.ShopMapDOMapper">
  <resultMap id="BaseResultMap" type="com.dataintel.xa.dataobject.ShopMapDO">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri Sep 27 16:34:02 CST 2019.
    -->
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="name" jdbcType="VARCHAR" property="name" />
  </resultMap>
  <sql id="Base_Column_List">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri Sep 27 16:34:02 CST 2019.
    -->
    id, name
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri Sep 27 16:34:02 CST 2019.
    -->
    select 
    <include refid="Base_Column_List" />
    from shop_map
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri Sep 27 16:34:02 CST 2019.
    -->
    delete from shop_map
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.dataintel.xa.dataobject.ShopMapDO">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri Sep 27 16:34:02 CST 2019.
    -->
    insert into shop_map (id, name)
    values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR})
  </insert>
  <insert id="insertSelective" parameterType="com.dataintel.xa.dataobject.ShopMapDO">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri Sep 27 16:34:02 CST 2019.
    -->
    insert into shop_map
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="name != null">
        name,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=INTEGER},
      </if>
      <if test="name != null">
        #{name,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.dataintel.xa.dataobject.ShopMapDO">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri Sep 27 16:34:02 CST 2019.
    -->
    update shop_map
    <set>
      <if test="name != null">
        name = #{name,jdbcType=VARCHAR},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.dataintel.xa.dataobject.ShopMapDO">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri Sep 27 16:34:02 CST 2019.
    -->
    update shop_map
    set name = #{name,jdbcType=VARCHAR}
    where id = #{id,jdbcType=INTEGER}
  </update>
</mapper>

 

擴展

若在數據庫中存在聯合主鍵默認會生成兩個實體類,可以通過下面的參數進行調節

defaultModelType="flat"

至此,我們就成功完成了Mybatis Generator插件的配置和使用!

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