@@ -525,38 +525,42 @@ protected <I> Result<I> createCallable(Result<I> result, T record, Object option
525525 * @param optionParam 可选参数,默认为 {@code null }
526526 * @return 返回执行结果,默认返回的是 {@code result } 参数,可以被子类覆盖重写
527527 */
528- @ SuppressWarnings ("unchecked" )
529528 protected <I > Result <I > create (Result <I > result , T record , Object optionParam ) {
530529 boolean ifExist = checkRecordIfExist4Create (result , record );
531530 if (!ifExist && result .isSuccess ()) {
532531 setValue4Create (record , optionParam );
533532 boolean flag = checkDBResult (crudMapper .insert (record ));
534533 if (flag ) {
535- Set <TableColumnInfo > primaryKeyColumns = tableInfo .getPrimaryKeyColumns ();
536- int size = primaryKeyColumns .size ();
537- if (size == 0 ) {
538- // 忽略没有主键字段的情况
539- return result ;
540- }
541- Object value = (size == 1 ? null : new Object [size ]);
542- int idx = 0 ;
543- for (TableColumnInfo columnInfo : primaryKeyColumns ) {
544- I tmp = BeanUtil .methodInvoke (columnInfo .getPropertyDescriptor ().getReadMethod (), record );
545- if (size > 1 ) {
546- // 如果有多个主键字段,使用数组返回
547- ((Object []) value )[idx ++] = tmp ;
548- } else {
549- value = tmp ;
550- }
551- }
552- result .setValue ((I ) value );
534+ return getPrimaryKeyValue (record , result );
553535 } else {
554536 result .setSuccess (false ).setErrorCode (MybatisConstants .INSERT_DB_FAILED ).setErrorMsg ("插入失败,请检查" );
555537 }
556538 }
557539 return result ;
558540 }
559541
542+ @ SuppressWarnings ("unchecked" )
543+ protected <I > Result <I > getPrimaryKeyValue (T record , Result <I > result ) {
544+ Set <TableColumnInfo > primaryKeyColumns = tableInfo .getPrimaryKeyColumns ();
545+ int size = primaryKeyColumns .size ();
546+ if (size == 0 ) {
547+ // 忽略没有主键字段的情况
548+ return result ;
549+ }
550+ Object value = (size == 1 ? null : new Object [size ]);
551+ int idx = 0 ;
552+ for (TableColumnInfo columnInfo : primaryKeyColumns ) {
553+ I tmp = BeanUtil .methodInvoke (columnInfo .getPropertyDescriptor ().getReadMethod (), record );
554+ if (size > 1 ) {
555+ // 如果有多个主键字段,使用数组返回
556+ ((Object []) value )[idx ++] = tmp ;
557+ } else {
558+ value = tmp ;
559+ }
560+ }
561+ return result .setValue ((I ) value );
562+ }
563+
560564 @ Transactional (rollbackFor = Exception .class )
561565 @ Override
562566 public Result <Boolean > createBatch (List <T > records , Object optionParam ) {
@@ -619,6 +623,71 @@ protected Result<Boolean> createBatch(Result<Boolean> result, List<T> records, O
619623 return result .setValue (true );
620624 }
621625
626+ @ Transactional (rollbackFor = Exception .class )
627+ @ Override
628+ public <I > Result <I > save (T record ) {
629+ return save (record , null );
630+ }
631+
632+ @ Transactional (rollbackFor = Exception .class )
633+ @ Override
634+ public <I > Result <I > save (T record , Object optionParam ) {
635+ Result <I > result = new Result <>();
636+ boolean validate = createValidate (result , record , optionParam );
637+ if (!validate ) {
638+ return result ;
639+ }
640+ return createCallable (result , record , optionParam , () -> save (result , record , optionParam ));
641+ }
642+
643+ /**
644+ * 保存新的记录,{@link #create(Object)} 方法的最后一步调用
645+ *
646+ * @param result 保存的结果
647+ * @param record 待保存的实体对象
648+ * @param <I> 主键类型
649+ * @param optionParam 可选参数,默认为 {@code null }
650+ * @return 返回执行结果,默认返回的是 {@code result } 参数,可以被子类覆盖重写
651+ */
652+ protected <I > Result <I > save (Result <I > result , T record , Object optionParam ) {
653+ List <T > exists = findExistRecord4CheckRecord (result , record );
654+ if (CollectionUtils .isEmpty (exists )) {
655+ setValue4Create (record , optionParam );
656+ boolean flag = checkDBResult (crudMapper .insert (record ));
657+ if (flag ) {
658+ return getPrimaryKeyValue (record , result );
659+ } else {
660+ return result .setSuccess (false ).setErrorCode (MybatisConstants .INSERT_DB_FAILED ).setErrorMsg ("插入失败,请检查" );
661+ }
662+ }
663+ return getPrimaryKeyValue (exists .get (0 ), result );
664+ }
665+
666+ @ Transactional (rollbackFor = Exception .class )
667+ @ Override
668+ public <I > Result <I > saveOrUpdate (T record ) {
669+ return saveOrUpdate (record , null );
670+ }
671+
672+ @ Transactional (rollbackFor = Exception .class )
673+ @ SuppressWarnings ("unchecked" )
674+ @ Override
675+ public <I > Result <I > saveOrUpdate (T record , Object optionParam ) {
676+ Result <I > result = new Result <>();
677+ Result <I > primaryKeyValue = getPrimaryKeyValue (record , result );
678+ if (primaryKeyValue .getValue () == null ) {
679+ create (record , optionParam );
680+ } else {
681+ Result <Boolean > updateResult = updateByPrimaryKey (record , optionParam );
682+ result .setSuccess (updateResult .isSuccess ())
683+ .setErrorCode (updateResult .getErrorCode ())
684+ .setErrorMsg (updateResult .getErrorMsg ())
685+ .setValue ((I ) primaryKeyValue )
686+ .setExtraInfo (updateResult .getExtraInfo ());
687+ }
688+ return result ;
689+ }
690+
622691 @ Transactional (rollbackFor = Exception .class )
623692 @ Override
624693 public Result <Boolean > updateByPrimaryKey (T record , Object optionParam ) {
0 commit comments