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插件的配置和使用!

发布了54 篇原创文章 · 获赞 19 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/DataIntel_XiAn/article/details/101543468