1.
pom.xml
添加MyBatis
依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.2</version>
</dependency>
2.
主类添加@MapperScan
注解,并指明mapper.java
所在包
@MapperScan("com.sapphire.framework.mapper")
public class SapphireSpringBootDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SapphireSpringBootDemoApplication.class, args);
}
}
3.
将mapper.xml
文件放到resources/com.sapphire.framework.mapper
同包名路径下,如若发现类似错误
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.sapphire.framework.mapper.DistrictTblMapper.selectAll
解决:
在pom.xml
文件中的build
标签下,添加如下依赖即可
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
若不想使用相同包名,则需要在application.yml
中添加如下配置
mybatis:
mapper-locations: classpath:mapper/**/*.xml
注意: 如此配置可能造成如下错误,找不到相应的配置文件.yml
或者.properties
Could not resolve placeholder ‘xxx’ in value "${xxx}
解决: <include>**/*.xml</include>
修改为 <include>**/*</include>