Mybatis的update方法

Update方法

更新某条记录

public void recycleAccount(Long Id) {
    Account account = new Account();
    account.setId(Id);
    account.setAccountStatus(RecordStatusEnum.VALID.getValue());
    accountService.update(account);
}

updateByPrimaryKeySelective方法

只是更新实体model中,不为空的字段

public void lockAccount(Long Id) {
    Account account = new Account();
    account.setId(Id);
    account.setAccountStatus(RecordStatusEnum.LOCKED.getValue());
    accountService.updateByPrimaryKeySelective(account);
}

updateByExample方法

更新实体所有的字段

如果,example定义了两个字段,数据库共4个字段
则修改数据库的两个字段,其余两个字段改为null

private void makedAssayOrderSample(String sampleNo) {
    AssayOrderSampleExample assayOrderSampleExample = new AssayOrderSampleExample();
    AssayOrderSampleExample.Criteria criteria1 = assayOrderSampleExample.createCriteria();
    criteria1.andRecordStatusEqualTo(RecordStatusEnum.VALID.getValue());
    criteria1.andSampleNoEqualTo(sampleNo);
    AssayOrderSample assayOrderSample = new AssayOrderSample();
    assayOrderSample.setStatusFlag(AssayTaskStatusEnum.SAMPLE_PREPARATION_FINISHED.getValue());
    assayOrderSampleService.updateByExample(assayOrderSample, assayOrderSampleExample);
}

updateByExampleSelective方法

选择性更新实体中的字段

如果,example定义了两个字段,数据库共4个字段
则修改数据库的两个字段,其余两个字段不动

public void deleteSelfAndChild(TestObjectCategoryTree tree) {

    TestObjectCategoryExample testObjectCategoryExample = new TestObjectCategoryExample();
    TestObjectCategoryExample.Criteria criteria1 = testObjectCategoryExample.createCriteria();
    criteria1.andIdEqualTo(tree.getId());
    TestObjectCategoryExample.Criteria criteria2 = testObjectCategoryExample.createCriteria();
    criteria2.andParentIdsLike(tree.makeSelfAsNewParentIds() + "%");
    testObjectCategoryExample.or(criteria2);

    TestObjectCategory testObjectCategory = new TestObjectCategory();
    testObjectCategory.setModifyTime(new Date());
    testObjectCategory.setRecordStatus(RecordStatusEnum.DESTROYED.getValue());

    testObjectCategoryService.updateByExampleSelective(testObjectCategory, testObjectCategoryExample);
}

你可能感兴趣的:(————Mybatis)