Consider defining a bean of type ‘com.wei.service.UserService‘ in your configuration
一般都是注解出错,在
service
的实现类中加入
@Service(value = "userService")
就行;
如果还不行,就看看下面我写的三层代码吧!
特别注意:注解的位置
,是否添加了注解
还有,我在
serviceImpl
中加入了注解
Autowired
之后变量变红,但是运行没错,我觉得应该是编译软件的问题吧!个人感觉
mapper
package com.wei.mapper;
import com.wei.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import java.util.List;
// 表明这是一个mybatis的mapper类;
@Mapper
@Repository // 表示dao层, 也可以用 @Component万能,所有层都可以用
public interface UserMapper {
List<User> queryUserList();
User queryUserById(int id);
int addUser(User user);
int deleteUser(int id);
int updateUser(User user);
}
service
:
package com.wei.service;
import com.wei.pojo.User;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import java.util.List;
@SpringBootApplication
public interface UserService {
List<User> queryUserList();
User queryUserById(int id);
int addUser(User user);
int deleteUser(int id);
int updateUser(User user);
}
service
实现类:
package com.wei.service;
import com.wei.mapper.UserMapper;
import com.wei.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service(value = "userService")
public class UserServiceImpl implements UserService{
@Autowired
private UserMapper userMapper;
public void setUserMapper(UserMapper userMapper){
this.userMapper=userMapper;
}
@Override
public List<User> queryUserList() {
return userMapper.queryUserList();
}
@Override
public User queryUserById(int id) {
return userMapper.queryUserById(id);
}
@Override
public int addUser(User user) {
return userMapper.addUser(user);
}
@Override
public int deleteUser(int id) {
return userMapper.deleteUser(id);
}
@Override
public int updateUser(User user) {
return updateUser(user);
}
}
controller
:
package com.wei.controller;
import com.wei.pojo.User;
import com.wei.service.UserService;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class userController {
@Autowired
public UserService userService;
@RequestMapping("/queryUserList")
public List<User> queryUserList(){
List<User> userList = userService.queryUserList();
for (User user : userList) {
System.out.println(user);
}
return userList;
}
}
后面因为自己的强迫症,实在忍不了了,就在网上查了解决方法,具体看图,取消该选择就行:
版权声明:本文为qq_45626944原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。