初学SpringBoot,在集成Mybatis的过程中遇到了点问题:
出现问题之前的操作:使用Mybatis的generator自动生成dao层和entity层中的类和接口
在UserController中注入userMapper:
代码如下:
@Autowired
private UserMapper userMapper;
运行SpringBoot应用,报错!!!
问题如下:
Field userMapper in com.coder520.mamabike.user.controller.UserController required a
bean of type 'com.coder520.mamabike.user.dao.UserMapper' that could not be found.
问题说注入失败,找不到UserMapper。
解决问题步骤:
步骤一:.查看pom.xml文件中是否加了如下代码:
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
无效;
步骤二:很多人说在SpringBoot主程序中添加
@MapperScan
(
"路径"
),或许这样可以解决一些人的问题,但我的还是没有解决。
步骤三:在UserMapper.java 中添加注解:@Mapper;
问题解决;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface UserMapper {
int deleteByPrimaryKey(Long id);
int insert(User record);
int insertSelective(User record);
User selectByPrimaryKey(Long id);
int updateByPrimaryKeySelective(User record);
int updateByPrimaryKey(User record);
}
有人说是SpringBoot版本过高的原因,需要添加@Mapper,不知道所以然,但问题解决了。
运行成功图: