MyBatis Generator 无法生成 selectByPrimaryKey、updateByPrimaryKey、deleteByPrimaryKey等主键操作方法

  SpringBoot 项目采用 MyBatis Generator 插件为表 tbl_onl_test 生成 Mapper 文件时,发现生成的文件比以往少了 selectByPrimaryKey、updateByPrimaryKey、deleteByPrimaryKey 等几个方法,对应的 xml 文件中同样也少了这几个,并且 xml 文件中的 resultMap 节点里主键 id 使用的是 result 标签,而不是id标签,现象如下所示。

  tbl_onl_test 表结构:

CREATE TABLE `tbl_onl_test` (
  `test_id1` varchar(10) NOT NULL DEFAULT '',
  `test_id2` varchar(10) NOT NULL DEFAULT '',
  `test_nm` varchar(20) NOT NULL DEFAULT '',
  PRIMARY KEY (`test_id1`,`test_id2`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

  生成的 Mapper 接口文件:

package com.test.dao.testonl;

import com.test.common.entity.Test;

public interface TestMapper {
    
    
    int insert(Test record);

    int insertSelective(Test record);
}

  生成的 Mapper.xml 文件中的 resultMap:

  <resultMap id="BaseResultMap" type="com.test.common.entity.Test">
    <result column="test_id1" jdbcType="VARCHAR" property="testId1" />
    <result column="test_id2" jdbcType="VARCHAR" property="testId2" />
    <result column="test_nm" jdbcType="VARCHAR" property="testNm" />
  </resultMap>

  产生该现象的原因是:MySQL 数据库版本升级之后,插件没有识别出表格的主键信息。

  解决方法有2种:

  1. 降低 mysql-connector-java 版本,如修改为以下配置。
	<dependency>
		<groupId>mysql</groupId>
		<artifactId>mysql-connector-java</artifactId>
		<version>5.1.21</version>
	</dependency>
  1. 在 generator.xml 配置文件中,开启 jdbcConnection 节点里的 useInformationSchema 属性和 remarks 属性,配置如下。
<!--数据库连接地址、账号、密码-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
                connectionURL="jdbc:mysql://localhost:3306/testonldb"
                userId="root"
                password="testPwd">
    <!--设置可以获取tables remarks信息-->
    <property name="useInformationSchema" value="true"/>
    <!--设置可以获取remarks信息-->
    <property name="remarks" value="true"/>
</jdbcConnection>

  采用以上2种方法中的任意一种,新生成的文件中即会生成各个主键方法。

  新生成的 Mapper 接口文件:

package com.test.dao.testonl;

import com.test.common.entity.Test;
import com.test.common.entity.TestKey;

public interface TestMapper {
    
    
    int deleteByPrimaryKey(TestKey key);

    int insert(Test record);

    int insertSelective(Test record);

    Test selectByPrimaryKey(TestKey key);

    int updateByPrimaryKeySelective(Test record);

    int updateByPrimaryKey(Test record);
}

  新生成的 Mapper.xml 文件中的 resultMap:

<resultMap id="BaseResultMap" type="com.test.common.entity.Test">
    <id column="test_id1" jdbcType="VARCHAR" property="testId1" />
    <id column="test_id2" jdbcType="VARCHAR" property="testId2" />
    <result column="test_nm" jdbcType="VARCHAR" property="testNm" />
</resultMap>

  mysql 官网 https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-configuration-properties.html 对 useInformationSchema 属性给了一段解释:

useInformationSchema

When connected to MySQL-5.0.7 or newer, should the driver use the INFORMATION_SCHEMA to derive information used by DatabaseMetaData?

Default: false

Since version: 5.0.0

  当连接 5.0.7 以上的 mysql 版本时,要用这个 useInformationSchema 来获取 DatabaseMetaData 吗? 默认是 false,所以当项目中的 mysql 数据库迁移到高版本时,需要显式地配置为 true。

猜你喜欢

转载自blog.csdn.net/piaoranyuji/article/details/107712565