您好,登錄后才能下訂單哦!
這篇文章主要介紹“springBoot集成mybatis轉(zhuǎn)換為mybatis-plus怎么實現(xiàn)”,在日常操作中,相信很多人在springBoot集成mybatis轉(zhuǎn)換為mybatis-plus怎么實現(xiàn)問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”springBoot集成mybatis轉(zhuǎn)換為mybatis-plus怎么實現(xiàn)”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.2</version> </dependency>
#mybatis-plus mybatis-plus: mapperPackage: com.xn.mapper typeAliasesPackage: com.xn.mapper mapperLocations: classpath:mapper/*.xml global-config: db-config: id-type: none configuration: # 字段下劃線轉(zhuǎn)駝峰 map-underscore-to-camel-case: false log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
(用于擴展 分頁/批量新增修改/更多的方法修改 基礎(chǔ)可以不加)
model繼承后可以 id自增會回填,更新時間在修改時刷新,創(chuàng)建時間在創(chuàng)建時刷新
import com.baomidou.mybatisplus.annotation.FieldFill; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import lombok.AllArgsConstructor; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; import lombok.experimental.SuperBuilder; import java.io.Serializable; import java.util.Date; @Getter @Setter @SuperBuilder @NoArgsConstructor @AllArgsConstructor public abstract class BaseEntity implements Serializable { /** * 創(chuàng)建時間 自增方式 */ @TableField(fill = FieldFill.INSERT) public Date create_time; /** * 更新時間 */ @TableField(fill = FieldFill.INSERT_UPDATE) public Date update_time; /** * ID */ @TableId(value="id" ,type = IdType.AUTO) private Long id; }
設(shè)置刷新 更新時間 創(chuàng)建時間
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler; import org.apache.ibatis.reflection.MetaObject; import java.util.Date; /** * MP注入處理器 */ public class CreateAndUpdateMetaObjectHandler implements MetaObjectHandler { public String CREATE_TIME = "create_time"; public String UPDATE_TIME = "update_time"; @Override public void insertFill(MetaObject metaObject) { if (metaObject.hasGetter(CREATE_TIME)) { if (metaObject.getValue(CREATE_TIME) == null) { this.setFieldValByName(CREATE_TIME, new Date(), metaObject); } } } @Override public void updateFill(MetaObject metaObject) { if (metaObject.hasGetter(UPDATE_TIME)) { if (metaObject.getValue(UPDATE_TIME) == null) { this.setFieldValByName(UPDATE_TIME, new Date(), metaObject); } } } }
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import java.util.List; /** * 自定義 Mapper 接口, 實現(xiàn) 自定義擴展 * */ public interface BaseMapperPlus<T> extends BaseMapper<T> { /** * 批量插入(mysql) * @param entityList * @return */ Integer insertBatchSomeColumn(List<T> entityList); /** * 批量更新(mysql) * @param entityList * @return */ Integer updateBatchSomeColumn(List<T> entityList); }
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; /** * mybatis + sql注入器 */ 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); } }
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; /** * mybatis-plus配置類 */ @EnableTransactionManagement(proxyTargetClass = true) @Configuration @MapperScan("${mybatis-plus.mapperPackage}") public class MybatisPlusConfig { @Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); // 分頁插件 interceptor.addInnerInterceptor(paginationInnerInterceptor()); // 樂觀鎖插件 interceptor.addInnerInterceptor(optimisticLockerInnerInterceptor()); return interceptor; } /** * 分頁插件,自動識別數(shù)據(jù)庫類型 */ public PaginationInnerInterceptor paginationInnerInterceptor() { PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor(); // 設(shè)置數(shù)據(jù)庫類型為mysql paginationInnerInterceptor.setDbType(DbType.MYSQL); // 設(shè)置最大單頁限制數(shù)量,默認 500 條,-1 不受限制 paginationInnerInterceptor.setMaxLimit(-1L); return paginationInnerInterceptor; } /** * 樂觀鎖插件 */ public OptimisticLockerInnerInterceptor optimisticLockerInnerInterceptor() { return new OptimisticLockerInnerInterceptor(); } /** * 元對象字段填充控制器 */ @Bean public MetaObjectHandler metaObjectHandler() { return new CreateAndUpdateMetaObjectHandler(); } /** * sql注入器配置 */ @Bean public 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); }
/** * 刪除狀態(tài) 0未刪除,1刪除 */ @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); }
到此,關(guān)于“springBoot集成mybatis轉(zhuǎn)換為mybatis-plus怎么實現(xiàn)”的學習就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續(xù)學習更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。