使用IDEA 整合SpringBoot Mybatis时 出现mapper注入失效 这个怎么解决?

bug仁 2019-05-23 02:55:19
运行时出现的异常:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userController': Unsatisfied dependency expressed through field 'userMapper'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userMapper' defined in file [E:\IDEAfile\extjsdemo\target\classes\com\extjs\mapper\UserMapper.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:596) ~[spring-beans-5.1.7.RELEASE.jar:5.1.7.RELEASE]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90) ~[spring-beans-5.1.7.RELEASE.jar:5.1.7.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:374) ~[spring-beans-5.1.7.RELEASE.jar:5.1.7.RELEASE]


我的工作目录:



启动类:

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;


@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@MapperScan("com.extjs.mapper")
public class ExtjsdemoApplication {

public static void main(String[] args) {

SpringApplication.run(ExtjsdemoApplication.class, args);
}

}


application.yml配置文件


我的依赖就这几个



User实体类:

public class User {
private Integer id;
private String username;
private String password;
private Integer roleid;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public Integer getRoleid() {
return roleid;
}

public void setRoleid(Integer roleid) {
this.roleid = roleid;
}
}

mapper接口:

package com.extjs.mapper;

import com.extjs.entity.User;
import org.apache.ibatis.annotations.Select;

public interface UserMapper {
@Select("select * from where id=#{id}")
User selectByid(Integer id);
}

我也试过在接口里面加@mapper注解,但是也没用,依然注入失败。




package com.extjs.controller;

import com.extjs.entity.User;
import com.extjs.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RestController
public class UserController {
@Autowired
private UserMapper userMapper;

@RequestMapping(value = "index")
public String index(ModelAndView model){
User user =userMapper.selectByid(1);
model.addObject("name",user.getUsername());
return "index";
}
}


我是刚开始接触springboot的,这个问题困扰了3天了,我想问下各位大佬是怎么解决的?
下面是异常翻译过了的:
beans.factory。创建名为“userController”的bean时出错:通过字段“userMapper”表示的未满足的依赖项;嵌套异常是org.springframe .bean .factory。BeanCreationException:在文件中定义名称为“userMapper”的bean创建时出错


...全文
1118 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
IT老炮-大钊 2022-02-24
  • 打赏
  • 举报
回复

解决了吗?同意问题

专注写bug 2019-05-23
  • 打赏
  • 举报
回复
干掉你本地仓库的那个下载失败了的库的文件夹,重新下载
bug仁 2019-05-23
  • 打赏
  • 举报
回复
使用Eclipse也爆这个错误

50,528

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧