⭐ In August, the second day of the challenge ⭐, MybatisPlus learning, welcome friends to learn 😁
Code Mantis shrimp is a sand sculpture and interesting young boy, like most friends like listening to music, games, of course, in addition to the interest of writing, Emm… The day is still very long, let us walk together 🌈
Welcome friends to pay attention to my public number: JavaCodes, although the name with Java, but the scope of more than Java field oh 😁, look forward to your attention ❤
The original link⭐MybatisPlus learning ⭐ (2) CRUD full set of detailed explanation
Look at the MybatisPlus module for this link
1. Mapper CRUD interface
Description:
- The universal CRUD encapsulates the BaseMapper interface for Mybatis-Plus to automatically parse the entity table relational mapping when started into Mybatis internal object injection container
- The generic T is any entity object
- Mybatis-Plus is not recommended to use the compound primary key convention. Each table has its own unique primary key id
- Object Wrapper is a conditional constructor
1.1, the Insert
// Insert a record
int insert(T entity);
Copy the code
== Parameter Description ==
type | Parameter names | describe |
---|---|---|
T | entity | Entity objects |
Employee employee = new Employee();
employee.setLastName("Mantis shrimp");
employee.setAge(21);
employee.setEmail("[email protected]");
employee.setGender(1);
employeeMapper.insert(employee);
Copy the code
1.2, Delete
// Delete the record based on the entity condition
int delete(@Param(Constants.WRAPPER) Wrapper<T> wrapper);
// Delete (batch delete based on ID)
int deleteBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);
// Delete based on ID
int deleteById(Serializable id);
Copy the code
== Parameter Description ==
type | Parameter names | describe |
---|---|---|
Wrapper | wrapper | Entity object encapsulates operation class (can be null) |
Collection<? extends Serializable> | idList | List of primary key ids (not null or EMPTY) |
Serializable | id | The primary key ID |
Map<String, Object> | columnMap | Table field map object |
1.3, the Update
// Modify according to the ID
int updateById(@Param(Constants.ENTITY) T entity);
Copy the code
== Parameter Description ==
type | Parameter names | describe |
---|---|---|
T | entity | Entity object (set conditional value, can be null) |
Wrapper | updateWrapper | Entity object encapsulates operation class (can be null, where entity is used to generate WHERE statement) |
1.4, the Select
// Query by ID
T selectById(Serializable id);
// Query a record based on the entity condition
T selectOne(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
// Query (batch query based on ID)
List<T> selectBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);
// Query all records based on the entity condition
List<T> selectList(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
// Query the total number of records according to the Wrapper condition
Integer selectCount(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
Copy the code
== Parameter Description ==
type | Parameter names | describe |
---|---|---|
Serializable | id | The primary key ID |
Wrapper | queryWrapper | Entity object encapsulates operation class (can be null) |
Collection<? extends Serializable> | idList | List of primary key ids (not null or EMPTY) |
Map<String, Object> | columnMap | Table field map object |
IPage | page | Paging query criteria (can be rowbound.default) |
2. Service CRUD interface
= = = = :
- Use Service CRUD to encapsulate IService interface, and further encapsulate CRUD. Use GET query single row remove delete list query set Page paging prefix to distinguish Mapper layer to avoid confusion.
- The generic T is any entity object
- If custom generic Service methods are possible, create your own IBaseService that inherits from the base class provided by Mybatis Plus
- Object Wrapper is a conditional constructor
== Create Service and implementation class ==
EmployeeService
public interface EmployeeService extends IService<Employee> {}Copy the code
EmployeeImpl
@Service
public class EmployeeImpl extends ServiceImpl<EmployeeMapper.Employee> implements EmployeeService {}Copy the code
2.1, Save
// Insert a record (select field, policy insert)
boolean save(T entity);
// Insert (batch)
boolean saveBatch(Collection<T> entityList);
Copy the code
== Parameter Description ==
type | Parameter names | describe |
---|---|---|
T | entity | Entity objects |
Collection | entityList | Entity object collection |
int | batchSize | Number of insert batches |
2.2, SaveOrUpdate
// Insert a new record in TableId
boolean saveOrUpdate(T entity);
// Batch modify inserts
boolean saveOrUpdateBatch(Collection<T> entityList);
Copy the code
== Parameter Description ==
type | Parameter names | describe |
---|---|---|
T | entity | Entity objects |
Wrapper | updateWrapper | Entity object wrapper operation class UpdateWrapper |
Collection | entityList | Entity object collection |
int | batchSize | Number of insert batches |
2.3, Remove
// Delete the record based on the entity condition
boolean remove(Wrapper<T> queryWrapper);
// Delete based on ID
boolean removeById(Serializable id);
// Delete (batch delete based on ID)
boolean removeByIds(Collection<? extends Serializable> idList);
Copy the code
== Parameter Description ==
type | Parameter names | describe |
---|---|---|
Wrapper | queryWrapper | Physical wrapper class QueryWrapper |
Serializable | id | The primary key ID |
Map<String, Object> | columnMap | Table field map object |
Collection<? extends Serializable> | idList | List of primary key ids |
2.4, the Update
// Select modify according to the ID
boolean updateById(T entity);
// Batch update based on ID
boolean updateBatchById(Collection<T> entityList);
Copy the code
== Parameter Description ==
type | Parameter names | describe |
---|---|---|
Wrapper | queryWrapper | Physical wrapper class QueryWrapper |
T | entity | Entity objects |
Collection | entityList | Entity object collection |
int | batchSize | Update batch number |
2.5, the Get
// Query by ID
T getById(Serializable id);
// Query a record from the Wrapper
T getOne(Wrapper<T> queryWrapper, boolean throwEx);
Copy the code
== Parameter Description ==
type | Parameter names | describe |
---|---|---|
Serializable | id | Primary key ID of the physical wrapper class |
T | entity | Entity objects |
Collection | entityList | Entity object collection |
Function<? super Object, V> | mapper | The transition function |
Wrapper | queryWrapper | Entity object wrapper operation class QueryWrapper |
2.6, the List
// Query all
List<T> list(a);
// Query the list
List<T> list(Wrapper<T> queryWrapper);
// Query (batch query based on ID)
Collection<T> listByIds(Collection<? extends Serializable> idList);
Copy the code
== Parameter Description ==
type | Parameter names | describe |
---|---|---|
Collection<? extends Serializable> | idList | List of primary key ids |
Map<? String, Object> | columnMap | Table field map object |
Collection | entityList | Entity object collection |
Function<? super Object, V> | mapper | The transition function |
Wrapper | queryWrapper | Entity object wrapper operation class QueryWrapper |
2.7, the Count
// Query the total number of records
int count(a);
// Query the total number of records according to the Wrapper condition
int count(Wrapper<T> queryWrapper);
Copy the code
== Parameter Description ==
type | Parameter names | describe |
---|---|---|
Wrapper | queryWrapper | Entity object wrapper operation class QueryWrapper |
The last
I am aCode pipi shrimpI am a mantis shrimp lover who loves to share knowledge. I will keep updating my blog in the future. I look forward to your attention!!
Creation is not easy, if this blog is helpful to you, I hope you can == a key three! ==, thanks for your support, see you next time ~~~
Share the outline
Big factory interview questions column
PC crawler column
App Crawler Column