问题:
nested exception is org.apache.ibatis.reflection.ReflectionException: Could not set property 'id' of 'class com.tulip.pojo.User' with value '1721566346333323266' Cause: java.lang.IllegalArgumentException: argument type mismatch
错误信息 "Could not set property 'id' of 'class com.tulip.pojo.User' with value '1721566346333323266'" 表明在尝试将值 '1721566346333323266' 设置到 User
类的 id
属性时出现了问题,具体原因是参数类型不匹配。
解决方法:
①controller层代码
package com.tulip.controller;
import com.tulip.pojo.User;
import com.tulip.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.constraints.Pattern;
@RestController
@RequestMapping("/user")
@Validated
public class UserController {
@Autowired
private UserService userService;
@PostMapping("/register")
public Result register(@Pattern(regexp = "^\\S{5,16}$") String username, @Pattern(regexp = "^\\S{5,16}$") String password){
User u1 = userService.findByName(username);
//用户不存在
if (u1 == null){
userService.register(username, password);
return Result.success();
}else {
//占用
return Result.error("用户名已被占用");
}
}
}
②pojo层(实体类层)
如果你的主键列使用了某种自动生成策略(例如自增主键),确保数据库表中的主键列已经正确配置为自动生成主键值。你可以使用 @TableId
注解来指定主键生成策略:
package com.tulip.pojo;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.time.LocalDateTime;
@Data
@TableName("user")
@NoArgsConstructor
@AllArgsConstructor
public class User {
@TableId(type = IdType.AUTO)
private Integer id;//主键ID
private String username;//用户名
private String password;//密码
private String nickname;//昵称
private String email;//邮箱
private String userPic;//用户头像地址
private LocalDateTime createTime;//创建时间
private LocalDateTime updateTime;//更新时间
}