Mybatis Generator源码修改

news/2024/10/18 3:28:21/

文章目录

    • 报java.net.MalformedURLException错误解决
      • 问题原因:编译的时候没有把下面的dtd文件打进去
      • 解决方法
    • XML文件判空优化-增加空字符串
      • 修改InsertSelectiveElementGenerator
      • 修改UpdateByPrimaryKeySelectiveElementGenerator
    • XML文件判空优化-最佳解决方案

报java.net.MalformedURLException错误解决

问题原因:编译的时候没有把下面的dtd文件打进去

在这里插入图片描述

解决方法

maven依赖增加下面的代码即可:

<build><resources>  <resource>  <directory>src/main/java</directory>  <includes>  <include>**/*.properties</include>  <include>**/*.xml</include>  <include>**/*.tld</include>  <include>**/*.dtd</include>  </includes>  <filtering>false</filtering>  </resource>  </resources> 
</build>

MyBatis Generator系列(三)-修改源码实现中文注释
大象修改源码

XML文件判空优化-增加空字符串

需要修改 \org\mybatis\generator\codegen\mybatis3\xmlmapper\elements 路径下面的
InsertSelectiveElementGeneratorUpdateByPrimaryKeySelectiveElementGenerator文件。

修改InsertSelectiveElementGenerator

在这里插入图片描述
一共有两处要修改的地方(大概119和130行)。

修改UpdateByPrimaryKeySelectiveElementGenerator

在这里插入图片描述

注意日期类型与空字符串比较会报错:

<if test="updateTime != null and updateTime != ''">update_time = #{updateTime,jdbcType=TIMESTAMP},
</if>

Cause: java.lang.IllegalArgumentException: invalid comparison: java.util.Date and java.lang.String
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30) ~[mybatis-3.5.5.jar:3.5.5]
at org.apache.ibatis.session.defaults.DefaultSqlSession.update

MySQL的日期类型和MyBatis的对应关系:

MySQL日期类型MyBatis日期类型
DATETIMETIMESTAMP
TIMESTAMPTIMESTAMP
DATEDATE
YEARDATE
TIMETIME

XML文件判空优化-最佳解决方案

数据库表结构如下:
在这里插入图片描述

InsertSelectiveElementGenerator和UpdateByPrimaryKeySelectiveElementGenerator中增加下面的方法:

/*** 判空优化:1、字符类型 != null and  != '' 2、非字符类型 != null** @param: introspectedColumn* @param: sb* @return void* @author liquanhong* @date 2023/6/9/*/
private void appendNullJudgment(IntrospectedColumn introspectedColumn, StringBuilder sb) {sb.setLength(0);String jdbcTypeName = introspectedColumn.getJdbcTypeName();// 字符类型处理if(jdbcTypeName.equals("VARCHAR") || jdbcTypeName.equals("CHAR") || jdbcTypeName.equals("LONGVARCHAR")){sb.append(introspectedColumn.getJavaProperty());sb.append(" != null and ");sb.append(introspectedColumn.getJavaProperty());sb.append(" != ''");}else{// 非字符类型处理sb.append(introspectedColumn.getJavaProperty());sb.append(" != null");}
}

把InsertSelectiveElementGenerator、UpdateByPrimaryKeySelectiveElementGenerator中相关判空逻辑改为读取上面的判空方法即可
例如:
在这里插入图片描述

生成XML文件效果如下:

<update id="updateByPrimaryKeySelective" parameterType="com.aspirecn.external.reward.pojo.entity.TestWithBLOBs">update t_test<set><if test="testVarchar != null and testVarchar != ''">test_varchar = #{testVarchar,jdbcType=VARCHAR},</if><if test="testChar != null and testChar != ''">test_char = #{testChar,jdbcType=CHAR},</if><if test="testTinytext != null and testTinytext != ''">test_tinytext = #{testTinytext,jdbcType=VARCHAR},</if><if test="testJson != null and testJson != ''">test_json = #{testJson,jdbcType=CHAR},</if><if test="testText != null and testText != ''">test_text = #{testText,jdbcType=LONGVARCHAR},</if><if test="testMediumtext != null and testMediumtext != ''">test_mediumtext = #{testMediumtext,jdbcType=LONGVARCHAR},</if><if test="testLongtext != null and testLongtext != ''">test_longtext = #{testLongtext,jdbcType=LONGVARCHAR},</if></set>where id = #{id,jdbcType=INTEGER}
</update>

MySQL的字符类型和MyBatis的对应关系:

MySQL字符类型MyBatis字符类型
VARCHARVARCHAR
CHARCHAR
TINYTEXTVARCHAR
JSONCHAR
TEXTLONGVARCHAR
MEDIUMTEXTLONGVARCHAR
LONGTEXTLONGVARCHAR

http://www.ppmy.cn/news/327763.html

相关文章

基于MSP430送药小车 ----- 拓展篇【2021年全国电赛(F题)】

文章目录 该篇文章并非完全按照赛题要求完成&#xff0c;仅当做个人休闲娱乐产品&#xff01;&#xff01;&#xff01;&#xff01; 一、赛题分析二、逻辑处理分析1. 近端病房2. 中端病房3. 远端病房 三、程序设计1. 近端送药2. 中端送药3. 远端送药4. 通信处理①蓝牙②OpenMV…

企业邮箱哪个好,邮箱品牌介绍—TOM邮箱

TOM邮箱TOM集团旗下的互联网产品&#xff0c; 1998年推出免费邮箱业务&#xff0c;是目前中国最大的电子邮箱服务提供商之一。自成立以来&#xff0c;TOM邮箱一直致力于为用户提供更安全、更稳定的邮箱服务系统。同时全面拓展邮箱业务&#xff0c;2001年开通VIP收费邮箱服务&am…

C语言 数组,指针,结构体,文件的训练内容

《计算思维&#xff08;二&#xff09;》实验报告 基础实践二 班级&#xff1a; 学号&#xff1a; 姓名&#xff1a; 实验时间&#xff1a; 2023 年 第01~12周 成绩 哈尔滨工程大学计算机基础课程教学中心 实验五&#xff1a;数组 实验题目1&a…

使用Java调用Yolo模型的准备工作与输入输出

当今社会&#xff0c;人工智能技术正日益成为各行各业的关键工具。其中&#xff0c;目标检测技术是计算机视觉领域中的一项重要任务。Yolo&#xff08;You Only Look Once&#xff09;是一种流行的目标检测算法&#xff0c;具有高效、准确的特点。本文将介绍如何使用Java语言调…

苹果4s怎么越狱教程_教你苹果手机(iPhone)上怎么装KODI (不用越狱)

教你怎么在苹果手机&#xff08;iPhone&#xff09;上装KODI &#xff08;不用越狱&#xff09; 不管你是喜欢在网上追剧的还是喜欢看4k电影的&#xff0c;当提到KODI&#xff08;XBMC&#xff09;估计没有人不认识&#xff0c;KODI前身叫XBMC&#xff0c;因为强大的解码能力和…

Learning C++ No.30 【lambda表达式实战】

引言&#xff1a; 北京时间&#xff1a;2023/6/9/9:13&#xff0c;今天8:15起床&#xff0c;可能是最近课非常少&#xff0c;导致写博客没什么压力&#xff0c;什么时间都能写&#xff0c;导致7点起不来&#xff0c;哈哈哈&#xff0c;习惯睡懒觉了&#xff0c;但是问题不大&a…

【JMeter压力测试】通过jmeter压测surging

目录 前言 环境 下载配置源码 JMeter和JDK下载 JDKJmeter安装 Jmeter非GUI运行压测 结尾 前言 surging是异构微服务引擎&#xff0c;提供了模块化RPC请求通道&#xff0c;引擎在RPC服务治理基础之上还提供了各种协议&#xff0c;并且还提供了stage组件&#xff0c;以便针…

华为OD机试 JavaScript 实现【简单密码】【牛客练习题 HJ21】,附详细解题思路

一、题目描述 现在有一种密码变换算法。 九键手机键盘上的数字与字母的对应&#xff1a; 1--1&#xff0c; abc--2, def--3, ghi--4, jkl--5, mno--6, pqrs--7, tuv--8 wxyz--9, 0--0&#xff0c;把密码中出现的小写字母都变成九键键盘对应的数字&#xff0c;如&#xff1a;a …