[英]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.