Mybatis Generator源码修改

报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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值