springboot的简化配置使web项目开发变得越来越高效,在springboot中,不需要定义太多的配置文件,取而代之的是用一些注解来实现配置。
springboot集成mybatis也是比较容易的,一些简单的sql语句,通过注解就能实现,不过对于复杂的sql语句,还是需要定义mapper文件的。定义mapper文件的时候需要注意,避免xml文件读取不到而产生Invalid bound statement这类错误。
通常这类错误是由于依赖包选错导致的,正确的引入包如下:
(1)引入mybatis-spring-boot-starter包
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
该引用包为springboot官方的mybatis集成包,对应的要在properties文件中定义mybatis.mapper-locations属性
#按实际路径配置
mybatis.mapper-locations=classpath:/mapper/**/*.xml
(2)引入mybatis-plus-boot-starter包
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.2.0</version>
</dependency>
对应的要在properties文件中定义mybatis-plus.mapper-locations属性
#按实际路径配置
mybatis-plus.mapper-locations=classpath:/mapper/**/*.xml