[英]springboot+mybatis multi-module project maven
下圖是我的項目結構。 項目結構
父項目是demo-mybatis 子模塊是mybatis-dao,demo-service。 演示-mybatis pom.xml 下面:
4.0.0 pom mybatis-dao demo-service demo-mybatis-app org.springframework.boot spring-boot-starter-parent 2.3.2.RELEASE com.example demo-mybatis 0.0.1-SNAPSHOT demo-mybatis Demo project for Z38008DD86E02F4AF1D7985ECF引導
<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 接口:```
@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);
}
}
@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; } }
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
application.properties :
@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)}
問題是:
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)}
我瘋了
@Mapper 公共接口 UserMapper
描述:
bean 'userMapper' 不能作為 'com.example.mapper.UserMapper' 注入,因為它是一個 JDK 動態代理,它實現:
行動:
考慮將 bean 作為其接口之一注入或通過在 @EnableAsync 和/或 @EnableCaching 上設置 proxyTargetClass=true 來強制使用基於 CGLib 的代理。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.