- 在controller同级目录下创建service
package com.spring2.service;
import com.spring2.entity.User;
import com.spring2.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public int save(User user){
if(user.getId() == null){
//有id 新增
return userMapper.insert(user);
}else{
return userMapper.update(user);
}
}
}
- 在controller里出入service