springBoot 整合mybatis-plus以及相关问题整理

搭建springboot 工程

如果https://start.spring.io/ 链接不上的话,可以使用阿里的https://start.aliyun.com/来构建。

相关依赖
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>3.1.0</version>
        </dependency>

        <dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
        </dependency>

        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.1.0</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.6</version>
        </dependency>

        <!--swagger-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>
mybatis-plus 配置
@Configuration
// mapper 扫描
@MapperScan("com.lihp.easyuitest.*.mapper*")
public class MabatisConfig {
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        // 设置请求的页面大于最大页后操作, true调回到首页,false 继续请求  默认false
        // paginationInterceptor.setOverflow(false);
        // 设置最大单页限制数量,默认 500 条,-1 不受限制
        // paginationInterceptor.setLimit(500);
        // 开启 count 的 join 优化,只针对部分 left join
        return paginationInterceptor;
    }
}
属性文件
@Configuration
#mybatis-plus
mybatis-plus.mapper-locations=classpath:com/lihp/easyuitest/**/*.xml
mybatis-plus.type-aliases-package=com.lihp.easyuitest.*.entity
mybatis-plus.configuration.map-underscore-to-camel-case: true
代码生成器

这里可以直接使用官方文档中的实例。
链接: 跳转链接.

过程中碰到的问题

Invalid bound statement (not found)

可能造成的原因
  • mapperxml 的namespace 和类全部路径不匹配
  • sql 片段id 和方法名称不匹配
  • parameterType和resultType 分别对应方法的入参和出参,类型有可能没有匹配
  • mybatis-plus.mapper-locations xml配置路径错误
  • maven打包的时候没有将xml文件打包的到jar包中。具体参考下图这个问题比较坑,不太好发现。
解决方法
  1. 针对于前面几个自己检查代码,通过插件Free Mybatis plugin可以快速检查问题
  2. 针对pom 打包问题,可以在pom.xml 中配置资源打包
<!-- 将mapper.xml一起打包 -->
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.*</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>

在这里插入图片描述

相关资料
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值