Spring和MyBatis整合自動生成代碼里面text類型遇到的坑
Spring和MyBatis整合以后,使用自動生成代碼工具生成dao和mapper配置文件,生成步驟如下(以Intelli idea為例)。
1.編寫生成代碼配置文件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>
<!--需要自動生成的表名和對應(yīng)的model名-->
<table tableName="sys_user" domainObjectName="SysUser"></table>
</context>
</generatorConfiguration>
2.配置如下maven運(yùn)行命令。
3.運(yùn)行g(shù)eneratorcode即可。
問題描述
假如數(shù)據(jù)庫表里面存在text或者blob字段。自動生成的數(shù)據(jù)庫配置文件如下,會多出幾個以withBlobs結(jié)尾的方法和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>
假如此時查詢數(shù)據(jù)或者更新數(shù)據(jù)的使用仍然使用selectByExample或者updateByExample,得到的text或者blob數(shù)據(jù)是null。
正確做法
應(yīng)該使用selectByExampleWithBLOBs或者updateByExampleWithBLOBs這兩個方法。
總結(jié)
以上所述是小編給大家介紹的Spring和MyBatis整合自動生成代碼里面text類型遇到的坑,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
linux部署出現(xiàn)java文件操作報錯:java.io.FileNotFoundException解決辦法
這篇文章主要g介紹了linux部署出現(xiàn)java文件操作報錯:java.io.FileNotFoundException解決的相關(guān)資料,這個錯誤通常表示你的Spring Boot應(yīng)用程序無法找到指定的文本文件,需要的朋友可以參考下2023-12-12
java動態(tài)代理和cglib動態(tài)代理示例分享
這篇文章主要介紹了java動態(tài)代理和cglib動態(tài)代理示例,JDK1.3之后,Java提供了動態(tài)代理的技術(shù),允許開發(fā)者在運(yùn)行期間創(chuàng)建接口的代理實(shí)例,下面我們使用示例學(xué)習(xí)一下2014-03-03
Spring中的@Scheduled定時任務(wù)注解詳解
這篇文章主要介紹了Spring中的@Scheduled定時任務(wù)注解詳解,要使用@Scheduled注解,首先需要在啟動類添加@EnableScheduling,啟用Spring的計劃任務(wù)執(zhí)行功能,這樣可以在容器中的任何Spring管理的bean上檢測@Scheduled注解,執(zhí)行計劃任務(wù),需要的朋友可以參考下2023-09-09
SpringBoot開發(fā)實(shí)戰(zhàn)系列之動態(tài)定時任務(wù)
在我們?nèi)粘5拈_發(fā)中,很多時候,定時任務(wù)都不是寫死的,而是寫到數(shù)據(jù)庫中,從而實(shí)現(xiàn)定時任務(wù)的動態(tài)配置,下面這篇文章主要給大家介紹了關(guān)于SpringBoot開發(fā)實(shí)戰(zhàn)系列之動態(tài)定時任務(wù)的相關(guān)資料,需要的朋友可以參考下2021-08-08
java8時間 yyyyMMddHHmmss格式轉(zhuǎn)為日期的代碼
這篇文章主要介紹了java8時間 yyyyMMddHHmmss格式轉(zhuǎn)為日期的代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09
java學(xué)生管理系統(tǒng)界面簡單實(shí)現(xiàn)(全)
這篇文章主要為大家詳細(xì)介紹了java學(xué)生管理系統(tǒng)界面的簡單實(shí)現(xiàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-01-01
SpringBoot使用Interceptor攔截器的實(shí)例
這篇文章主要介紹了SpringBoot使用Interceptor攔截器的相關(guān)知識,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-03-03

