springboot整合tx.mybatis遇到的问题


在springboot整合mybatis,使用tk.mybatis的时候,遇到了两个问题。写了个简单的demo后解决,在这里记录一下。

问题一:项目无法正常启动

报错内容

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-07-14 23:41:17.390 ERROR 108492 --- [  restartedMain] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Field mapper in com.cn.my.service.impl.UserMoneyServiceImpl required a bean of type 'com.cn.my.utils.mapper.UserMoneyMapper' that could not be found.

The injection point has the following annotations:
	- @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'com.cn.my.utils.mapper.UserMoneyMapper' in your configuration.

问题原因

未在启动类中配置扫描包,增加如下mapper扫描即可

@SpringBootApplication
//扫描 mybatis mapper 包路径
@MapperScan(basePackages = "com.cn.my.utils.mapper")
public class DemoApplication {

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

}

问题二:service调用mapper方法时报错

报错内容

在service中调用mapper接口方法的时候,报出以下问题

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.builder.BuilderException: Error invoking SqlProvider method (tk.mybatis.mapper.provider.base.BaseSelectProvider.dynamicSQL).  Cause: java.lang.InstantiationException: tk.mybatis.mapper.provider.base.BaseSelectProvider
	at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:77)
	at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:446)

问题原因

在启动类中导入了错误MapperScan类,应该导入tx.mytatis包下的,而代码中导入了org.mybatis下的,修正的代码如下

package com.cn.my;


// import org.mybatis.spring.annotation.MapperScan;   这是错误的导包
import tk.mybatis.spring.annotation.MapperScan;   // 这是正确的导包

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;




@SpringBootApplication
//扫描 mybatis mapper 包路径
@MapperScan(basePackages = "com.cn.my.utils.mapper")
public class DemoApplication {

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

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值