问题原因:编译的时候没有把下面的dtd文件打进去
解决方法
maven依赖增加下面的代码即可:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 <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 路径下面的
InsertSelectiveElementGenerator 和UpdateByPrimaryKeySelectiveElementGenerator 文件。
修改InsertSelectiveElementGenerator
一共有两处要修改的地方(大概119和130行)。
修改UpdateByPrimaryKeySelectiveElementGenerator
注意日期类型与空字符串比较会报错:
1 2 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文件判空优化-最佳解决方案
数据库表结构如下:
在InsertSelectiveElementGenerator和UpdateByPrimaryKeySelectiveElementGenerator 中增加下面的方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 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文件效果如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 <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