【发布时间】:2020-12-02 21:03:14
【问题描述】:
下图是我的项目结构。 project structure
父项目是demo-mybatis子模块是mybatis-dao,demo-service。 demo-mybatis pom.xml 如下:
4.0.0 绒球 mybatis-dao 演示服务 演示-mybatis-app org.springframework.boot spring-boot-starter-parent 2.3.2.发布 com.example 演示-mybatis 0.0.1-快照 演示-mybatis Spring Boot 演示项目
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
mybatis-dao UserMapper.java 接口:
```
@Repository
public interface UserMapper {
List selectByName(String name);
int insert(User user);
void update(User user);
void delete(Long id);
User verify(User user);
List select();
}
demo-service UserServiceImpl.java class :
@Service
public class UserServiceImpl implements UserService {
@Resource
private UserMapper userMapper;
@Override
public List selectByName(String name) {
return null;
}
@Override
public int insert(User user) {
return 0;
}
}
run application class :
``` @SpringBootApplication
@ComponentScan(basePackages = {"com.example","com.wjs"})
@EnableCaching(proxyTargetClass = true)
@MapperScan(basePackages = {"com.wjs.model.dao"})
public class DemoMybatisAppApplication {
public static void main(String[] args) {
SpringApplication.run(DemoMybatisAppApplication.class, args);
}
}
application.properties:
server.port=8002
spring.datasource.url=jdbc:mysql://localhost:3306/demo?useSSL=false
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.application.name=demo-mybatis
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.wjs.model.entity
以下总是例外: 我找不到为什么? 我试试这个:
@Resource
private UserMapper userMapper;
问题是:
nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'userMapper' is expected to be of type 'com.wjs.model.dao.UserMapper' but was actually of type 'com.sun.proxy.$Proxy55'
和
@Autowired
private UserMapper userMapper;
问题是:
No qualifying bean of type 'com.wjs.model.dao.UserMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
我疯了
【问题讨论】:
-
这是一个由IntelliJ IEDA, springboot+mybatis dao service mult-module创建的maven项目。 ```
4.0.0 pom ```mybatis-dao demo-service demo-mybatis -app -
错误信息本身并不是很有用。请添加完整的堆栈跟踪。如果你能在 GitHub 上分享这些项目,那将是最好的。
-
org.springframework.beans.factory.BeanNotOfRequiredTypeException:名为 'userMapper' 的 Bean 应该是 'com.example.mapper.UserMapper' 类型,但实际上是 'com.sun.proxy' 类型。 $Proxy57' at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:399) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE] at org.springframework.beans .factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:207) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
-
github.com/shinebar/demo-mybatis.git项目git地址,谢谢。
-
你试过删除
EnableCaching吗?
标签: java spring-boot maven mybatis multi-module