MybatisGenerator生成的问题及解决方案

一、MybatisGenerator生成WithBLOBS.java文件解决方案

在generatorConfig中按如下格式添加配置


    		
    		
    		

二、selectByExampleWithRowbounds这个方法有的表会生成好多个

问题

  •  生成的过程中发现xml中selectByExampleWithRowbounds这个方法有的表会生成好多个
  •  一张表新增了一个字段,发现生成的时候没有生成这个字段。
    后来上网搜,收到了这篇文章:https://www.jianshu.com/p/dbeeac29ff27

解决方案:

  1. 在连接字符串后面添加nullCatalogMeansCurrent=true
    原来我jdbc连接串的数据库没起作用,MybatisGenerator取的是最近一个数据库的表(我数据库有很多database),所以没取到目标的数据库的表
    使用mysql驱动连接mysql时,指定数据库是用catalog,而不是schema。
    那nullCatalogMeansCurrent就好理解了,因为之前配置的是schema=“piwik”,那么catalog就是null了,null的话就means current,就取当前的catalog,这个catalog就是连接串jdbc:mysql://localhost:3306/piwik里的“piwik”。

  2. 在generatorConfig.xml中的table属性中添加catalog,如下:

但是问题是会建立新的包,所以还是第一种方法好用

三、保存时返回自增主键配置

在generatorConfig.xml中配置


        

或者在xml中加上useGeneratedKeys, 具体参考http://note.youdao.com/noteshare?id=9ba0bba72548f286e19442e5a83a8245&sub=568B154A0DF84C98916268C9CAC5D3C2

四、JavaTypeResolver不生效的问题

我希望将数据库中的tinyint转成java的Integer。
我的数据库字段:

 `status` tinyint(1) DEFAULT NULL comment '是否有效,默认1->有效,0->无效';

我在JavaTypeResolver定制的实现类中(参考https://www.cnblogs.com/grey-wolf/p/9090337.html)已经修改成如下:

this.typeMap.put(-6, new JavaTypeResolverDefaultImpl.JdbcTypeInformation("TINYINT", new FullyQualifiedJavaType(Integer.class.getName())));

但是生成的时候,最后还是生成Boolean

private Boolean status;

刚开始怀疑是JavaTypeResolver对应的字段是Boolean:

this.typeMap.put(-7, new JavaTypeResolverDefaultImpl.JdbcTypeInformation("BIT", new FullyQualifiedJavaType(Boolean.class.getName())));
```不生效,但是我在我自己的实现类中添加断点是可以进入到我的实现类。

后来将tinyint(1)改成tinyint(2)就可以变成Integer了。

后来再仔细看。tinyint(1),在mybatis对应的字段是:BIT,下面是生成代码后我的mapper.xml中的status字段对应的mysql 类型:
```

在JavaTypeResolver对应的字段是Boolean:

this.typeMap.put(-7, new JavaTypeResolverDefaultImpl.JdbcTypeInformation("BIT", new FullyQualifiedJavaType(Boolean.class.getName())));

所以解决这个问题有两种方案:

  1. 修改数据库的字段的类型,将tinyint(1)改成tinyint(2),这里数据库占用的空间是一样的
  2. 修改JavaTypeResolver,将BIT对应的java类型改成Integer:
this.typeMap.put(-7, new JavaTypeResolverDefaultImpl.JdbcTypeInformation("BIT", new FullyQualifiedJavaType(Integer.class.getName())));

我选择了1,因为status字段后续有可能有别的含义,0和1还不够表达

参考链接

  • mybatis-generator tinyint 转Integer:https://blog.csdn.net/Roy_70/article/details/82496446
  • Mybatis Generator最完整配置详解:https://www.cnblogs.com/Guhongying/p/10834926.html
  • Mybatis自动生成key值(selectKey和useGeneratedKeys):https://blog.csdn.net/weixin_38809962/article/details/80091554
  • 修改mysql类型对应的java类型(smallint对应Integer)https://www.cnblogs.com/grey-wolf/p/9090337.html

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