In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to realize the conversion from springBoot integration mybatis to mybatis-plus". In daily operation, I believe many people have doubts about how to realize the conversion from springBoot integration mybatis to mybatis-plus. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the question of "how to convert springBoot integration mybatis to mybatis-plus". Next, please follow the editor to study!
Import maven com.baomidou mybatis-plus-boot-starter 3.4.2 and update the yml file # mybatis-plusmybatis-plus: mapperPackage: com.xn.mapper typeAliasesPackage: com.xn.mapper mapperLocations: classpath:mapper/*.xml global-config: db-config: id-type: none configuration: # fields underlined to hump map-underscore-to-camel-case: false log-impl: org.apache.ibatis.logging.stdout.StdOutImpl add extension file
(for expanding paging / adding modifications in batches / more methods to modify the base may not be added)
BaseEntity is used to define model
After model inheritance, you can add id to backfill. The update time is refreshed when modified, and the creation time is refreshed when created.
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@AllArgsConstructorpublic abstract class BaseEntity implements Serializable {/ * self-increasing creation time * / @ TableField (fill = FieldFill.INSERT) public Date create_time; / * Update time * / @ TableField (fill = FieldFill.INSERT_UPDATE) public Date update_time / * ID * / @ TableId (value= "id", type = IdType.AUTO) private Long id;} CreateAndUpdateMetaObjectHandler
Set refresh update time creation time
Import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;import org.apache.ibatis.reflection.MetaObject;import java.util.Date;/** * MP injection processor * / 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) }} batch insert / update mapper needs to inherit this interface import com.baomidou.mybatisplus.core.mapper.BaseMapper;import java.util.List / * Custom Mapper API to implement custom extension * * / public interface BaseMapperPlus extends BaseMapper {/ * bulk insert (mysql) * @ param entityList * @ return * / Integer insertBatchSomeColumn (List entityList); / * batch update (mysql) * @ param entityList * @ return * / Integer updateBatchSomeColumn (List entityList) } put batch methods into sql injector 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 injector * / public class MybatisPlusSqlInjector extends DefaultSqlInjector {@ Override public List getMethodList (Class mapperClass) {List methodList = super.getMethodList (mapperClass) / / add InsertBatchSomeColumn method methodList.add (new InsertBatchSomeColumn ()); methodList.add (new UpdateBatchSomeColumn ()); return methodList;} implementation of batch update method 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 / * batch update some columns * * / public class UpdateBatchSomeColumn extends AbstractMethod {@ Override public MappedStatement injectMappedStatement (Class mapperClass, Class modelClass, TableInfo tableInfo) {String sql = "\ n\ nupdate% s% s where% slots # {% s}% s\ n\ n"; 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 configuration class 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 configuration class * / @ EnableTransactionManagement (proxyTargetClass = true) @ Configuration@MapperScan ("${mybatis-plus.mapperPackage}") public class MybatisPlusConfig {@ Bean public MybatisPlusInterceptor mybatisPlusInterceptor () {MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor (); / / paging plug-in interceptor.addInnerInterceptor (paginationInnerInterceptor ()) / / optimistic lock plug-in interceptor.addInnerInterceptor (optimisticLockerInnerInterceptor ()); return interceptor;} / * paging plug-in, automatically identify database type * / public PaginationInnerInterceptor paginationInnerInterceptor () {PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor () / / set the database type to mysql paginationInnerInterceptor.setDbType (DbType.MYSQL); / / set the maximum number of single page limits. Default is 500entries,-1 unrestricted paginationInnerInterceptor.setMaxLimit (- 1L), return paginationInnerInterceptor } / * * optimistic lock plug-in * / public OptimisticLockerInnerInterceptor optimisticLockerInnerInterceptor () {return new OptimisticLockerInnerInterceptor ();} / * meta-object field fill controller * / @ Bean public MetaObjectHandler metaObjectHandler () {return new CreateAndUpdateMetaObjectHandler () } / * sql injector configuration * / @ Bean public MybatisPlusSqlInjector easySqlInjector () {return new MybatisPlusSqlInjector ();}}
The other two are paging and query methods that can be defined by yourself.
Paging classes need to inherit IPage, while query classes can inherit IService
Start testing using paging query @ Override public ServerResponse selectTableTestList (TableTestPOJO tableTest) {/ / paging query 1 sql is suitable for multi-table Page page = new Page (mutualStep.getPageNum (), mutualStep.getPageSize ()); page = tableTestMapper.findTableList (page,new TableTest ()); / / paging query 2 object filtering applies single table condition default equal QueryWrapper query = Wrappers.query () Query.like ("name", "ls"); query.and (wrapper-> wrapper.notLike ("name", "1"). Or (). Like ("name", "ls"); query.orderByDesc ("id"); Page page = new Page (tableTest.getPageNum (), tableTest.getPageSize ()); Page pageList = tableTestMapper.selectPage (page, query) Return ServerResponse.createBySuccess (pageList);} logical delete definition / * delete status 0 is not deleted, 1 delete * / @ TableLogic (value = "0", delval = "1") private Integer is_del; logical delete @ Override public ServerResponse deleteTableTest (MutualStepPage mutualStepPage, Integer...) Ids) {int number = tableTestMapper.deleteBatchIds (Arrays.asList (ids)); return ServerResponse.createBySuccess (number);} at this point, the study on "how to convert springBoot integration mybatis to mybatis-plus" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.