springboot+mybatis多模块项目maven



下图是我的项目结构。项目结构

母项目是demo mybatis子模块是mybatis dao,demo服务。演示mybatis pom.xml如下:

4.0.0pom八哥岛演示服务演示mybatis应用程序org.springframework.boot弹簧引导启动器父级2.3.2.租赁com.example演示mybatis0.0.1快照演示mybatisSpring 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)}

我疯了

@映射器公共接口UserMapper

描述:

bean"userMapper"无法作为"com.example.mapper.userMapper"注入,因为它是实现的JDK动态代理

行动:

考虑将bean作为其接口之一注入,或者通过在@EnableAsync和/或@EnableCaching上设置proxyTargetClass=true来强制使用基于CGLib的代理。

最新更新