我发现连接mariadb时生成的映射界面中有一些缺少的方法,生成的映射器是:
public interface Mapper {
int insert(Record record);
int insertSelective(Record record);
}
应该是什么:
public interface Mapper {
int deleteByPrimaryKey(Record id);
int insert(Record record);
int insertSelective(Record record);
City selectByPrimaryKey(Record id);
int updateByPrimaryKeySelective(Record record);
int updateByPrimaryKey(Record record);
}
我尝试了几种不同的连接器lib,mysql-connector-java和mariadb-java-client。连接mysql时生成的代码是正确的,这让我想起mybatis generator 1.3.6是否不支持mariadb 5.7.20? 顺便说一句,mysql的版本是5.7。 这是我的config.xml:
<?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="/maven/repo/org/mariadb/jdbc/mariadb-java-client/2.2.1/mariadb-java-client-2.2.1.jar" />
<context id="mybatisgen" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressAllComments" value="true" />
<property name="suppressDate" value="true" />
</commentGenerator>
<jdbcConnection driverClass="org.mariadb.jdbc.Driver"
connectionURL="jdbc:mariadb://127.0.0.1:3306/db?characterEncoding=utf8"
userId="user"
password="password">
</jdbcConnection>
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<javaModelGenerator targetPackage="me.model" targetProject="src">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<sqlMapGenerator targetPackage="me.mapper" targetProject="src">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<javaClientGenerator type="XMLMAPPER" targetPackage="me.dao" targetProject="src">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<table tableName="test_table" domainObjectName="ATable"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
</table>
</context>
</generatorConfiguration>
答案 0 :(得分:1)
当MyBatis Generator无法从JDBC驱动程序(它调用DatabaseMetaData.getPrimaryKeys)获取主键信息时,会发生这种情况。
当JDBC驱动程序无法返回该信息时,您可以使用VirtualPrimaryKey插件(在http://www.mybatis.org/generator/reference/plugins.html中记录)手动指定表的主键。