轉自:OnyWang 的《Spring和MyBatis整合自動生成代碼裏面text類型坑》
原址:https://www.jianshu.com/p/8e035078b8e5
Spring和MyBatis整合以後,使用自動生成代碼工具生成dao和mapper配置文件,生成步驟如下(以Intelli idea爲例)。
- 編寫生成代碼配置文件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"/>
<!-- 是否去除自動生成的註釋 true:是 : false:否 -->
<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>
<!-- 生成模型的包名和位置 -->
<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>
<!--需要自動生成的表名和對應的model名-->
<table tableName="sys_user" domainObjectName="SysUser"></table>
</context>
</generatorConfiguration>
-
配置如下maven運行命令。
maven運行命令.png
- 運行generatorcode即可。
問題描述
假如數據庫表裏面存在text或者blob字段。自動生成的數據庫配置文件如下,會多出幾個以withBlobs結尾的方法和resultMap:
<!--僅僅貼上不一樣的部分-->
<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>
假如此時查詢數據或者更新數據的使用仍然使用selectByExample或者updateByExample,得到的text或者blob數據是null。
正確做法
應該使用selectByExampleWithBLOBs或者updateByExampleWithBLOBs這兩個方法。
大爺你好,看您心情。