@TableId(value = "wx_user_id", type = IdType.ASSIGN_ID)无法自动生成主键

1,034 阅读3分钟
本文已参与「新人创作礼」活动,一起开启掘金创作之路。
@TableId(value = "wx_user_id", type = IdType.ASSIGN_ID)无法自动生成主键

关于mybatis-plus

MyBatis-Plus (opens new window)(简称 MP)是一个 MyBatis (opens new window)的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。

有如下特性:

  • 无侵入
  • 损耗小
  • 强大的CURD操作
  • 支持Lambda形式调用
  • 支持主键自动生成
  • 支持ActiveRecord模式
  • 支持自定义全局通用操作
  • 内置代码生成器
  • 内置分页插件
  • 分页插件支持多数据库
  • 内置性能分析插件
  • 内置全局拦截插件

使用@TableId注解生成主键

  • 描述:主键注解
  • 使用位置:实体类主键字段
@TableName("sys_user")
public class User {
    @TableId
    private Long id;
    private String name;
    private Integer age;
    private String email;
}

属性类型必须指定默认值描述
valueString""主键字段名称
typeEnumIdType.NONE指定主键类型
  • IdType
描述
AUTO数据库 ID 自增
NONE无状态,该类型为未设置主键类型(注解里等于跟随全局,全局里约等于 INPUT)
INPUTinsert 前自行 set 主键值
ASSIGN_ID分配 ID(主键类型为 Number(Long 和 Integer)或 String)(since 3.3.0),使用接口IdentifierGenerator的方法nextId(默认实现类为DefaultIdentifierGenerator雪花算法)
ASSIGN_UUID分配 UUID,主键类型为 String(since 3.3.0),使用接口IdentifierGenerator的方法nextUUID(默认 default 方法)

解决方案

  • 当出现这个问题时,感觉很轻松就可以解决,只需要在主键上添加注解就可以

  • 但是添加以后仍然不能生成(我采用的是通过mapper.xml文件自定义的SQL,因为数据库的字段和实体的字段采用的命名方式不同,实体采用的驼峰命名规则)

  • 开始查找原因

  • 通过查资料,一般的配置方式主键的名字都为 id ,但是一般我们主键命令不直接使用 id ,为了写SQL时更好的区分,我猜可能是必须用 id 命名,修改后仍不行,所以不是这个原因

  • 通过反反复复的查阅资料,仍然没有得到解决,直到看到一篇文章, 使用了@TableId注解,就必须用MybatisPlus中的BaseMapper自带的Insert方法,也就是一张表针对一个对象做的增删改查。手写的SQL一律不生效! 我突然认为这个解决方案是最终解,但我认为这是错误的解答,Mybatis可以实现这个功能,MybatisPlus应该也是可以

  • 下面通过使用 @TableId(value = "wx_user_id", type = IdType.ASSIGN_ID) 进行解决案例说明

  • 数据库字段类型采用 ”bigint“,实体属性类型采用”Long“,注意这里是”Long“类型

  • 数据库主键把”自动递增“、”无符号“、”填充零“取消勾选

  • mapper.xml代码

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.dao.IWxUserDao">

    <resultMap type="com.example.entity.WxUser" id="WxUserResult">
        <result property="wxUserId" column="wx_user_id" />
        <result property="sessionKey" column="session_key" />
        <result property="openId" column="openid" />
        <result property="unionId" column="unionid" />
        <result property="nickName" column="nick_name" />
        <result property="gender" column="gender" />
        <result property="language" column="language" />
        <result property="city" column="city" />
        <result property="province" column="province" />
        <result property="country" column="country" />
        <result property="avatarUrl" column="avatar_url" />
        <result property="signature" column="signature" />
        <result property="createTime" column="create_time" />
        <result property="updateTime" column="update_time" />

    </resultMap>

    <sql id="selectWxUserSql">
        select * from wx_user
    </sql>

    <select id="findList" resultMap="WxUserResult">
        <include refid="selectWxUserSql" />

    </select>

    <insert id="insertWxUser" parameterType="com.example.entity.WxUser">
        insert into wx_user(
        wx_user_id,
        <if test="sessionKey != null and sessionKey != ''">session_key,</if>
        <if test="openId != null and openId != ''">openid,</if>
        <if test="unionId != null and unionId != ''">unionid,</if>
        <if test="nickName != null and nickName != ''">nick_name,</if>
        <if test="gender != null">gender,</if>
        <if test="language != null and language != ''">language,</if>
        <if test="city != null and city != ''">city,</if>
        <if test="province != null and province != ''">province,</if>
        <if test="country != null and country != ''">country,</if>
        <if test="avatarUrl != null and avatarUrl != ''">avatar_url,</if>
        <if test="signature != null and signature != ''">signature,</if>
        create_time,
        update_time
        )values(
        #{wxUserId},
        <if test="sessionKey != null and sessionKey != ''">#{sessionKey},</if>
        <if test="openId != null and openId != ''">#{openId},</if>
        <if test="unionId != null and unionId != ''">#{unionId},</if>
        <if test="nickName != null and nickName != ''">#{nickName},</if>
        <if test="gender != null">#{gender},</if>
        <if test="language != null and language != ''">#{language},</if>
        <if test="city != null and city != ''">#{city},</if>
        <if test="province != null and province != ''">#{province},</if>
        <if test="country != null and country != ''">#{country},</if>
        <if test="avatarUrl != null and avatarUrl != ''">#{avatarUrl},</if>
        <if test="signature != null and signature != ''">#{signature},</if>
        sysdate(),
        sysdate()
        )
    </insert>

</mapper>

  • 实体配置
@Data
@TableName("wx_user")
public class WxUser {
    @TableId(value = "wx_user_id", type = IdType.ASSIGN_ID)
    private Long wxUserId;
    private String sessionKey;
    private String openId;
    private String unionId;
    private String nickName;
    private Integer gender;
    private String language;
    private String city;
    private String province;
    private String country;
    private String avatarUrl;
    private String signature;
    private Timestamp createTime;
    private Timestamp updateTime;
}

  • 按照上述方式就可以解决