本文已参与「新人创作礼」活动,一起开启掘金创作之路。
springboot+mybatis-plus+pagehelper整合项目无法启动
错误信息及解决方案
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'albumController': Unsatisfied dependency expressed through field 'albumMapper'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'albumMapper' defined in file [D:\IdeaProjects\disk\disk-parent\disk-service\disk-service-goods\target\classes\com\disk\mapper\AlbumMapper.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'albumMapper' defined in file [D:\IdeaProjects\disk\disk-parent\disk-service\disk-service-goods\target\classes\com\disk\mapper\AlbumMapper.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required
Caused by: java.lang.IllegalArgumentException: Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required
通过上面的错误分析,一直认为是spring boot+mybatis-plus整合注入mapper的问题,但是这个错误是因为mybatis-plus和pagehelper这两个依赖包中有冲突项导致的,通过idea中的maven工具可以发现,下面标注表明存在mybatis和mybatis-spring冲突。
针对上述问题可以在pom.xml文件中排除依赖冲突,如下:
<!-- mybatis分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
<exclusions>
<exclusion>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
</exclusion>
<exclusion>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
</exclusion>
</exclusions>
</dependency>
spring boot+mybatis-plus+pagehelper整合案例
- 导入依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>
<!-- mybatis分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
<exclusions>
<exclusion>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
</exclusion>
<exclusion>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
- application.yml配置
server:
port: 9001
spring:
application:
name: goods
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/disk_goods?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
username: root
password: 123456
pagehelper:
helperDialect: mysql
reasonable: true
supportMethodsArguments: true
params: count=countSql
mybatis-plus:
mapper-locations: classpath:mapper/*Mapper.xml #相对于resource目录的路径
typeAliasesPackage: com.disk.goods.pojo
configuration:
map-underscore-to-camel-case: true #驼峰命名
- mapper文件
public interface AlbumMapper extends BaseMapper<Album> {
}
- controller文件
@RestController
@RequestMapping("album")
public class AlbumController {
@Autowired
private AlbumMapper albumMapper;
@GetMapping("/list")
public List<Album> list(){
return albumMapper.selectList(null);
}
}
- springboot项目启动入口Application(使用注解@MapperScan扫描mapper文件所在的包,这点特别重要,否则会失败)
@SpringBootApplication
@MapperScan("com.disk.mapper")
public class GoodsApplication {
public static void main(String[] args){
SpringApplication.run(GoodsApplication.class, args);
}
}
- 实体类
@Data
@TableName("tb_album")
public class Album implements Serializable{
private Long id;
private String title;
private String image;
private String imageItems;
}
- 创建数据库sql语句
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for tb_album
-- ----------------------------
DROP TABLE IF EXISTS `tb_album`;
CREATE TABLE `tb_album` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '编号',
`title` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '相册名称',
`image` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '相册封面',
`image_items` text CHARACTER SET utf8 COLLATE utf8_general_ci NULL COMMENT '图片列表',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of tb_album
-- ----------------------------
INSERT INTO `tb_album` VALUES (2, 's12', '//localhost/1.jpg', '[{\"url\":\"//192.168.200.128:8080/group1/M00/00/00/wKjIgF0IrUWAYdkyAAANt9KDpWU669.jpg\",\"uid\":1561719575032,\"status\":\"success\"}]');
- 直接运行项目