搭建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包中
。具体参考下图这个问题比较坑,不太好发现。
解决方法
- 针对于前面几个自己检查代码,通过插件
Free Mybatis plugin
可以快速检查问题 - 针对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>
相关资料
- mybatis-plus 官方文档