!252 完善seata-test

Merge pull request !252 from life/main
This commit is contained in:
Michael Yang 2023-08-08 10:10:41 +00:00 committed by Gitee
commit 02b7997d16
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F

View File

@ -10,6 +10,11 @@ import com.mybatisfle.test.mybatisflexspringbootseata.mapper.OrderTblMapper;
import com.mybatisfle.test.mybatisflexspringbootseata.mapper.StockTblMapper;
import com.mybatisflex.core.datasource.DataSourceKey;
import com.mybatisflex.core.query.QueryWrapper;
import io.seata.core.context.RootContext;
import io.seata.spring.annotation.GlobalTransactional;
import org.mybatis.logging.Logger;
import org.mybatis.logging.LoggerFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@ -17,6 +22,8 @@ import org.springframework.transaction.annotation.Transactional;
@Service
public class TestService {
private static final Logger LOGGER = LoggerFactory.getLogger(TestService.class);
@Autowired
AccountTblMapper accountTblMapper;
@ -27,27 +34,29 @@ public class TestService {
StockTblMapper stockTblMapper;
// @Transactional
@GlobalTransactional
public boolean buy() {
// DataSourceKey.use("accountdb");
LOGGER.warn(() -> "xid:"+RootContext.getXID());
QueryWrapper account =new QueryWrapper();
account.where(AccountTblTableDef.ACCOUNT_TBL.USER_ID.eq("1001"));
AccountTbl accountTbl = accountTblMapper.selectOneByQuery(account);
accountTbl.setMoney(accountTbl.getMoney() - 5);
accountTblMapper.update(accountTbl);
// DataSourceKey.use("stockdb");
// QueryWrapper stock = new QueryWrapper();
// stock.where("id=1");
// StockTbl stockTbl = stockTblMapper.selectOneByQuery(stock);
// stockTbl.setCount(stockTbl.getCount() - 1);
// stockTblMapper.update(stockTbl);
// DataSourceKey.use("orderdb");
// OrderTbl orderTbl = new OrderTbl();
// orderTbl.setCount(5);
// orderTbl.setMoney(5);
// orderTbl.setUserId(accountTbl.getUserId());
// orderTbl.setCount(1);
// orderTbl.setCommodityCode(stockTbl.getCommodityCode());
// orderTblMapper.insert(orderTbl);
DataSourceKey.use("stockdb");
QueryWrapper stock = new QueryWrapper();
stock.where("id=1");
StockTbl stockTbl = stockTblMapper.selectOneByQuery(stock);
stockTbl.setCount(stockTbl.getCount() - 1);
stockTblMapper.update(stockTbl);
DataSourceKey.use("orderdb");
OrderTbl orderTbl = new OrderTbl();
orderTbl.setCount(5);
orderTbl.setMoney(5);
orderTbl.setUserId(accountTbl.getUserId());
orderTbl.setCount(1);
orderTbl.setCommodityCode(stockTbl.getCommodityCode());
orderTblMapper.insert(orderTbl);
return true;
}
}