Search code examples
javapostgresqlmybatismybatis-generator

why did not generate enableSelectByPrimaryKey when using mybatis generator


When I am using this command to generate mybatis xml file:

java -jar mybatis-generator-core-1.3.4.jar -configfile generatorConfig.xml -overwrite

everything works fine but finnaly i found the user mapper result file did not genreate SelectByPrimaryKey function. this is part of my generate file config:

<table tableName="users"
               enableCountByExample="true"
               enableUpdateByExample="true"
               enableDeleteByExample="true"
               enableSelectByExample="true"
               enableSelectByPrimaryKey="true"
               enableUpdateByPrimaryKey="true"
               selectByPrimaryKeyQueryId="true"
               selectByExampleQueryId="true">
            <generatedKey column="ID" sqlStatement="JDBC" identity="true" />
        </table>

my database is PostgreSQL 13. what should I do to fix it? This is my user table DML:

CREATE TABLE public.users (
    id int8 NOT NULL GENERATED ALWAYS AS IDENTITY,
    nickname varchar NULL,
    avatar_url varchar NULL,
    phone varchar NOT NULL,
    updated_time int8 NOT NULL,
    created_time int8 NOT NULL,
    salt varchar NULL,
    pwd varchar NULL,
    sex int4 NULL,
    level_type varchar NULL,
    phone_region varchar NULL,
    country_code int4 NULL,
    user_status int4 NULL DEFAULT 1,
    last_login_time int8 NULL,
    first_login_time int8 NULL,
    app_id int4 NOT NULL,
    register_time int8 NULL,
    apple_iap_product_id varchar NULL,
    CONSTRAINT unique_phone UNIQUE (phone)
);

Solution

  • The DML you posted shows that the table doesn't have a primary key. You have two options.

    You could define a primary key in the table by adding this to the create table statement:

    create table public.users (
      ...
      primary key (id)
    );
    

    Or, if you don't want to define a primary key in the database, you can use the "VirtualPrimaryKeyPlugin" in MyBatis Generator. See here for details: https://mybatis.org/generator/reference/plugins.html