I am new to the whole spring ecosystem. I've been following some tutorials and was able to create a spring boot application and performed crud operations. Then I started changing that project to standards of mybatis.
I have tried many answers to other similar questions but none are working so far.
Here is the problem statement:
//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.
}
The implementation class is implemented as:
public class CustomerServiceImpl implements CustomerService {
@Autowired
private CustomerMapper customerMapper;
@Override
public List<Customer> getAllCustomers() {
List<Customer> list = customerMapper.getAllCustomers();
return list;
}
}
My Mapper class is like this:
@Mapper
public interface CustomerMapper {
public List<Customer> getAllCustomers();
}
My Mapper.xml class is:
<?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>
and finally my 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;
}
}
The error I'm getting is:
Description:
Field customerMapper in com.crudapp.service.impl.CustomerServiceImpl required a bean of type 'com.crudapp.mapper.CustomerMapper' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'com.crudapp.mapper.CustomerMapper' in your configuration.
The error is straightforward without a doubt. I think the two @Autowired I have one in the controller the other in service implmentation might be the problem. But my understanding is that the bean will be auto-managed by the framework. So, how would I fix this?
UPDATE: By adding @MapperScan("com.crudapp.mapper")
the above error is no longer there but I'm getting this new error:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customerRepo' defined in com.crudapp.repository.CustomerRepo defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class com.crudapp.entity.Customer
My main class is:
@SpringBootApplication
@MapperScan("com.crudapp.mapper")
public class CrudappApplication {
public static void main(String[] args) {
SpringApplication.run(CrudappApplication.class, args);
}
}
In my application.properties file I have:
mybatis.config=classpath:mapper/mybatis_config.xml
mybatis.typeAliasesPackage=com.crudapp.entity
mybatis.mapperLocations=classpath*:**/mappers/*.xml
Ensure you have this in your properties file
mybatis.typeAliasesPackage=com.sivalabs.demo.domain mybatis.mapperLocations=classpath*:**/mappers/*.xml
Ref: https://dzone.com/articles/spring-boot-working-with-mybatis
And MapperScan annotation
@SpringBootApplication
@MapperScan("com.sivalabs.demo.mappers")
public class SpringbootMyBatisDemoApplication
{
public static void main(String[] args)
{
SpringApplication.run(SpringbootMyBatisDemoApplication.class, args);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.