spring boot 第三节 三层架构

目标:重点是弄懂     @Autowired

public interface IUserDao {
	
	void save(User user);
	
	void remove(User user);
	
	void update(User user);
	
	User loadById(Long id);
	
	List loadAll();
	
	
}

public class  UserDaoImpl implements IUserDao {

	@Override
	public void save(User user) {
		System.out.println("kaishi ganhuo");
		
	}

	@Override
	public void remove(User user) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void update(User user) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public User loadById(Long id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List loadAll() {
		// TODO Auto-generated method stub
		return null;
	}
	
public interface IUserService {

	//添加一个对象
	void add(User user);

	//删除一个对象
	void delete(Long id);

	//更新对象
	void update(User user);

	//通过Id获取一个对象
	User getById(Long id);

	//加获取所有的对象
	List getAll();
}
@Service
public class UserServiceImpl implements IUserService {
	
	@Autowired
	private IUserDao UserDao;

	@Override
	public void add(User user) {		
		UserDao.save(user);				
	}

	@Override
	public void delete(Long id) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void update(User user) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public User getById(Long id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List getAll() {
		// TODO Auto-generated method stub
		return null;
	}
	
	
}

最后control层

@RestController
@RequestMapping("/user")
public class UserController {

	@Autowired
	private IUserService userService;

	@RequestMapping("/add")
	public Map add(User user) {
		Map result = new HashMap<>();
		try {
			userService.add(user);
			result.put("success", true);
			result.put("message", "操作成功!");
			return result;
		} catch (Exception e) {

			e.printStackTrace();
			result.put("success", false);
			result.put("message", "操作失败!");
			return result;
		}
	}
}

 

你可能感兴趣的:(spring,boot,spring,boot)