springboot搭配mybatisplus使用已作為常用的組合方式,本文介紹兩者整合時的步驟及遇到的坑。
1. 依賴
<!--mybatis-plus相關-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.1.2</version>
</dependency>
<!--數(shù)據(jù)庫相關-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--常用庫依賴-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.6</version>
</dependency>
2.配置文件
mybatis-plus:
mapper-locations: mapper/**/*.xml
type-aliases-package: com.example.mybatisplus.generator.entity
type-aliases-package的作用
<bean>
<property name = " typeAliasesPackage" value = " com.bean">
</property>
</bean>
設置這個以后再Mapper配置文件中在parameterType 的值就不用寫成全路徑名了
ep:parameterType="com.bean.User"可以寫成parameterType = "User"
mapper-locations的作用
讓框架找到xml文件,mapper//.xml 和classthpath:mapper//.xml 等價
3.項目結構

項目結構.png
4.小結
4.1 遇到的問題
遇到org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)問題,有些文章說要在pom文件的build節(jié)點下夾下面這段引用,因為idea忽略了resource下的xml文件。然后加了并沒有用,關鍵還是出在mybatis-plus.mapper-locations屬性配錯了。
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<testResources>
<testResource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</testResource>
</testResources>-->
4.2 看編譯結果
學會了從target目錄下看編譯結果。

image.png