当前位置: 首页 > 知识库问答 >
问题:

创建具有通过字段表示的名称不满足依赖项的bean时出错

章绪
2023-03-14

我尝试使用Mybatis XML配置实现一个简单的CRUD应用程序已经是第三天了,我对整个Spring生态系统还是新手。这个问题有点长,但我只分享了要点。

我有一个类employee.java:

import lombok.*;

@Getter @Setter @AllArgsConstructor @NoArgsConstructor @ToString
public class Employee {

    private long emp_Id;
    private String emp_name;
    private String emp_dept;

}

然后我写了一个映射器

@Mapper
public interface EmployeeMapper {

    public Employee getEmployee(@Param("emp_Id") Long emp_Id);
    public List<Employee> getAllEmployee();

}
...
<mapper namespace="Employee">
    <select id="getAllEmployee" resultType="com.tmb.mybatiscrudproj.domains.Employee">
        select * from employee
    </select>

    <select id="getEmployee" resultType="com.tmb.mybatiscrudproj.domains.Employee">
        select * from employee where emp_Id = #{emp_Id}
    </select>

</mapper>
public interface EmployeeService {

    public Employee getEmployee(Long emp_Id);
    public List<Employee> getAllEmployee();

}
@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    EmployeeMapper employeeMapper;

    @Override
    public List<Employee> getAllEmployee() {
        List<Employee> list = employeeMapper.getAllEmployee();
        return list;
    }

    @Override
    public Employee getEmployee(Long emp_Id) {
        return employeeMapper.getEmployee(emp_Id);
    }
}
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    EmployeeService empService;

    @RequestMapping(value="/getEmp/{empId}",method = RequestMethod.GET)
    public Employee getEmployee(@PathVariable Long empId){
        return empService.getEmployee(empId);
    }

    @GetMapping(value="/getAllEmp")
    public List<Employee> getAllEmployee(){
        return empService.getAllEmployee();
    }

server.port: 8081

spring.h2.console.enabled=true
spring.h2.console.path=/h2

spring.datasource.url=jdbc:h2:~/test
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver

mybatis.config-location=classpath:mapper/mybatisConfig.xml
mybatis.mapper-locations=classpath:mapper/*Mapper.xml
mybatis.type-aliases-package=com.tmb.mybatiscrudproj.domains
mybatis.configuration.map-underscore-to-camel-case=true
mybatis.configuration.default-fetch-size=100
mybatis.configuration.default-statement-timeout=30

在许多教程和解答之后,我创建了一个MyBatisConfig.xmlEmployeeMapper.xml文件也在`Resources文件夹中。

mybatisconfig.xml中,我所做的与在.properties文件中所做的几乎相同

...
<configuration>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="org.h2.Driver"/>
                <property name="url" value="jdbc:h2:~/test"/>
                <property name="username" value="sa"/>
                <property name="password" value=""/>
                <property name="path" value="/h2"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="EmployeeMapper.xml"/>
    </mappers>
</configuration>

以下是项目结构截图:

嵌套异常是org.springframework.beans.factory.unsatisfiedDependencyException:创建名为“Employee ServiceImpl”的bean时出错:通过字段“Employee Mapper”表示未满足的依赖关系;

嵌套异常是org.springframework.beans.factory.unsatisfieddependencyException:创建文件[my_path/mybatiscrudproject/target/classes/com/tmb/mybatiscrudproj/mapper/employeemapper.class]中定义的名为“employee mapper”的bean时出错:通过bean属性“sql sessionfactory”表示未满足的依赖关系;嵌套异常是org.springframework.beans.factory.beanCreationException:创建类路径资源[org/mybatis/spring/boot/autoconfiguration/mybatisautoconfiguration.class]中定义的名为“SQL SessionFactory”的bean时出错:通过工厂方法实例化bean失败;嵌套异常是org.springframework.beans.beanInstantiationException:未能实例化[org.apache.ibatis.session.sqlSessionFactory]:工厂方法'sql sessionFactory'引发异常;嵌套异常是java.lang.IllegalStateException:属性“配置”和“配置位置”不能同时指定

现在,根据我所做的搜索,我确保在主类中添加:

@MappedTypes({Employee.class})
@MapperScan("com.tmb.mybatiscrudproj.mapper")
@SpringBootApplication
public class MybatiscrudprojApplication {

    public static void main(String[] args) {
        SpringApplication.run(MybatiscrudprojApplication.class, args);
    }

}

但还是一样的错误。我如何才能成功地用我在本问题中分享的结构配置项目?

共有1个答案

扈昀
2023-03-14

您的“EmployeeMapper”没有声明为spring bean,因此当依赖项注入时,没有这种类型的bean。尝试在类上添加@组件。

 类似资料: