public class GalaxyCalAppSlotsRepo { @Autowired GalaxyCalAppSlotsDAO galaxyCalAppSlotsDAO; public void insertAppSlotsRecord(String appCode,String executeType, int slotCount) { GalaxyCalAppSlotsDO galaxyCalAppSlotsDO = new GalaxyCalAppSlotsDO(); galaxyCalAppSlotsDO.setAppCode(appCode); galaxyCalAppSlotsDO.setExecuteType(executeType); galaxyCalAppSlotsDO.setSlotsCount(slotCount); galaxyCalAppSlotsDAO.insertSelective(galaxyCalAppSlotsDO); } }以上方法如何在public interface ResourceFacade { BaseResult<Boolean> setGalaxyCalAppSlots(); }方法去调用它,如果此appCode已经存在抛异常告知已经存在,并对slotCount设置一个初始值,slotCount也可以扩大跟缩小java代码如何实现
时间: 2024-03-29 11:40:11 浏览: 13
可以先在ResourceFacade中注入GalaxyCalAppSlotsRepo,在setGalaxyCalAppSlots方法中调用insertAppSlotsRecord方法,并在方法中进行appCode是否存在的判断,如果存在则抛出异常,否则设置初始值并插入到数据库中。代码如下:
```java
public interface ResourceFacade {
BaseResult<Boolean> setGalaxyCalAppSlots(String appCode, String executeType, int slotCount);
}
@Service
public class ResourceFacadeImpl implements ResourceFacade {
@Autowired
private GalaxyCalAppSlotsRepo galaxyCalAppSlotsRepo;
@Override
public BaseResult<Boolean> setGalaxyCalAppSlots(String appCode, String executeType, int slotCount) {
try {
// 判断appCode是否存在
GalaxyCalAppSlotsDO galaxyCalAppSlotsDO = galaxyCalAppSlotsRepo.selectByAppCode(appCode);
if (galaxyCalAppSlotsDO != null) {
throw new RuntimeException("appCode已存在");
}
// 设置初始值并插入数据库
galaxyCalAppSlotsRepo.insertAppSlotsRecord(appCode, executeType, slotCount);
} catch (Exception e) {
return new BaseResult<>(BaseResult.FAILURE, e.getMessage(), false);
}
return new BaseResult<>(BaseResult.SUCCESS, "插入成功", true);
}
}
@Repository
public interface GalaxyCalAppSlotsDAO {
int insertSelective(GalaxyCalAppSlotsDO record);
GalaxyCalAppSlotsDO selectByAppCode(String appCode);
}
@Service
public class GalaxyCalAppSlotsRepo {
@Autowired
private GalaxyCalAppSlotsDAO galaxyCalAppSlotsDAO;
public void insertAppSlotsRecord(String appCode, String executeType, int slotCount) {
GalaxyCalAppSlotsDO galaxyCalAppSlotsDO = new GalaxyCalAppSlotsDO();
galaxyCalAppSlotsDO.setAppCode(appCode);
galaxyCalAppSlotsDO.setExecuteType(executeType);
galaxyCalAppSlotsDO.setSlotsCount(slotCount);
galaxyCalAppSlotsDAO.insertSelective(galaxyCalAppSlotsDO);
}
}
```
阅读全文