springBoot集成mybatis转换为mybatis-plus怎么实现
短信预约 -IT技能 免费直播动态提醒
这篇文章主要介绍“springBoot集成mybatis转换为mybatis-plus怎么实现”,在日常操作中,相信很多人在springBoot集成mybatis转换为mybatis-plus怎么实现问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”springBoot集成mybatis转换为mybatis-plus怎么实现”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
导入maven
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.2</version> </dependency>
更新yml文件
#mybatis-plusmybatis-plus: mapperPackage: com.xn.mapper typeAliasesPackage: com.xn.mapper mapperLocations: classpath:mapper @TableField(fill = FieldFill.INSERT) public Date create_time; @TableField(fill = FieldFill.INSERT_UPDATE) public Date update_time; @TableId(value="id" ,type = IdType.AUTO) private Long id;}
CreateAndUpdateMetaObjectHandler
设置刷新 更新时间 创建时间
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;import org.apache.ibatis.reflection.MetaObject;import java.util.Date;public class CreateAndUpdateMetaObjectHandler implements MetaObjectHandler {public String CREATE_TIME = "create_time";public String UPDATE_TIME = "update_time";@Overridepublic void insertFill(MetaObject metaObject) {if (metaObject.hasGetter(CREATE_TIME)) {if (metaObject.getValue(CREATE_TIME) == null) {this.setFieldValByName(CREATE_TIME, new Date(), metaObject);}}}@Overridepublic void updateFill(MetaObject metaObject) {if (metaObject.hasGetter(UPDATE_TIME)) {if (metaObject.getValue(UPDATE_TIME) == null) {this.setFieldValByName(UPDATE_TIME, new Date(), metaObject);}}}}
批量插入/更新 mapper需要继承这个接口
import com.baomidou.mybatisplus.core.mapper.BaseMapper;import java.util.List;public interface BaseMapperPlus<T> extends BaseMapper<T> { Integer insertBatchSomeColumn(List<T> entityList); Integer updateBatchSomeColumn(List<T> entityList);}
将批量方法放到 sql注入器中
import com.baomidou.mybatisplus.core.injector.AbstractMethod;import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector;import com.baomidou.mybatisplus.extension.injector.methods.InsertBatchSomeColumn;import java.util.List;public class MybatisPlusSqlInjector extends DefaultSqlInjector { @Override public List<AbstractMethod> getMethodList(Class<?> mapperClass) { List<AbstractMethod> methodList = super.getMethodList(mapperClass); // 添加InsertBatchSomeColumn方法 methodList.add(new InsertBatchSomeColumn()); methodList.add(new UpdateBatchSomeColumn()); return methodList; }
实现批量更新的方法
import com.baomidou.mybatisplus.core.injector.AbstractMethod;import com.baomidou.mybatisplus.core.metadata.TableInfo;import org.apache.ibatis.mapping.MappedStatement;import org.apache.ibatis.mapping.SqlSource;public class UpdateBatchSomeColumn extends AbstractMethod { @Override public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) { String sql = "<script>\n<foreach collection=\"list\" item=\"item\" separator=\";\">\nupdate %s %s where %s=#{%s} %s\n</foreach>\n</script>"; String additional = tableInfo.isWithVersion() ? tableInfo.getVersionFieldInfo().getVersionOli("item", "item.") : "" + tableInfo.getLogicDeleteSql(true, true); String setSql = sqlSet(tableInfo.isWithLogicDelete(), false, tableInfo, false, "item", "item."); String sqlResult = String.format(sql, tableInfo.getTableName(), setSql, tableInfo.getKeyColumn(), "item." + tableInfo.getKeyProperty(), additional); SqlSource sqlSource = languageDriver.createSqlSource(configuration, sqlResult, modelClass); return this.addUpdateMappedStatement(mapperClass, modelClass, "updateBatchSomeColumn", sqlSource); }}
mybatis-plus配置类
import com.baomidou.mybatisplus.annotation.DbType;import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;import org.mybatis.spring.annotation.MapperScan;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.transaction.annotation.EnableTransactionManagement;@EnableTransactionManagement(proxyTargetClass = true)@Configuration@MapperScan("${mybatis-plus.mapperPackage}")public class MybatisPlusConfig {@Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor() {MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();// 分页插件interceptor.addInnerInterceptor(paginationInnerInterceptor());// 乐观锁插件interceptor.addInnerInterceptor(optimisticLockerInnerInterceptor());return interceptor;}public PaginationInnerInterceptor paginationInnerInterceptor() {PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();// 设置数据库类型为mysqlpaginationInnerInterceptor.setDbType(DbType.MYSQL);// 设置最大单页限制数量,默认 500 条,-1 不受限制paginationInnerInterceptor.setMaxLimit(-1L);return paginationInnerInterceptor;}public OptimisticLockerInnerInterceptor optimisticLockerInnerInterceptor() {return new OptimisticLockerInnerInterceptor();}@Beanpublic MetaObjectHandler metaObjectHandler() {return new CreateAndUpdateMetaObjectHandler();}@Beanpublic MybatisPlusSqlInjector easySqlInjector () {return new MybatisPlusSqlInjector();}}
还有两个是分页与查询方式可以自己定义
分页类需要继承 IPage,查询类可以继承 IService
开始测试
使用分页查询
@Override public ServerResponse selectTableTestList(TableTestPOJO tableTest) { // 分页查询 1 sql自己写 适用于多表 Page<TableTest> page = new Page<>(mutualStep.getPageNum(), mutualStep.getPageSize()); page = tableTestMapper.findTableList(page,new TableTest()); // 分页查询 2 对象筛选 适用单表 条件默认相等 QueryWrapper<TableTest> query = Wrappers.query(); query.like("name","ls"); query.and( wrapper -> wrapper.notLike("name","1").or().like("name","ls") ); query.orderByDesc("id"); Page<TableTest> page = new Page<>(tableTest.getPageNum(), tableTest.getPageSize()); Page<TableTest> pageList = tableTestMapper.selectPage(page, query); return ServerResponse.createBySuccess(pageList); }
逻辑删除定义
@TableLogic(value = "0",delval = "1") private Integer is_del;
逻辑删除
@Override public ServerResponse deleteTableTest(MutualStepPage mutualStepPage, Integer... ids) { int number = tableTestMapper.deleteBatchIds(Arrays.asList(ids)); return ServerResponse.createBySuccess(number); }
到此,关于“springBoot集成mybatis转换为mybatis-plus怎么实现”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注编程网网站,小编会继续努力为大家带来更多实用的文章!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341