Spring Boot passed directly executed sql statement, sql same time check, after the successful implementation of the rollback manually set

When performing Sql statement, the statement as a whole Sql parameter performed, using the annotation layer dao manner using the following:

@Select("${sql}")
List<LinkedHashMap<String,Object>> checkSql(@Param("sql") String sql);

Sql is correct at the time of verification, to be verified by the implementation of sql, when executed successfully, sql correct, when executed sql fails, the results are abnormal. When sql executed successfully, sometimes data will be affected, such as updates, inserts, deletes, at this time, we need to roll back.

Rollback way @Transactional added annotation in the method, the following examples:

@ApiOperation("校验SQL")
@PostMapping(value = "/checkSql")
@Transactional
public HttpResult checkSql(String sql){
    try{
       List<LinkedHashMap<String,Object>> result=dataService.checkSql(sql);
        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
    } catch (Exception e){
       return HttpResult.build(false,e.getMessage().split("\r\n"));
    }
    return HttpResult.build(true);
}

Because this section of code in the function is executed successfully rolled back, so try to roll back the statement written on the inside.

Published 36 original articles · won praise 19 · views 30000 +

Guess you like

Origin blog.csdn.net/qq_27182767/article/details/103786639