Mybatis plus (use of crud)

Prodigal son Tang Shao 2022-02-13 07:29:40 阅读数:238

mybatis use crud

MyBatis-Plus(CRUD Use )

1. Call in the control layer (controller)

explain :

  • mybatis-plus Encapsulates the business (IService) Interface method , To call... To the control layer . among , encapsulation CRUD use get Query a single line remove Delete list Query the collection page Pagination
  • mybatis-plus Generics inside T Is any entity object
  • mybatis-plus The object in it Wrapper by Conditional constructor
  • Insert , Modified transactions mybatis-plus It has been written for us , Don't worry about things

1. Insert ( Store the data )

save Method
// Insert a record ( Selection field , Strategy insertion )
boolean save(T entity);
// Insert ( Batch )
boolean saveBatch(Collection<T> entityList);
// Insert ( Batch insert , among batchSize Is the number of inserted batches ) 
boolean saveBatch(Collection<T> entityList, int batchSize);
Example :
1.// Insert user Object data 
userService.save(User);
2.// Batch insert user object 
List<User> list = new ArrayList<>();
userService.saveBatch(user);
3.// Batch insert 10 Time user object 
List<User> list = new ArrayList<>();
userService.saveBatch(user,10);

Parameter description

type Parameter name describe
T entity Entity object
Collection entityList Collection of entity objects
int batchSize Insert number of batches
SaveOrUpdate Method
// Query the database id Whether there is , There are update records , There is no record to insert 
boolean saveOrUpdate(T entity);
// according to updateWrapper Query whether the data exists , Existential use T Object update , Insert if it doesn't exist 
boolean saveOrUpdate(T entity, Wrapper<T> updateWrapper);
// Batch modify insert , If the object in the collection id The database exists , Then update the record , There is no insert data 
boolean saveOrUpdateBatch(Collection<T> entityList);
// Batch modify insert , If the object in the collection id The database exists , Then update the record , There is no insert data , But set the number of inserts 
boolean saveOrUpdateBatch(Collection<T> entityList, int batchSize);
Example :
1.// Inquire about user Object's id Whether there is , There are update records , There is no insert data 
userService.saveOrUpdate(user);
2.// according to updateWrapper Query whether the data exists , Existential use T Object update , Insert if it doesn't exist 
UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
updateWrapper.eq("username", "lin");
userService.saveOrUpdate(user,updateWrapper);
3.// Batch modify insert , If in the collection User object id The database exists , Then update the record , There is no insert data 
List<User> list = new ArrrayList();
userService.saveOrUpdateBatch(list);
4.// Batch modify insert , If in the collection User object id The database exists , Then update the record , There is no insert data , But set the number of inserts 
List<User> list = new ArrrayList();
userService.saveOrUpdateBatch(list,10);

Parameter description

type Parameter name describe
T entity Entity object
Wrapper updateWrapper Entity objects encapsulate operation classes UpdateWrapper
Collection entityList Collection of entity objects
int batchSize Insert number of batches

2. Delete

remove Method
// according to entity Conditions , Delete record 
boolean remove(Wrapper<T> queryWrapper);
// according to ID Delete 
boolean removeById(Serializable id);
// according to columnMap Conditions , Delete eligible data 
boolean removeByMap(Map<String, Object> columnMap);
// Delete ( according to ID Batch deletion )
boolean removeByIds(Collection<? extends Serializable> idList);
Example :
1.// according to queryWrapper Conditional delete record 
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("username", "lin");
userService.remove(queryWrapper);
2.// according to user Object's id Delete record 
userService.removeById(userId);
3.// according to columnMap Conditions delete username and id Consistent data 
Map<String, Object> columnMap = new HashMap<>();
columnMap.put("username", user.getUsername());
columnMap.put("id", user.getId());
userService.removeByMap(columnMap);
4.// according to list Of id aggregate , Batch deletion 
List<Integer> list = new ArrayList<>;
userService.removeByIds(list);

Parameter description

type Parameter name describe
Wrapper queryWrapper Physical packaging class QueryWrapper
Serializable id Primary key ID
Map<String, Object> columnMap Table field map object
Collection<? extends Serializable> idList Primary key ID list

3. modify

Update Method
// according to updateWrapper Conditions , Use object T Update record 
boolean update(T entity, Wrapper<T> updateWrapper);
// according to object T Of ID Choose to modify 
boolean updateById(T entity);
// According to the objects in the object collection ID Batch update 
boolean updateBatchById(Collection<T> entityList);
// According to the objects in the object collection ID Batch update , But set the number of updates 
boolean updateBatchById(Collection<T> entityList, int batchSize);
1.// according to updateWrapper Conditions , Use object User Update record 
UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
updateWrapper.eq("username", "lin");
userService.update(user,updateWrapper);
2.// according to User Object's ID Update records 
userService.updateById(user);
3.// according to user Object collection for batch update records ( Use user Object's id to update )
List<User> list = new ArrayList<>;
userService.updateBatchById(list);
4.// according to user Object collection for batch update records ( Use user Object's id to update ), But set the number of updates 
List<User> list = new ArrayList<>;
userService.updateBatchById(list,10);

Parameter description

type Parameter name describe
Wrapper updateWrapper Entity objects encapsulate operation classes UpdateWrapper
T entity Entity object
Collection entityList Collection of entity objects
int batchSize Update batch number

4. Inquire about

get Method ( Query single record )
// according to ID Inquire about 
T getById(Serializable id);
// according to Wrapper, Look up a record . Result set , If it is more than one, it will throw an exception , Take one at random and add restrictions wrapper.last("LIMIT 1")
T getOne(Wrapper<T> queryWrapper);
// according to Wrapper, Look up a record ,throwEx If the parameter is true, Multiple result sets throw exceptions ,false Don't throw exceptions , The default is true
T getOne(Wrapper<T> queryWrapper, boolean throwEx);
Example :
1.// according to user Object's ID The query 
userService.getById(userId);
2.// according to Wrapper Conditions to query 
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("username", "lin");
userService.getOne(queryWrapper);
3.// according to Wrapper Conditions to query , add to throwEx Parameters 
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("username", "lin");
userService.getOne(queryWrapper,false);

Parameter description

type Parameter name describe
Serializable id Primary key ID
Wrapper queryWrapper Entity objects encapsulate operation classes QueryWrapper
boolean throwEx There are many. result Whether to throw an exception
T entity Entity object
list Method ( Query multiple records )
// Query all 
List<T> list();
// according to queryWrapper Conditions , Query list 
List<T> list(Wrapper<T> queryWrapper);
// Inquire about ( according to ID Batch query )
Collection<T> listByIds(Collection<? extends Serializable> idList);
// Inquire about ( according to columnMap Conditions )
Collection<T> listByMap(Map<String, Object> columnMap);
// Search all records 
List<Object> listObjs();
// according to Wrapper Conditions , Search all records 
List<Object> listObjs(Wrapper<T> queryWrapper);
Example :
1.// Query all user Table data 
userService.list();
2.// according to queryWrapper Conditions , Query list 
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("username", "lin");
userService.list(queryWrapper);
3.// According to the set ID Batch query 
List<Integer> list = new ArrayList<>;
userService.listByMap(list);
4.// Inquire about ( according to columnMap Conditions )
Map<String, Object> columnMap = new HashMap<>();
columnMap.put("username", user.getUsername());
columnMap.put("id", user.getId());
userService.listByMap(columnMap);
5.// Query all the data 
userService.listObjs();
6.// according to Wrapper Conditions , Search all records 
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("username", "lin");
userService.listObjs(queryWrapper);

Parameter description

type Parameter name describe
Wrapper queryWrapper Entity objects encapsulate operation classes QueryWrapper
Collection<? extends Serializable> idList Primary key ID list
Map<?String, Object> columnMap Table field map object
Page Method ( Paging query )
// Unconditional paging query 
IPage<T> page(IPage<T> page);
// Conditional paging query 
IPage<T> page(IPage<T> page, Wrapper<T> queryWrapper);
Example :
1.// Unconditional paging 
Page<User> page = new Page<>(page, pageSize);
userService.page(page);
2.// Conditional paging query 
Page<User> page = new Page<>(page, pageSize);
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("username", "lin");
userService.page(page,queryWrapper);

Parameter description

type Parameter name describe
IPage page Page turning object
Wrapper queryWrapper Entity objects encapsulate operation classes QueryWrapper
Count Method ( Total number of queries )
// Total number of records queried 
int count();
// according to Wrapper Conditions , Total number of records queried 
int count(Wrapper<T> queryWrapper);
Example :
1.// Inquire about user Table total records 
userService.count();
2.// Query the total number of records according to the criteria 
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("username", "lin");
userService.count(queryWrapper);

Parameter description

type Parameter name describe
Wrapper queryWrapper Entity objects encapsulate operation classes QueryWrapper

2. Call in the business layer ( service )

explain :

  • mybatis-plus Encapsulates the persistence layer (Mapper) Interface method , To call the business layer . Universal CRUD encapsulation BaseMapper Interface , by Mybatis-Plus At startup, the entity table is automatically parsed and the relation mapping is converted to Mybatis Internal object injection container
  • Generic T Is any entity object
  • Parameters Serializable For any type of primary key Mybatis-Plus It is not recommended to use composite primary key convention. Each table has its own unique id Primary key
  • object Wrapper by Conditional constructor
  • You need to add transactions manually
  • The calling method is baseMapper. Method name ()

1. Insert ( Store the data )

insert Method
// Insert a record 
int insert(T entity);
Example :
1.// Insert User Object data to database 
baseMapper.insert(user);

Parameter description

type Parameter name describe
T entity Entity object

2. Delete

delete Method
// according to entity Conditions , Delete record 
int delete(@Param(Constants.WRAPPER) Wrapper<T> wrapper);
// Delete ( according to ID Batch deletion )
int deleteBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);
// according to ID Delete 
int deleteById(Serializable id);
// according to columnMap Conditions , Delete record 
int deleteByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);
Example :
1.// according to wrapper Conditional delete record 
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("username", "lin");
baseMapper.delete(@Param(queryWrapper);
2.// according to user Of id Set batch delete records 
List<Integer> list = new ArrayList<>;
baseMapper.deleteBatchIds(list);
3.// according to user Of id Delete record 
baseMapper.deleteById(userId);
4.// according to columnMap Conditions , Delete record 
Map<String, Object> columnMap = new HashMap<>();
columnMap.put("username", user.getUsername());
columnMap.put("id", user.getId());
baseMapper.deleteByMap(columnMap);

Parameter description

type Parameter name describe
Wrapper wrapper Entity objects encapsulate operation classes ( It can be for null)
Collection<? extends Serializable> idList Primary key ID list ( Not for null as well as empty)
Serializable id Primary key ID
Map<String, Object> columnMap Table field map object

3. modify

Update Method
// according to whereEntity Conditions , Update record 
int update(@Param(Constants.ENTITY) T entity, @Param(Constants.WRAPPER) Wrapper<T> updateWrapper);
// according to ID modify 
int updateById(@Param(Constants.ENTITY) T entity);
Example :
1.// Update records according to conditions 
UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
updateWrapper.eq("username", "lin");
baseMapper.update(user,updateWrapper);
2.// according to user Of ID Make changes 
baseMapper.updateById(user);

Parameter description

type Parameter name describe
T entity Entity object (set Conditional value , for null)
Wrapper updateWrapper Entity objects encapsulate operation classes ( It can be for null, Inside entity Used to generate where sentence )

4. Inquire about

select Method
// according to ID Inquire about 
T selectById(Serializable id);
// according to entity Conditions , Look up a record 
T selectOne(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
// Inquire about ( according to ID Batch query )
List<T> selectBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);
// according to entity Conditions , Search all records 
List<T> selectList(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
// Inquire about ( according to columnMap Conditions )
List<T> selectByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);
// according to Wrapper Conditions , Search all records 
List<Map<String, Object>> selectMaps(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
// according to Wrapper Conditions , Search all records . Be careful : Just return the value of the first field 
List<Object> selectObjs(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
// according to entity Conditions , Search all records ( And turn the page )
IPage<T> selectPage(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
// according to Wrapper Conditions , Search all records ( And turn the page )
IPage<Map<String, Object>> selectMapsPage(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
// according to Wrapper Conditions , Total number of records queried 
Integer selectCount(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
Example :
1.// according to user Of id Query a single record 
baseMapper.selectById(userId);
2.// Query a single record according to conditions 
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("username", "lin");
baseMapper.selectOne(queryWrapper);
3.// according to map Query multiple records by criteria 
Map<String, Object> map = new HashMap<>();
map.put("username", user.getUsername());
map.put("id", user.getId());
baseMapper.selectByMap(map);
4.

Parameter description

type Parameter name describe
Serializable id Primary key ID
Wrapper queryWrapper Entity objects encapsulate operation classes ( It can be for null)
Collection<? extends Serializable> idList Primary key ID list ( Not for null as well as empty)
Map<String, Object> columnMap Table field map object
IPage page Paging query criteria ( It can be for RowBounds.DEFAULT)
copyright:author[Prodigal son Tang Shao],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/02/202202130729382421.html