Mybatis Generator源码修改

文章目录

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

报java.net.MalformedURLException错误解决

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

Mybatis Generator源码修改_第1张图片

解决方法

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

<build>
	<resources>  
		<resource>  
			<directory>src/main/javadirectory>  
			<includes>  
				<include>**/*.propertiesinclude>  
				<include>**/*.xmlinclude>  
				<include>**/*.tldinclude>  
				<include>**/*.dtdinclude>  
			includes>  
			<filtering>falsefiltering>  
		resource>  
	resources> 
build>

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

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

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

修改InsertSelectiveElementGenerator

Mybatis Generator源码修改_第2张图片
一共有两处要修改的地方(大概119和130行)。

修改UpdateByPrimaryKeySelectiveElementGenerator

Mybatis Generator源码修改_第3张图片

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

<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日期类型
DATETIME TIMESTAMP
TIMESTAMP TIMESTAMP
DATE DATE
YEAR DATE
TIME TIME

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

数据库表结构如下:
Mybatis Generator源码修改_第4张图片

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中相关判空逻辑改为读取上面的判空方法即可
例如:
Mybatis Generator源码修改_第5张图片

生成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字符类型
VARCHAR VARCHAR
CHAR CHAR
TINYTEXT VARCHAR
JSON CHAR
TEXT LONGVARCHAR
MEDIUMTEXT LONGVARCHAR
LONGTEXT LONGVARCHAR

你可能感兴趣的:(mybatis,java,mysql)