Spring整合Mybatis-plus

今天睡不着,搭建一个SpringBoot整合Mybatis-plus都花了一上午,尴尬,还踩了很多坑。记录下

Springboot整合Mybatis整合步骤:

  1. 添加依赖
<!--mybatis-plus-->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.2.0</version>
</dependency>
<!--mysql驱动-->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.17</version>
</dependency>
  1. 设置配置文件
mybatis-plus:
  mapper-locations: classpath*:/mapper/**/*.xml
#  global-config:
#    db-config:
#      id-type: auto
  1. 添加注解
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@MapperScan("com.wx.springboottest.dao")
@SpringBootApplication
public class SpringbootTestApplication {

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

}

Invalid bound statement (not found)报错

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.wx.springboottest.dao.UserDao.findByName

	at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:235)
	at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.<init>(MybatisMapperMethod.java:49)
	at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.lambda$cachedMapperMethod$0(MybatisMapperProxy.java:65)
	at java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1660)
	at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.cachedMapperMethod(MybatisMapperProxy.java:65)
	at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.invoke(MybatisMapperProxy.java:60)
	at com.sun.proxy.$Proxy68.findByName(Unknown Source)
	at com.wx.springboottest.service.impl.UserServiceImpl.findByName(UserServiceImpl.java:31)

这个报错,大家可以按照这个步骤进行排查

  1. 查看SpringBoot启动类上的MapperScan注解的路径设置是否正确
  2. 看.xml配置文件中是否将<mapper namespace=地址配置是否正确
  3. <resultMap id=“user” type="地址配置是否正确
  4. application.yml文件中地址配置正确
mybatis-plus:
  mapper-locations: classpath*:/mapper/**/*.xml

如果这几个地方都检查过了,可以看下生成的这个包的文件,是否正确。不能直接mapper.user这样创建目录,需要一级一级目录的创建,不然就会是下面这样
错误案例:
在这里插入图片描述
正确案例:
在这里插入图片描述

Springboot整合Mybatis分页插件

引入config配置类

//Spring boot方式
@Configuration
@MapperScan("com.wx.springboottest.dao")
public class MybatisPlusConfig {

    // 旧版
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        // 设置请求的页面大于最大页后操作, true调回到首页,false 继续请求  默认false
        // paginationInterceptor.setOverflow(false);
        // 设置最大单页限制数量,默认 500 条,-1 不受限制
        // paginationInterceptor.setLimit(500);
        // 开启 count 的 join 优化,只针对部分 left join
        paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
        return paginationInterceptor;
    }

    // 最新版
    // @Bean
    // public MybatisPlusInterceptor mybatisPlusInterceptor() {
    //     MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
    //     interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.H2));
    //     return interceptor;
    // }

}

在service层中进行调用逻辑的编写

@Override
public IPage<User> findAllByPage(Integer current, Integer size) {

     IPage<User> userPage = new Page<>();
     userPage.setCurrent(current);
     userPage.setSize(size);
     IPage<User> userIPage = userDao.selectPage(userPage, null);
     return userIPage;
 }

可以在application.yml配置文件中配置sql日志打印

logging:
  level:
    com.wx.springboottest: debug

我们可以发现,mybatis的分页插件调用的sql语句
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值