我使用mybaits-plus自动生成的list()方法进行查询数据库的测试时出现报错。
报错信息如下:
Error creating bean with name 'helloController': Unsatisfied dependency expressed through field 'userService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userServiceImpl': Unsatisfied dependency expressed through field 'baseMapper'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.mapper.UserMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
我的目录结构为:
其中controller层次的代码为
HelloController:
package com.controller;
import com.entity.User;
import com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
//@RestController注释表示一个用于处理HTTP请求的控制器
@RestController
public class HelloController {
@GetMapping
public String hello(){
return "测试成功!!!!";
}
@Autowired
private UserService userService;
@GetMapping("/list")
public List<User> list(){
return userService.list();
}
}
实体层
User:
package com.entity;
import com.baomidou.mybatisplus.annotation.TableField;
import lombok.Data;
@Data
public class User {
private Integer id;
private String no;
private String name;
private String password;
private Integer age;
private Integer sex;
private String phone;
private Integer roleId;
@TableField("isValid")
private String isvalid;
}
UserService:
package com.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.entity.User;
public interface UserService extends IService<User> {
}
UserServiceImpl:
package com.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.entity.User;
import com.mapper.UserMapper;
import com.service.UserService;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
}
UserMapper:
package com.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.entity.User;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface UserMapper extends BaseMapper<User> {
}
启动代码为:
package com;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ChangkuJavaApplication {
public static void main(String[] args) {
SpringApplication.run(ChangkuJavaApplication.class, args);
}
}
在百度上面搜说在启动类上添加@MapperScan注解于是我加了一个@MapperScan("com.mapper")还是报错。