mybatis升级为mybatis-plus
1.背景
为了快速开发,需要把之前的老项目升级为mybatis-plus
2.步骤
步骤一:导入jar包
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.0-RELEASE</version> </dependency>
步骤二:删除mybatis-spring桥梁包
一定要删除之前与mybatis相关的包,否则可能会报错
<!--mybatis-spring桥梁-->
<!-- <dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>-->
步骤三:添加配置参数
mybatis-plus.mapper-locations=classpath:mapper/*.xml
mybatis-plus.global-config.db-config.field-strategy=not_empty
mybatis-plus.global-config.db-config.id-type=input
#mybatis-plus.configuration.map-underscore-to-camel-case=true
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
mybatis-plus.configuration.call-setters-on-nulls=true
步骤四:添加java配置对象
package com.XXXXX.biz.config; import com.baomidou.mybatisplus.autoconfigure.SpringBootVFS; 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.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.transaction.annotation.EnableTransactionManagement; import javax.sql.DataSource; /** * @Copyright (C) XXXXXXXXXXX科技股份技有限公司 * @Author: lidongping * @Date: 2021-10-27 14:54 * @Description: */ @Configuration @MapperScan(basePackages = {"com.qianxingniwo.biz.mapper"}) @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; } }
3.使用测试
到这里大家就可以使用mybatis-plus了