SpringBoot升级Mybatis为MybatisPlus遇到的问题及解决 | 您所在的位置:网站首页 › mybatis和mybatisplus混用 › SpringBoot升级Mybatis为MybatisPlus遇到的问题及解决 |
前言
MP的安装使用正如他官网说的异常简单,但是在原有的Mybatis项目兼容Mybatis-Plus还是遇到了些许问题:MyBatisPlus的整合(springboot)和使用。 先说结论: 检测依赖是否正确引入; 检测使用的BaseMapper是否正确的依赖; 检查POJO对应表名称,字段名称是否对应,若表存在主键,需要指定POJO中对应主键(用注解 @TableId 注解表 ID 主键); 检查是否被扫描到Dao:@MapperScan; application.properties配置是否恰当; 最终我的问题是:mybatis.mapper-locations和mybatis-plus.mapper-locations导致的。这里我只记录这个过程,1、2、3、4点已在基础博客中有写到: 最初尝试运行 #mybatis相关配置文件 mybatis.mapper-locations=classpath*:mapper/*Dao.xml #mybatis-plus.mapper-locations=classpath*:mapper/*Dao.xml出现了问题,进行了错误的尝试。 错误的尝试刚开始测试用的是baseMapper里面的selectList方法,未用到自行编写的映射mapper.xml文件;误以为需要将 mybatis 的 sqlSessionFactoryBean给替换: 添加appliication.properties配置: mybatis-plus.global-config.sql-session-factory=com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean报错: 终归原因还是配置赋值是String,而要求是SqlSessonFactory。 正确的解决方法分别给mybatis和mp指定映射文件对象: 值得注意的是如果mybatis和mybatis-plus两个都写了就要保持一致,如下,如果一个写了,另外一个没写,则会报对应的配置找不到: #mybatis相关配置文件 mybatis.mapper-locations=classpath*:mapper/*Dao.xml mybatis-plus.mapper-locations=classpath*:mapper/*Dao.xml单独只配置mybatis,则无法支持mp: 报错: Unsatisfied dependency expressed through bean property ‘sqlSessionFactory’; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘sqlSessionFactory’ defined in class path resource [com/baomidou/mybatisplus/autoconfigure/MybatisPlusAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.ibatis.session.SqlSessionFactory]: Factory method ‘sqlSessionFactory’ threw exception; nested exception is org.springframework.core.NestedIOException: Failed to parse mapping resource: ‘xx’ \target\classes\mapper\config\sqlMapConfig.xml]’. Cause: java.lang.NullPointerException’ 单独将mybatis.mapper-locations和mybatis.config-location替换为对应的mp,则无法兼容原有的mybatis代码; 最后添加: #mybatis相关配置文件 mybatis.mapper-locations=classpath*:mapper/*Dao.xml mybatis.config-location=classpath:mapper/config/sqlMapConfig.xml mybatis-plus.mapper-locations=classpath*:mapper/*Dao.xml mybatis-plus.config-location=classpath:mapper/config/sqlMapConfig.xml |
CopyRight 2018-2019 实验室设备网 版权所有 |