一般出现这个情况的时候,怎么办?
第一步:不要慌,保持冷静的思考和清醒的头脑,这很关键!
第二步:打开浏览器,搜索一下:Cannot obtain primary key information from the database, generated objects may be incomplete
这个错误,
遇到这种情况的时候,代码生成器就只会生成insert和selectAll这两个方法,这个时候需要在jdbc配置的connectionURL上加上一个参数:nullCatalogMeansCurrent=true
。然后重新执行代码生成器,就可以看到其他的方法都出现了。
我的generatorConfig.xml配置如下(注意看connectionURL配置):
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<classPathEntry
location="D:\iflytek\maven\repository\mysql\mysql-connector-java\8.0.15\mysql-connector-java-8.0.15.jar"/>
<context id="MysqlTables" targetRuntime="MyBatis3Simple" defaultModelType="flat">
<property name="autoDelimitKeywords" value="true"/>
<property name="javaFileEncoding" value="utf-8"/>
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
<!--覆盖xml文件-->
<plugin type="com.xqnode.boot.util.OverwriteXmlPlugin"/>
<!-- 注释 -->
<commentGenerator type="com.xqnode.boot.util.CommentGenerator">
<property name="suppressAllComments" value="true"/><!-- 是否取消注释 -->
</commentGenerator>
<!-- jdbc连接 -->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/test?serverTimezone=Asia/Shanghai&nullCatalogMeansCurrent=true"
userId="root"
password="123456"/>
<!-- 类型转换 -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- 生成实体类地址 -->
<javaModelGenerator targetPackage="com.xqnode.boot.model" targetProject="src/main/java">
<property name="enableSubPackages" value="false"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- 生成mapxml文件 -->
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator>
<!-- 生成mapxml对应client,也就是接口dao -->
<javaClientGenerator targetPackage="com.xqnode.boot.dao" targetProject="src/main/java"
type="XMLMAPPER">
<property name="enableSubPackages" value="false"/>
</javaClientGenerator>
<table tableName="t_user" domainObjectName="User" mapperName="UserMapper" />
</context>
</generatorConfiguration>
这里面有我的两个代码插件:
1、注释插件CommentGenerator:
package com.xqnode.boot.util;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.internal.DefaultCommentGenerator;
/**
* 生成model中,字段增加注释
*/
public class CommentGenerator extends DefaultCommentGenerator {
@Override
public void addFieldComment(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
super.addFieldComment(field, introspectedTable, introspectedColumn);
if (!"".equals(introspectedColumn.getRemarks())) {
field.addJavaDocLine("/**");
field.addJavaDocLine(" * " + introspectedColumn.getRemarks());
field.addJavaDocLine(" */");
}
}
}
2、xml文件覆盖插件OverwriteXmlPlugin:
package com.xqnode.boot.util;
import org.mybatis.generator.api.GeneratedXmlFile;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import java.util.List;
/**
* @version 1.0.0
*/
public class OverwriteXmlPlugin extends PluginAdapter {
@Override
public boolean validate(List<String> warnings) {
return true;
}
@Override
public boolean sqlMapGenerated(GeneratedXmlFile sqlMap, IntrospectedTable introspectedTable) {
sqlMap.setMergeable(false);
return super.sqlMapGenerated(sqlMap, introspectedTable);
}
}
–end–