【Spring Boot 採坑記】- MyBatis

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>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章