MyBatis获取数据库自增id为null的解决方案

问题复现:

实体类:

package com.sky.entity;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 套餐
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Setmeal implements Serializable {

    private static final long serialVersionUID = 1L;

    private Long id;

    //分类id
    private Long categoryId;

    //套餐名称
    private String name;

    //套餐价格
    private BigDecimal price;

    //状态 0:停用 1:启用
    private Integer status;

    //描述信息
    private String description;

    //图片
    private String image;

    private LocalDateTime createTime;

    private LocalDateTime updateTime;

    private Long createUser;

    private Long updateUser;
}

Service中的方法:

public void saveSetmeal(SetmealDTO dto) {
        setmealMapper.insert(dto);
        System.out.println(build.getId());
     
}

Mapper中的方法:

@AutoFill(OperationType.INSERT)
    void insert(Setmeal build);

xml中的sql:

<insert id="insert">
        insert into
        setmeal(`category_id`,`name`,`price`,`status`,`description`,`image`,`create_time`,`update_time`,`create_user`,`update_user`)
        <trim prefix="VALUES (" suffix=")" suffixOverrides=",">
            <if test="categoryId != null">#{categoryId},</if>
            <if test="name != null">#{name},</if>
            <if test="price != null">#{price},</if>
            <if test="status != null">#{status},</if>
            <if test="description != null">#{description},</if>
            <if test="image != null">#{image},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="createUser != null">#{createUser},</if>
            <if test="updateUser != null">#{updateUser},</if>
        </trim>
    </insert>j

解决方法:

方案一:在xml中的sql标签添加

<insert id="insert" useGeneratedKeys="true" keyProperty="id">
        insert into
        setmeal(`category_id`,`name`,`price`,`status`,`description`,`image`,`create_time`,`update_time`,`create_user`,`update_user`)
        <trim prefix="VALUES (" suffix=")" suffixOverrides=",">
            <if test="categoryId != null">#{categoryId},</if>
            <if test="name != null">#{name},</if>
            <if test="price != null">#{price},</if>
            <if test="status != null">#{status},</if>
            <if test="description != null">#{description},</if>
            <if test="image != null">#{image},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="createUser != null">#{createUser},</if>
            <if test="updateUser != null">#{updateUser},</if>
        </trim>
    </insert>

成功解决!

方案二:使用注解方式,同时insert语句也要使用注解形式,抛弃xml中的sql语句

@Insert("INSERT INTO setmeal (category_id, name, price, status, description, image, create_time, update_time, create_user, update_user) " +
            "VALUES (#{categoryId}, #{name}, #{price}, #{status}, #{description}, #{image}, #{createTime}, #{updateTime}, #{createUser}, #{updateUser})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void insert(Setmeal build);

成功解决!!!

但是千万注意!!!

不可以混用,

错误案例:sql语句在xml中,useGenerateKey在注解中;同理,注解中使用sql,xml使用useGenerateKey也不行!!!如下:

@Options(useGeneratedKeys = true, keyProperty = "id")
    void insert(Setmeal build);
<insert id="insert">
        insert into
        setmeal(`category_id`,`name`,`price`,`status`,`description`,`image`,`create_time`,`update_time`,`create_user`,`update_user`)
        <trim prefix="VALUES (" suffix=")" suffixOverrides=",">
            <if test="categoryId != null">#{categoryId},</if>
            <if test="name != null">#{name},</if>
            <if test="price != null">#{price},</if>
            <if test="status != null">#{status},</if>
            <if test="description != null">#{description},</if>
            <if test="image != null">#{image},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="createUser != null">#{createUser},</if>
            <if test="updateUser != null">#{updateUser},</if>
        </trim>
    </insert>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java Mybatis Plus是一种流行的ORM框架,它可以与许多关系型数据库进行交互并提供对主流数据库的支持。在使用Mybatis Plus进行数据库操作时,经常需要获取自增ID,下面将介绍获取自增ID的方法。 在进行数据插入操作时,Mybatis Plus提供了一种简单的获取自增ID的方式。如下所示: ```java User user = new User(); user.setName("Tom"); user.setAge(26); user.setEmail("tom@email.com"); userMapper.insert(user); System.out.println("自增ID: " + user.getId()); ``` 上述代码中,我们首先创建一个User实例,设置了name、age和email属性的值,然后插入到数据库中。最后通过获取实例的ID属性来获取自增ID。在使用Mybatis Plus进行数据插入时,insert()方法会返回插入成功的记录数,同时也会将自增ID设置到实例的ID属性中。 当然,如果需要在插入数据时获取自增ID的值,也可以使用Mybatis Plus提供的其他方法。最常用的方法是使用@Insert注解并在SQL语句中指定获取自增ID的方式。例如: ```java @Insert("insert into user(name, age, email) values(#{name}, #{age}, #{email})") @Options(useGeneratedKeys = true, keyProperty = "id") int insertUser(User user); ``` 上述代码中,我们使用@Insert注解指定SQL语句并使用@Options注解来指定获取自增ID的方式。其中useGeneratedKeys=true表示使用数据库支持的自增ID生成方式,keyProperty="id"表示设置自增ID的属性名为"id"。最后,返回的int值表示插入成功的记录数。 总之,Mybatis Plus提供了多种方式来获取自增ID,需要根据具体情况选择使用。在使用@Insert注解时,需要注意在SQL语句中指定获取自增ID的方式,以确保正确获取自增ID的值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值