Spring and MyBatis integrate to automatically generate text type pit in the code

Keywords: Mybatis MySQL Java xml

After Spring and MyBatis are integrated, dao and mapper configuration files are generated by using the automatic code generation tool. The steps are as follows (take Intelli idea as an example).

  1. Write the generate code configuration file generatorConfig.xml.
<?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>
    <classPathEntry location="D:\dev\maven\repository\mysql\mysql-connector-java\5.1.39\mysql-connector-java-5.1.39.jar"/>
    <context id="DB2Tables" defaultModelType="flat" targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <!-- Remove automatically generated comments or not true: Yes: false:no -->
            <property name="suppressAllComments" value="false"/>
        </commentGenerator>
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/mycollege?characterEncoding=utf-8"
                        userId="root"
                        password="root">
        </jdbcConnection>

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


        <!-- Package name and location of the build model  -->
        <javaModelGenerator targetPackage="com.cx.elearnning.model"
                            targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!-- generate xml -->
        <sqlMapGenerator targetPackage="/"
                         targetProject="src/main/resources/mapper">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!-- generate Mapper -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.cx.elearnning.dao"
                             targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
      <!--Automatically generated table names and corresponding model name-->
        <table tableName="sys_user" domainObjectName="SysUser"></table>


    </context>
</generatorConfiguration>
  1. Configure maven to run the command as follows.


    maven runs command.png
  2. Just run generatorcode.

Problem description

If there are text or blob fields in the database table. The automatically generated database configuration files are as follows, with several more methods and resultmaps ending with blobs:

<!--Just paste different parts-->
<resultMap extends="BaseResultMap" id="ResultMapWithBLOBs" type="com.cx.elearnning.model.EduWebsiteProfile">
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
    -->
    <result column="DESCIPTION" jdbcType="LONGVARCHAR" property="desciption" />
  </resultMap>


<select id="selectByExampleWithBLOBs" parameterType="com.cx.elearnning.model.EduWebsiteProfileExample" resultMap="ResultMapWithBLOBs">
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
    -->
    select
    <if test="distinct">
      distinct
    </if>
    <include refid="Base_Column_List" />
    ,
    <include refid="Blob_Column_List" />
    from edu_website_profile
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null">
      order by ${orderByClause}
    </if>
  </select>

If selectByExample or updateByExample is still used to query or update data at this time, the resulting text or blob data is null.

Correct approach

You should use the two methods selectByExampleWithBLOBs or updateByExampleWithBLOBs.

Posted by matthew1 on Thu, 30 Apr 2020 05:27:55 -0700