[英]Consider defining a bean of type in your configuration: MyBatis
我是整个 spring 生态系统的新手。 我一直在学习一些教程,并且能够创建一个 spring 引导应用程序并执行 crud 操作。 然后我开始将该项目更改为 mybatis 的标准。
我已经尝试了很多其他类似问题的答案,但到目前为止都没有奏效。
这是问题陈述:
//CustomerService.java
package com.crudapp.service;
import com.crudapp.entity.Customer;
import java.util.List;
@Service
public interface CustomerService {
public List<Customer> getAllCustomers();
//I have other methods as well to delete, update and post.
}
实现 class 实现为:
public class CustomerServiceImpl implements CustomerService {
@Autowired
private CustomerMapper customerMapper;
@Override
public List<Customer> getAllCustomers() {
List<Customer> list = customerMapper.getAllCustomers();
return list;
}
}
我的Mapper class是这样的:
@Mapper
public interface CustomerMapper {
public List<Customer> getAllCustomers();
}
我的 Mapper.xml class 是:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.crudapp.mapper.CustomerMapper">
<select id="getAllCustomers" resultType="customer">
select * from customers
</select>
</mapper>
最后是我的 Controller class:
@RestController
@RequestMapping("/api/v1")
public class CustomerController {
@Autowired
CustomerService customerService;
@RequestMapping(value = "/getAllCustomers", method = RequestMethod.GET)
public List<Customer> getAllCustomers() {
List<Customer> list = customerService.getAllCustomers();
return list;
}
}
我得到的错误是:
描述:
com.crudapp.service.impl.CustomerServiceImpl 中的字段 customerMapper 需要找不到类型为“com.crudapp.mapper.CustomerMapper”的 bean。
注入点有以下注解:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
行动:
考虑在您的配置中定义类型为“com.crudapp.mapper.CustomerMapper”的 bean。
毫无疑问,错误很简单。 我认为我在 controller 中有两个 @Autowired,另一个在服务实现中可能是问题所在。 但我的理解是该 bean 将由框架自动管理。 那么,我将如何解决这个问题?
更新:通过添加@MapperScan("com.crudapp.mapper")
上面的错误不再存在,但我收到了这个新错误:
org.springframework.beans.factory.BeanCreationException:在 com.crudapp.repository.CustomerRepo 中定义的名为“customerRepo”的 bean 在 JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration 上声明的@EnableJpaRepositories 中定义时出错:Invocation of init 方法失败; 嵌套异常是 java.lang.IllegalArgumentException:不是托管类型:class com.crudapp.entity.Customer
我的主 class 是:
@SpringBootApplication
@MapperScan("com.crudapp.mapper")
public class CrudappApplication {
public static void main(String[] args) {
SpringApplication.run(CrudappApplication.class, args);
}
}
在我的 application.properties 文件中,我有:
mybatis.config=classpath:mapper/mybatis_config.xml
mybatis.typeAliasesPackage=com.crudapp.entity
mybatis.mapperLocations=classpath*:**/mappers/*.xml
确保你的属性文件中有这个
mybatis.typeAliasesPackage=com.sivalabs.demo.domain mybatis.mapperLocations=classpath*:**/mappers/*.xml
参考: https://dzone.com/articles/spring-boot-working-with-mybatis
和 MapperScan 注解
@SpringBootApplication
@MapperScan("com.sivalabs.demo.mappers")
public class SpringbootMyBatisDemoApplication
{
public static void main(String[] args)
{
SpringApplication.run(SpringbootMyBatisDemoApplication.class, args);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.