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