最近在整合spring boot 和通用mapper时遇到错误,纠结了好长时间,先看错误
Description:Field baseMapper in com.sinosoft.common.mybatis.service.impl.BaseServiceImpl required a bean of type 'com.sinosoft.common.mybatis.mapper.MyBaseMapper' that could not be found.Action:Consider defining a bean of type 'com.sinosoft.common.mybatis.mapper.MyBaseMapper' in your configuration.Process finished with exit code 1
问题解决思路
1.先看下pom,下边是整合通用mapper所须的依赖
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
<!-- pagehelper分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
<!-- 通用mapper -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.0.2</version>
</dependency>
<!-- 阿里巴巴druid数据库连接池 非必须 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.18</version>
</dependency>
2.代码目录结构
3.需要检查的点
3.1basemapper 不能和业务类的mapper放在同一目录下
3.2service实现类应该抽象的避免被通用mapper扫描
public abstract class BaseServiceImpl<T,ID extends Serializable> implements BaseService<T,ID> {
@Autowired
public MyBaseMapper<T> baseMapper;
3.3业务类mapper应该加上@Mapper注解,或者在spring boot启动类上添加@MapperScan注解
@Mapper
public interface MusicMapper extends MyBaseMapper<MusicModel> {
}
@SpringBootApplication
@MapperScan(basePackages ={"com.sinosoft.musicproxy.mapper"})
public class MusicProxyApplication {
public static void main(String[] args) {
SpringApplication.run(MusicProxyApplication.class, args);
}
}
3.4业务类应该继承你自己的通用mapper接口。我自己的错误就是吧MyBaseMapper写成了BaseMapper(这个错误是真的低级)
//错误
@Mapper
public interface MusicMapper extends BaseMapper<MusicModel> {
}
//正确
@Mapper
public interface MusicMapper extends MyBaseMapper<MusicModel> {
}
3.5namespace对应接口名
<mapper namespace="com.sinosoft.musicproxy.mapper.MusicMapper" >
package com.sinosoft.musicproxy.mapper;
/**
* @author fxl
* @Title: ${file_name}
* @Package ${package_name}
* @Description: ${todo}
* @date 2018-06-149:55
*/
@Mapper
public interface MusicMapper extends BaseMapper<MusicModel> {
}
3.6.还有可能是你的某些mapper没有加@mapper注解,也会出现这种情况
这算我在遇到这个问题在网上找到解决方法吧,希望别有人跟我一样犯同样的错误,特别是低级错误。写的不好,见谅。