中软国际实习第四天
ssm框架登陆效果—User增删改查
User
public class User {
public User(){
}
public User(int id, String username,String password){
this.id = id;
this.username = username;
this.password = password;
}
public User(String username,String password){
this.username = username;
this.password = password;
}
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
UserController
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private IUserService userService;
@RequestMapping("/login.do")
public ModelAndView login(User user){
boolean flag=userService.login(user.getUsername(),user.getPassword());
ModelAndView modelAndView = new ModelAndView();
if(flag){
modelAndView.setViewName("main");
System.out.println("success");
}else {
modelAndView.setViewName("../failer");
}
return modelAndView;
}
@RequestMapping("/findAll.do")
public ModelAndView findAll(){
List<User> userList=userService.findAll();
ModelAndView modelAndView=new ModelAndView();
modelAndView.addObject("userList",userList);
modelAndView.setViewName("user-list");
return modelAndView;
}
@RequestMapping("/deleteById.do")
public String delete(int id){
userService.deleteById(id);
return "redirect:findAll.do";
}
@RequestMapping("/add.do")
public String add(User user){
userService.add(user);
return "redirect:findAll.do";
}
@RequestMapping("/toUpdate.do")
public ModelAndView toUpdate(int id){
User user=userService.selectUserById(id);
ModelAndView modelAndView=new ModelAndView();
modelAndView.setViewName("user-update");
modelAndView.addObject("user",user);
return modelAndView;
}
@RequestMapping("/update.do")
public String update(User user){
userService.update(user);
return "redirect:findAll.do";
}
}
userdao
public interface UserDao {
User findUserByUserName(String username);
List<User> findAll();
void deleteById(int id);
void add(User user);
User selectById(int id);
void update(User user);
}
UseServiceImpl
public class UserServiceImpl implements IUserService {
@Autowired
private UserDao userDao;
@Override
public boolean login(String username, String password) {
User user = (User) userDao.findUserByUserName(username);
if(user!=null&&user.getPassword().equals(password)){
return true;
}else{
return false;
}
}
@Override
public List<User> findAll() {
return userDao.findAll();
}
@Override
public void add(User user) {
userDao.add(user);
}
@Override
public void deleteById(int id) {
userDao.deleteById(id);
}
@Override
public void toUpdate(User user) {
}
@Override
public User selectUserById(int id) {
return userDao.selectById(id);
}
@Override
public void update(User user) {
userDao.update(user);
}
}