没啥要点,上代码:
@Modifying
@Query(value = " insert into Xxx(id) values (:#{#bo.id}) ", nativeQuery = true)
int saveXxx(@Param("bo") Xxx bo);
使用编程式事务:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.interceptor.TransactionTemplate;
@Configuration
@EnableTransactionManagement
public class AppConfig {
@Bean
public TransactionTemplate transactionTemplate(PlatformTransactionManager transactionManager) {
return new TransactionTemplate(transactionManager);
}
}
具体使用:
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;
import org.springframework.stereotype.Service;
@Service
public class SomeService {
@Resource
private TransactionTemplate transactionTemplate;
public void performTransaction() {
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
// 在这里编写需要在事务中执行的代码
// 例如:保存一个实体到数据库
// YourEntity entity = new YourEntity();
// entityManager.persist(entity);
}
});
transactionTemplate.execute(new TransactionCallback() {
@Override
protected void doInTransaction(TransactionStatus status) {
// 在这里编写需要在事务中执行的代码
// 例如:保存一个实体到数据库
// YourEntity entity = new YourEntity();
// entityManager.persist(entity);
}
});
}
}
@Modifying
@Query(value = " delete from 表 where 字段1=?1 and 字段2=?2 and 字段3=?3 ", nativeQuery = true)
void deleteXxx(String billId, String billCode, Integer tenantId);
@Modifying
@Query(value = " update 表 set id=:newId where id=:id and id=ParentID ", nativeQuery = true)
int uptXxx(@Param("newId") String newId, @Param("id")String id);