Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What should be paid attention to when upgrading from mybatis to mybatis-plus

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

Xiaobian to share with you mybatis upgrade to mybatis-plus need to pay attention to what, I believe most people do not know how, so share this article for your reference, I hope you read this article after a great harvest, let us go to understand it together!

preface

Recently RuoYi-Vue was used to manage scaffolding in the background. RuoYi-Vue is a Java EE enterprise-level rapid development platform, based on classic technology combinations (Spring Boot, Spring Security, MyBatis, Jwt, Vue), built-in modules such as: department management, role users, menu and button authorization, data permissions, system parameters, log management, code generation, etc. Online scheduled task configuration; support clustering, support multiple data sources. Its official documentation is as follows

http://doc.ruoyi.vip/

Interested friends, you can click on the link to view. The platform's current org framework is mybatis, while the project team's org framework is mybatis-plus. In order to unify the technology stack, the project team decided to upgrade Ruoyi's orm framework to mybatis-plus. Because I had experience upgrading mybatis to mybatis-plus before, I felt that this upgrade was very simple. However, after the modification, the running program reported the following exception

Invalid bound statement (not found): com.lybgeek.admin.file.mapper.FileMapper.insert search

From the literal meaning of exception, the insert method in FIleMapper has no binding. Check FileMapper.xml configuration, indeed did not find the binding insert sql statement block. Does adding the SQL statement block of insert solve the problem? Plus it does solve the problem.

However, if you have used mybatis-plus friends, you should know that BaseMapper in mybatis-plus has helped us package a series of single table additions, deletions and queries. We can implement single table additions, deletions and queries without writing configuration. Therefore, insert configuration in xml is a temporary solution.

So how do we investigate?

1. Direction 1: Is it caused by packet conflict?

Using maven helper plug-in package conflicts

It can be seen from the diagram that it is not caused by packet collision.

Note: Because I suffered from package conflicts before, I have already removed jar conflicts related to mybatis before changing the ORM of Ruoyi to mybatis-plus.

Direction 2: Is BaseMapper introduced into different packages

We have to introduce

import com.baomidou.mybatisplus.core.mapper.BaseMapper;

rather than

import com.baomidou.mybatisplus.mapper.BaseMapper;

However, this problem usually occurs when different versions of mybatis-plus jar are introduced. If you only use version 3+, he introduces only

import com.baomidou.mybatisplus.core.mapper.BaseMapper;

Direction 3: General method (breakpoint debugging)

In fact, code troubleshooting is most afraid of the exception stack was eaten, if there is abnormal information, the direction of troubleshooting is relatively easy to find. For example, this exception, its exception stack information is

Caused by: org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.lybgeek.admin.file.mapper.FileMapper.insert at org.apache.ibatis.binding.MapperMethod$SqlCommand. (MapperMethod.java:235) at org.apache.ibatis.binding.MapperMethod. (MapperMethod.java:53) at org.apache.ibatis.binding.MapperProxy.lambda$cachedInvoker$0(MapperProxy.java:107) at java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1660) at org.apache.ibatis.binding.MapperProxy.cachedInvoker(MapperProxy.java:94) at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:85) at com.sun.proxy.$ Proxy129.insert(Unknown Source) at com.baomidou.mybatisplus.extension.service.IService.save(IService.java:59) at com.baomidou.mybatisplus.extension.service.IService$$FastClassBySpringCGLIB$$f8525d18.invoke() at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)

We know from the exception stack information that we can know this exception from

org.apache.ibatis.binding.MapperMethod

This class throws, so we can set the breakpoint here first. Through the source code we can know that org.apache.ibatis.mapping.MappedStatement is empty, resulting in the above exception, and MappedStatement is composed of

org.apache.ibatis.session.Configuration

Provided. Configuration is through

org.apache.ibatis.session.SqlSessionFactory

Make settings. And if you keep digging, you'll find

com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration

This automatic assembly class. There's this code in there.

@Bean @ConditionalOnMissingBean public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception { // TODO uses MybatisSqlSessionFactoryBean instead of SqlSessionFactoryBean MybatisSqlSessionFactoryBean factory = new MybatisSqlSessionFactoryBean(); factory.setDataSource(dataSource); factory.setVfs(SpringBootVFS.class); if (StringUtils.hasText(this.properties.getConfigLocation())) { factory.setConfigLocation(this.resourceLoader.getResource(this.properties.getConfigLocation())); } applyConfiguration(factory); if (this.properties.getConfigurationProperties() != null) { factory.setConfigurationProperties(this.properties.getConfigurationProperties()); } if (! ObjectUtils.isEmpty(this.interceptors)) { factory.setPlugins(this.interceptors); } if (this.databaseIdProvider != null) { factory.setDatabaseIdProvider(this.databaseIdProvider); } if (StringUtils.hasLength(this.properties.getTypeAliasesPackage())) { factory.setTypeAliasesPackage(this.properties.getTypeAliasesPackage()); } if (this.properties.getTypeAliasesSuperType() != null) { factory.setTypeAliasesSuperType(this.properties.getTypeAliasesSuperType()); } if (StringUtils.hasLength(this.properties.getTypeHandlersPackage())) { factory.setTypeHandlersPackage(this.properties.getTypeHandlersPackage()); } if (! ObjectUtils.isEmpty(this.typeHandlers)) { factory.setTypeHandlers(this.typeHandlers); } Resource[] mapperLocations = this.properties.resolveMapperLocations(); if (! ObjectUtils.isEmpty(mapperLocations)) { factory.setMapperLocations(mapperLocations); } // TODO makes some changes to the source code (because the source code adapts to the old mybatis version, but we don't need to adapt it) Class

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report