×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: ture ture
Added: Apr 8, 2018 10:32 PM
Views: 6
Tags: no tags
  1. Creating Service Layer
  2. Create a UserService Interface under package springmvc_example.service package and write the following code in it
  3.  
  4. package springmvc_example.service;
  5.  
  6. import java.util.List;
  7.  
  8. import springmvc_example.model.User;
  9.  
  10. public interface UserService {
  11.  
  12.  public List getListUser();
  13.  
  14.  public void saveOrUpdate(User user);
  15.  
  16.  public void deleteUser(int id);
  17.  
  18.  public User findUserById(int id);
  19.  
  20. }
  21.  
  22. Create a UserServiceImpl class implements UserService Interface under springmvc_example.service package and write the following code in it
  23.  
  24. import java.util.List;
  25.  
  26. import org.springframework.beans.factory.annotation.Autowired;
  27. import org.springframework.stereotype.Service;
  28. import org.springframework.transaction.annotation.Transactional;
  29.  
  30. import springmvc_example.dao.UserDao;
  31. import springmvc_example.model.User;
  32.  
  33. @Service
  34. @Transactional
  35. public class UserServiceImpl implements UserService {
  36.  
  37.  UserDao userDao;
  38.  
  39.  @Autowired
  40.  public void setUserDao(UserDao userDao) {
  41.   this.userDao = userDao;
  42.  }
  43.  
  44.  public List getListUser() {
  45.   return userDao.getListUser();
  46.  }
  47.  
  48.  public void saveOrUpdate(User user) {
  49.   userDao.saveOrUpdate(user);
  50.  }
  51.  
  52.  public void deleteUser(int id) {
  53.   userDao.deleteUser(id);
  54.  }
  55.  
  56.  public User findUserById(int id) {
  57.   return userDao.findUserById(id);
  58.  }
  59.  
  60. }