24小时在线平台

当前位置: 24小时在线平台 > 部署教程大全 > 文章页

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

时间:2025-07-03 07:02来源: 作者:admin 点击: 4 次
以下内容是CSDN社区关于使用IDEA 整合SpringBoot Mybatis时 出现mapper注入失效 这个怎么解决?相关内容,如果想了解更多关于Java社区其他内容,请访问CSDN社区。

运行时出现的异常:
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创建时出错


(责任编辑:)
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:
发布者资料
查看详细资料 发送留言 加为好友 用户等级: 注册时间:2025-07-03 23:07 最后登录:2025-07-03 23:07
栏目列表
推荐内容