This is the 13th day of my participation in the August More text Challenge. For details, see: August More Text Challenge

1. The premise

Combined with the previous article, a series of code generated by the code generator

1. Entity

@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
public class TEvaluation implements Serializable {
Copy the code

2. The Controller layer

@RestController
@RequestMapping("/t-evaluation")
public class TEvaluationController {}
Copy the code

3. The Service interface layer

public interface ITEvaluationService extends IService<TEvaluation> { }
Copy the code

The implementation layer of service

@Service
public class TEvaluationServiceImpl extends ServiceImpl<TEvaluationMapper, TEvaluation> implements ITEvaluationService {}
Copy the code

4.Mapper (DAO) layer

public interface TEvaluationMapper extends BaseMapper<TEvaluation> {}
Copy the code

Today’s focus is on sharing the Service CRUD interface and Mapper CRUD interface combined with the second and third points

Service CRUD

1. The Service IService inheritance

2. The implementation layer of service inherits the Iservice interface implementation class

Note: ServiceImpl<M extends BaseMapper, T> : Parameter description M is a Mapper object and T is an entity

  • Add the main share of batch preservation
boolean saveBatch(Collection<T> entityList);
boolean saveBatch(Collection<T> entityList, int batchSize);
Copy the code

Where: Collection Entity object Collection; It is obvious that this batch operation is a transaction operation, and SqlSession is the core interface of Mybatis (which also indicates that Mybatis plus knowledge is enhanced on the basis of Mybatis).

  • Modify insert mainly wants to share single record inserts

When a single record is inserted, the final result returned with the code:

return StringUtils.checkValNull(idVal) || Objects.isNull(getById((Serializable) idVal)) ? save(entity) : updateById(entity);
Copy the code

GetById method:

Check whether the incoming object is empty or exists in the incoming object database. If true, save the object. If false, update the object according to its ID

Note Serializable as an argument to the method: Using Java polymorphism, Serializable defines the type of the ID, which is the interface type to use. The actual call to this method can be passed Integer,Long,String, or a class that implements the Serializable interface. The Java package type (Integer, Long, String, Double…). Almost all implement this interface.

Batch modify insert source code, as a single insert is basically the same only loop traversal

  • Query delete update operation can read the source code, about batch update and batch insert principle is similar
  • Paging query condition Paging query
@Override
public IPage<Map<String, Object>> pageMaps(IPage<T> page, Wrapper<T> queryWrapper) {
    return baseMapper.selectMapsPage(page, queryWrapper);
}
Copy the code

Page Page turning object; QueryWrapper entity object encapsulating operation class queryWrapper

Mapper CRUD

Mapper inherits BaseMapper interface, no need to write mapper. XML file, you can get CRUD function, equivalent to Mapper layer some basic add and delete the difference do not have to write themselves, bytes to call, and some specific operations still need to manually write, then call

  • insert
int insert(T entity);
Copy the code
  • delete

Delete the columnMap based on the ID (batch), and delete the columnMap based on the entity condition

  • update

Update based on ID or whereWrapper condition

  • select

Query by ID (batch query); Query one (or all) records according to various conditions; Paging query; Querying the total number of records

conclusion

Today I mainly share some interface methods of Service CRUD and Mapper CRUD, see some part of the interface source code, sometimes source code for us to understand is also very helpful, for Service CRUD, can be in the implementation layer of Service, directly call method: Such as this.save(T), but if there are some operations that need to be operated through the database, you can try baseMapper. Call some of the CRUD methods that come with Mapper or your own custom methods.

Through these two operations, features in MyBatis – Plus: powerful CRUD operations, built-in universal Mapper, universal Service, only through a small amount of configuration can realize most of the single table CRUD operations, more powerful conditional constructor, to meet all kinds of use requirements. This feature is also obvious after looking at the share above, and I’ll share more about the powerful conditional constructor in the next post; Mybatis – Plus also has built-in paging plugin: Based on myBatis physical paging, developers do not need to care about the specific operation, after configuring the plugin, write paging is equivalent to normal List query