MyBatis和MyBatis Plus并存問題及解決
前言
Mybatis-plus:Mybatis增強(qiáng)工具,只做增強(qiáng),不作改變,簡化開發(fā),大大提高了開發(fā)效率。
經(jīng)常會碰到項目原先是采用MyBatis開發(fā),每次增加一張表,要配置實體類、xml文件等,操作上略顯復(fù)雜。因此,引入MyBatis Plus,對于新增加的表可以用MyBatis Plus來操作,而原有的表則保持不變,這樣就出現(xiàn)了MyBatis和MyBatis Plus并存的情況。下面以具體的例子來描述再一個原有項目的基礎(chǔ)上如何添加MyBatis Plus。
一、pom.xml文件修改
加入依賴:
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.4.1</version>
</dependency>二、application.yml文件修改
原有的MyBatis片段:
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.qqh.demo.mybatis.bean只需要將mybatis修改為:mybatis-plus
mybatis-plus:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.qqh.demo.mybatis.bean三、新增實體類
@Builder
@Data
@TableName("`user`")
public class User {
private Long id;
private String name;
private Integer age;
}
四、新增MyBatis Plus Mapper類
public interface UserMapper extends BaseMapper<User> {
}五、新增UserMapperTest類
做完上述幾步改造,已經(jīng)算改造完成,這時候可以添加UserMapperTest測試類進(jìn)行測試。新增加的測試類代碼如下:
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserMapperTest {
@Autowired
private UserMapper userMapper;
/**
* 查詢測試
*/
@Test
public void selectListTest() {
List<User> userList = userMapper.selectList(null);
Assert.assertEquals("user list size 不等于 5",5, userList.size());
}
@Test
public void insertTest() {
userMapper.insert(User.builder().name("qqh1").age(21).build());
userMapper.insert(User.builder().name("qqh2").age(22).build());
userMapper.insert(User.builder().name("qqh3").age(23).build());
userMapper.insert(User.builder().name("qqh4").age(24).build());
userMapper.insert(User.builder().name("qqh5").age(25).build());
}
@Test
public void updateTest() {
userMapper.updateById(User.builder().id(1751458136121847809L).name("xiaohong1-update").build());
}
@Test
public void deleteTest() {
userMapper.deleteById(User.builder().id(1751458136121847809L).build());
}
}
六、執(zhí)行測試方法,碰到問題
執(zhí)行測試方法:insertTest,啟動時出現(xiàn)如下問題:
***************************
APPLICATION FAILED TO START
***************************
Description:
An attempt was made to call a method that does not exist. The attempt was made from the following location:
com.baomidou.mybatisplus.core.MybatisMapperAnnotationBuilder$AnnotationWrapper.<init>(MybatisMapperAnnotationBuilder.java:686)
The following method did not exist:
org.apache.ibatis.annotations.Select.databaseId()Ljava/lang/String;
The method's class, org.apache.ibatis.annotations.Select, is available from the following locations:
jar:file:/D:/mavenRepository/org/mybatis/mybatis/3.5.3/mybatis-3.5.3.jar!/org/apache/ibatis/annotations/Select.class
It was loaded from the following location:
file:/D:/mavenRepository/org/mybatis/mybatis/3.5.3/mybatis-3.5.3.jar
問題排查:
1.錯誤分析
根據(jù)上述錯誤信息,發(fā)現(xiàn)集中在mybatis-3.5.3.jar,懷疑是jar包版本沖突引起的。
2.依賴分析
對pom.xml中的依賴進(jìn)行分析,如下圖,發(fā)現(xiàn)mybatis-plus-boot-starter和mybatis-spring-boot-starter引用的mybatis包版本不一致。

3.解決
將mybatis-spring-boot-starter版本從2.1.1升級到2.3.1,使其使用的mybatis版本和mybatis-plus-boot-starter所使用的mybatis版本保持一致,再次運行測試方法insertTest,執(zhí)行成功。查看數(shù)據(jù)庫,如下圖,數(shù)據(jù)已正確插入。

總結(jié)
MyBatis Plus確實可以大幅提高開發(fā)效率,上例中的UserMapper不需要寫1行代碼,即可實現(xiàn)增刪改查。
到此這篇關(guān)于MyBatis和MyBatis Plus并存問題及解決的文章就介紹到這了,更多相關(guān)MyBatis和MyBatis Plus并存內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java多態(tài)實現(xiàn)原理詳細(xì)梳理總結(jié)
這篇文章主要介紹了Java多態(tài)實現(xiàn)原理詳細(xì)梳理總結(jié),多態(tài)是繼封裝、繼承之后,面向?qū)ο蟮牡谌筇匦裕疚闹豢偨Y(jié)了多態(tài)的實現(xiàn)原理,需要的朋友可以參考一下2022-06-06
SpringBoot?@Scheduled?Cron表達(dá)式使用方式
這篇文章主要介紹了SpringBoot?@Scheduled?Cron表達(dá)式使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-03-03
SpringBoot開發(fā)實戰(zhàn)系列之定時器
定時任務(wù)我想諸位童鞋都不陌生,簡而言之名為“設(shè)定定時鬧鐘做某件事情”,下面這篇文章主要給大家介紹了關(guān)于SpringBoot定時器的相關(guān)資料,需要的朋友可以參考下2021-08-08
mybatisplus報Invalid bound statement (not found)錯誤的解決方法
搭建項目時使用了mybatisplus,項目能夠正常啟動,但在調(diào)用mapper方法查詢數(shù)據(jù)庫時報Invalid bound statement (not found)錯誤。本文給大家分享解決方案,感興趣的朋友跟隨小編一起看看吧2020-08-08
關(guān)于jd-gui啟動報This?program?requires?Java?1.8+的錯誤問題及解決方法
最近,在Mac使用上JD-GUI啟動時總是報錯,接下來通過本文給大家介紹關(guān)于jd-gui啟動報this?program?requires?Java?1.8+的錯誤問題及解決方法,需要的朋友可以參考下2022-05-05

