关于SpingBoot整合MP中Mapper相关的错误
错误:Error creating bean with name ‘userServiceImpl’: Unsatisfied dependency expressed through field ‘baseMapper’;
1.出现的问题:创建userServiceImpl的bean失败
2.解决过程
- 解决过程:
首先看UserMapper
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.qiren.gt.entity.User;
import org.mapstruct.Mapper;
@Mapper
public interface UserMapper extends BaseMapper<User> {
}
起初以为是Mapper注解的包导错了引起的,进行修改
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.qiren.gt.entity.User;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface UserMapper extends BaseMapper<User> {
}
但是还是出现上面的问题,很费解,所以尝试不使用Mapper注解,使用@Repository注解,但是使用@Repository这个注解就需要在启动类上配置下@MapperScan,因为@Repository只是个标识,表示这个是bean对象,如果想要这个接口被扫描到需要使用@MapperScan
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.qiren.gt.entity.User;
import org.springframework.stereotype.Repository;
@Repository
public interface UserMapper extends BaseMapper<User> { }
@SpringBootApplication(scanBasePackages = {"com.qiren.gt"})
@MapperScan(basePackages = {"com.qiren.gt.mapper"})
public class ServiceAclApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceAclApplication.class, args);
}
}
这回错误解决了,但是还是有一点不清楚,@Mapper的作用相当于@Repository+@MapperScan的作用,但是有的时候会出现bug(这个目前还不清楚为什么),这是个解决方案,可以先用着