目录

 

错误信息及解决方案

spring boot+mybatis-plus+pagehelper整合案例

关于@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})注解作用 


 

错误信息及解决方案

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整合案例

1.导入依赖 

<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>

2.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 #驼峰命名

3.mapper文件

public interface AlbumMapper extends BaseMapper<Album> {

}

4.controller文件

@RestController
@RequestMapping("album")
public class AlbumController {

    @Autowired
    private AlbumMapper albumMapper;

    @GetMapping("/list")
    public List<Album> list(){
        return albumMapper.selectList(null);
    }

}

5.springboot项目启动入口Application(使用注解@MapperScan扫描mapper文件所在的包,这点特别重要,否则会失败)

@SpringBootApplication
@MapperScan("com.disk.mapper")
public class GoodsApplication {

    public static void main(String[] args){
        SpringApplication.run(GoodsApplication.class, args);
    }

}

6.实体类

@Data
@TableName("tb_album")
public class Album implements Serializable{

	private Long id;

	private String title;

	private String image;

	private String imageItems;

}

7.创建数据库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', 'http://192.168.200.128:8080/group1/M00/00/00/wKjIgF0IrtmAf_tXAAALP8HQLWA987.jpg', '[{\"url\":\"http://192.168.200.128:8080/group1/M00/00/00/wKjIgF0IrUWAYdkyAAANt9KDpWU669.jpg\",\"uid\":1561719575032,\"status\":\"success\"},{\"url\":\"http://192.168.200.128:8080/group1/M00/00/00/wKjIgF0IrUWAZ0UTAAAZLmoT79w845.jpg\",\"uid\":1561719575039,\"status\":\"success\"},{\"url\":\"http://192.168.200.128:8080/group1/M00/00/00/wKjIgF0IrUWAXnipAAAQ9pXk-oA727.jpg\",\"uid\":1561719575042,\"status\":\"success\"},{\"url\":\"http://192.168.200.128:8080/group1/M00/00/00/wKjIgF0IrUaAT4sFAAAZIgrXilA369.jpg\",\"uid\":1561719575046,\"status\":\"success\"},{\"url\":\"http://192.168.200.128:8080/group1/M00/00/00/wKjIgF0IrUaADogZAAATxAf-zBo522.jpg\",\"uid\":1561719575049,\"status\":\"success\"}]');

8.直接运行项目

 

关于@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})注解作用 

如果没有DataSource相关的配置java类(类似在MybatisPlusConfig文件中配置多数据源),那么采用springboot自动装配,不使用上述注解,完成配置。具体使用参考https://blog.csdn.net/jinrucsdn/article/details/106539916博主的文章

 

备注:部分知识引用https://blog.csdn.net/qq_19309473/article/details/109362253?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.channel_param文章,感谢

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐