更新时间:2022-10-23 23:31:27
Your service, as it stands, is simply delegating everything to the underlying DAO. This may be desired sometimes, but typically I put "business logic" in the service layer. Putting logic in the service layer will help keep your controller layer pretty light too.
A service can use one or more DAOs to accomplish the task it needs. So consider a simple bank system, where I have a AccountDao
public class AccountDao implements GenericDao<Account, Long> {
// your save, insert, delete, find, etc
}
Then in my service, I would put "makePayment" or something
@Service
public class AccountService {
@Autowired
private AccountDao dao;
@Transactional
public void makePayment(Long fromId, Long toId, double amount) {
Account from = dao.find(fromId);
from.withdrawl(amount);
Account to = dao.find(toId);
to.deposit(amount);
dao.save(from);
dao.save(to);
}
}
Use transactions on your service layer, to give you more control over which operations need to be in the same transaction.