SpringBoot項目將mybatis升級為mybatis-plus的方法
最近做的項目是用的開源的一個項目改造得來的,而且項目是19年就已經(jīng)停止維護(hù)了,項目的年齡比我工作經(jīng)驗還長,而且我們要在原來的接口上進(jìn)行改動,但為了兼容前端,所以很多接口改起來很麻煩,新寫的話還要寫很多sql,于是就想將mybatis升級成mybatis-plus,簡化一下開發(fā)
第一步,引入maven依賴
<!-- 注釋掉原來的mybatis,否則可能會報錯 -->
<!-- <dependency>-->
<!-- <groupId>org.mybatis.spring.boot</groupId>-->
<!-- <artifactId>mybatis-spring-boot-starter</artifactId>-->
<!-- </dependency>-->
<!-- 引入 mybatis-plus 的包 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-extension</artifactId>
<version>3.4.1</version>
<scope>compile</scope>
</dependency>
<!-- 引入 generator,代碼生成器需要用到,如果不用代碼生成器則不需要引入 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.4.1</version>
</dependency>
<!-- 引入 freemarker -->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
</dependency>添加配置類
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.extension.incrementer.OracleKeyGenerator;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.github.pagehelper.PageInterceptor;
import org.apache.ibatis.io.VFS;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.boot.autoconfigure.SpringBootVFS;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.sql.DataSource;
/**
?* @author javaer
?* @date 2021/11/11 13:35
?**/
@Configuration
//掃描的mapper文件夾地址
@MapperScan(basePackages = {"cn.com.anzhiyi.exam.mapper"})
//開啟注解事務(wù)管理
@EnableTransactionManagement
public class MybatisPlusConfig {
? ? /**
? ? ?* 分頁插件
? ? ?*/
? ? @Bean
? ? public PaginationInterceptor paginationInterceptor() {
? ? ? ? return new PaginationInterceptor();
? ? }
? ? /**
? ? ?* pagehelper的分頁插件
? ? ?*/
? ? @Bean
? ? public PageInterceptor pageInterceptor() {
? ? ? ? return new PageInterceptor();
? ? }
? ? @Bean
? ? public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
? ? ? ? String mapperLocations = "classpath:mapper/*.xml";
? ? ? ? VFS.addImplClass(SpringBootVFS.class);
? ? ? ? final MybatisSqlSessionFactoryBean sessionFactory = new MybatisSqlSessionFactoryBean();
? ? ? ? sessionFactory.setDataSource(dataSource);
? ? ? ? sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(mapperLocations));
? ? ? ? // 配置組件生成策略
? ? ? ? sessionFactory.setGlobalConfig(globalConfig());
? ? ? ? return sessionFactory.getObject();
? ? }
? ? @Bean
? ? public GlobalConfig globalConfig() {
? ? ? ? GlobalConfig conf = new GlobalConfig();
? ? ? ? conf.setDbConfig(new GlobalConfig.DbConfig().setKeyGenerator(new OracleKeyGenerator()));
? ? ? ? return conf;
? ? }
}修改nacos或者配置文件中關(guān)于mybatis的配置
# mybatis配置
# mybatis: //將原先的mybatis注釋掉,調(diào)整為 mybatis-plus 即可
mybatis-plus:
type-aliases-package: cn.com.anzhiyi.exam.api.module
configuration:
# 駝峰轉(zhuǎn)換
map-underscore-to-camel-case: true
# 延遲加載
lazy-loading-enabled: true
mapper-locations: classpath:mapper/*.xml
基本需要改動的就是上面這些,接下來就是要將相應(yīng)的代碼進(jìn)行一下生成,我們即可使用mybatis-plus進(jìn)行開發(fā)了,由于mybatis-plus是在mybatis的基礎(chǔ)上進(jìn)行的封裝,所以對其的兼容性做的很好,之前的代碼最好就不要再動了,要對線上保持敬畏。如果后續(xù)需要新開發(fā)的接口和功能,我們可以采用mybatis-plus進(jìn)行開發(fā)了
到此這篇關(guān)于SpringBoot項目將mybatis升級為mybatis-plus的方法的文章就介紹到這了,更多相關(guān) mybatis升級為mybatis-plus內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Retrofit+RxJava實現(xiàn)帶進(jìn)度下載文件
這篇文章主要為大家詳細(xì)介紹了Retrofit+RxJava實現(xiàn)帶進(jìn)度下載文件,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-05-05
Java實現(xiàn)Excel百萬級數(shù)據(jù)導(dǎo)入功能的示例代碼
這篇文章主要為大家詳細(xì)介紹了Java如何實現(xiàn)Excel百萬級數(shù)據(jù)導(dǎo)入功能,文中的示例代碼講解詳細(xì),具有一定的借鑒價值,有需要的小伙伴可以參考下2024-04-04
Slf4j+logback實現(xiàn)JSON格式日志輸出方式
這篇文章主要介紹了Slf4j+logback實現(xiàn)JSON格式日志輸出方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12

