前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >MyBatis问题汇总

MyBatis问题汇总

作者头像
雨临Lewis
发布2022-03-08 16:35:39
6530
发布2022-03-08 16:35:39
举报
文章被收录于专栏:雨临Lewis的博客

使用domainObjectRenamingRule报错

在使用MyBatis逆向工程时报错如下:

代码语言:javascript
复制
org.mybatis.generator.exception.XMLParserException: XML Parser Error on line 43: 必须声明元素类型 "domainObjectRenamingRule"。
    at org.mybatis.generator.config.xml.ConfigurationParser.parseConfiguration(ConfigurationParser.java:121)
    at org.mybatis.generator.config.xml.ConfigurationParser.parseConfiguration(ConfigurationParser.java:82)
    at org.mybatis.generator.config.xml.ConfigurationParser.parseConfiguration(ConfigurationParser.java:74)
    at GeneratorSqlmap.generator(GeneratorSqlmap.java:22)
    at GeneratorSqlmap.main(GeneratorSqlmap.java:32)

domainObjectRenamingRule该功能项是在MBG 1.3.6中新增加的功能,用于定义实体的重命名规则,常见的用途是取消表前缀。类似于columnRenamingRule,前者是重命名生成的模型对象的名称,后者是重命名表字段的名称。

如果在低于该版本的MBG中使用该功能,会出现如下错误:

代码语言:javascript
复制
XML Parser Error on line 59: 必须声明元素类型 "domainObjectRenamingRule"。

使用domainObjectRenamingRule无效

配置好了domainObjectRenamingRule后,运行逆向工程却无效果,原因是searchString的值配置不对。

根据表名来生成的类名是按照驼峰命名法,生成的类名首字母是大写的。而searchString是区分大小写的,并且它的值是Java里的正则表达式。

举个例子,现在有个表叫tb_vq,我希望生成的类名是Vq,而不是TbVq,可以通过domainObjectRenamingRule来实现这个功能,配置如下:

代码语言:javascript
复制
<table tableName="tb_vq" schema="" enableCountByExample="false"
enableDeleteByExample="false" enableUpdateByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false">
    <domainObjectRenamingRule searchString="^Tb" replaceString="" />
</table>

需要注意的是,这里的searchString必须填^Tb,这是个Java正则表达式,这里不能写成^tb,必须首字母大写,否则匹配不上,另外为了避免匹配出错,这里使用了^Tb而不是Tb

插入null值时报错

当插入的数据包含null值时报错:

代码语言:javascript
复制
### Cause: org.apache.ibatis.type.TypeException: Error setting null for parameter #3 with JdbcType OTHER . Try setting a different JdbcType for this parameter or a different jdbcTypeForNull configuration property. Cause: java.sql.SQLException: 无效的列类型
	at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:26)
	...

这个报错是因为MyBatis无法识别null值的类型,有两种解决方案。

方法一

在MyBatis的配置文件中添加jdbcTypeForNull的配置,通过这个全局配置来指定null值的类型:

代码语言:javascript
复制
<configuration>
    <settings>
        <setting name="jdbcTypeForNull" value="NULL" />
    </settings>
</configuration>

如果是在SpringBoot中使用MyBatis-Plus,则在application.yml中配置如下:

代码语言:javascript
复制
mybatis-plus:
  configuration:
    jdbc-type-for-null: 'null' #注意:单引号

也可以直接定义一个Bean:

代码语言:javascript
复制
    @Bean
    public ConfigurationCustomizer configurationCustomizer(){
        return new MybatisPlusCustomizers();
    }

    class MybatisPlusCustomizers implements ConfigurationCustomizer {

        @Override
        public void customize(org.apache.ibatis.session.Configuration configuration) {
            configuration.setJdbcTypeForNull(JdbcType.NULL);
        }
    }

方法二

在sql中为允许插入null值的column单独指定对应的类型,如下:

代码语言:javascript
复制
<insert id="save"  
        parameterType="com.test.entity.Student">
        insert into student values(
            student_seq.nextval,
            #{name,jdbcType=VARCHAR}
        )
</insert>

参考链接

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-03-032,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 使用domainObjectRenamingRule报错
  • 使用domainObjectRenamingRule无效
  • 插入null值时报错
    • 方法一
      • 方法二
      • 参考链接
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档