目录
问题产生
问题分析
Yaml配置分析
检查数据源和MyBatis-Plus配置: 确保Spring Boot应用的application.properties
或application.yml
文件中正确配置了数据源和MyBatis-Plus相关配置。、
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/你的数据库名称?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC
username: 数据库用户名
password: 数据库密码
mybatis-plus:
configuration:
#这个配置会将执行的sql打印出来,在开发或测试的时候可以用
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
call-setters-on-nulls: true
global-config:
db-config:
logic-delete-value: 1
logic-not-delete-value: 0
id-type: auto
控制层代码分析
public Users SelectUserAll(Integer userId) {
Users users = userMapper.selectById(userId);
return users;
}
Dao(Mapper)分析
检查Mapper接口定义: 确保UsersMapper
接口正确地继承了BaseMapper<Users>
。这将自动为你的Users
实体类提供selectById
方法。
@Mapper
public interface UsersMapper extends BaseMapper<Users> { }
实体类分析
检查实体类和字段注解: 确保Users
实体类的id
字段上有@TableId
注解,这是selectById
方法识别主键所必需的。
package com.takem.entity;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
@TableName("users")
public class Users {
// 这是关键 只要认识到主要id才行
@TableId
@TableField(value = "user_pid")
private Integer userPid;
@TableField(value = "user_name")
private String userName;
@TableField(value = "user_password")
private String userPassword;
@TableField(value = "user_role")
private String userRole;
@TableField(value = "user_register_time")
private String userRegisterTime;
}
启动类分析
检查包扫描: 确认在Spring Boot应用的启动类上使用了@MapperScan
注解来指定UsersMapper
所在的包。
package com.takem;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
// 这里很是关键
@MapperScan("com.takem.mapper")
public class TakeMApplication {
public static void main(String[] args) {
SpringApplication.run(TakeMApplication.class, args);
}
}
Maven
清理并重新构建项目: 在IDE中进行项目清理(Clean Project)和重新构建(Rebuild),以确保所有更改都被正确编译和加载。
(到底啦~可关注 vx 公棕号 : wmcode 帮你解决更多问题)