接上篇 ssm整合增删查改实现代码

包结构
在这里插入图片描述
在这里插入图片描述

controller层

package com.ww.ssm.controller;

import com.ww.ssm.po.MyUser;
import com.ww.ssm.service.MyUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

@Controller
public class MyUserController {
    @Autowired
    MyUserService myUserService;

    @RequestMapping("/users.do")
    public ModelAndView userlist() throws Exception {
        List<MyUser> list=myUserService.selectUsersSer();
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("list",list);
        modelAndView.setViewName("myuserlist");
        return modelAndView;
    }

    @RequestMapping("/del.do")
    public ModelAndView deluser(int id) throws Exception {
        System.out.println(id+"-----------------");
        myUserService.delUserSer(id);
        return userlist();
    }

    @RequestMapping("/add.do")
    public String add() throws Exception {
        return "redirect:add.jsp";
    }

    @RequestMapping("/addMyuser.do")
    public ModelAndView adduser(MyUser myUser) throws Exception {
        myUserService.addUserSer(myUser);
        return userlist();
    }

    @RequestMapping("/up.do")
    public ModelAndView up(String id) throws Exception {
        int uid=Integer.valueOf(id);
        MyUser myUser = myUserService.selectByIdSer(uid);
        System.out.println(myUser);
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("myuser",myUser);
        modelAndView.setViewName("edit");
        return modelAndView;
    }

    @RequestMapping("/update.do")
    public ModelAndView up(MyUser myUser) throws Exception {
        myUserService.upUserSer(myUser);
        return userlist();
    }

    //批量查询操作
    @RequestMapping("/queryMyusers.do")
    public ModelAndView queryMyuserpacths(Integer[] check_id) throws Exception {
        List<MyUser> list=new ArrayList<MyUser>();
        for(int i:check_id)
        {
            System.out.println(i);
            list.add(myUserService.selectByIdSer(i));
        }
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("list",list);
        modelAndView.setViewName("myuserlist");
        return modelAndView;
    }


    //批量删除操作
    @RequestMapping("/deleteMyusers.do")
    public ModelAndView delMyUsers(Integer[] check_id) throws Exception {
        for(int i:check_id)
        {
            System.out.println(i);
            myUserService.delUserSer(i);
        }
        return userlist();
    }

    //批量修改操作
    @RequestMapping("/upMyusers.do")
    public ModelAndView upMyUsers(Integer[] check_id) throws Exception {
        List<MyUser> list=new ArrayList<MyUser>();
        for(int i:check_id)
        {
            System.out.println(i);
            list.add(myUserService.selectByIdSer(i));
        }
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("list",list);
        modelAndView.setViewName("uplist");
        return modelAndView;
    }

    //批量修改操作
    @RequestMapping("/updateUsers.do")
    public void updateMyUsers(Integer[] check_id) throws Exception {

    }

}

mapper

package com.ww.ssm.mapper;

import com.ww.ssm.po.MyUser;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface MyUserMapper {
    @Select("select * from myuser")
    List<MyUser> selectUsers() throws Exception;

    @Delete("delete from myuser where id=#{id}")
    void delUser(int id)throws Exception;

    @Insert("insert into myuser(username,sex,address) values(#{username},#{sex},#{address})")
    void addUser(MyUser myUser) throws Exception;


    @Select("select * from myuser where id=#{id}")
    MyUser selectById(int id) throws Exception;

    @Update("update myuser set username=#{username},sex=#{sex},address=#{address} where id=#{id}")
    void upUser(MyUser myUser) throws Exception;
}

po

package com.ww.ssm.po;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Date;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class MyUser {

    private Integer id;
    private String username;
    private Date birthday;
    private String  sex;
    private String address;

    public MyUser(String username, String sex, String address) {
        this.username = username;
        this.sex = sex;
        this.address = address;
    }
}

service

package com.ww.ssm.service;

import com.ww.ssm.po.MyUser;
import org.springframework.stereotype.Service;

import java.util.List;
public interface MyUserService {
    List<MyUser> selectUsersSer() throws Exception;
    void delUserSer(int id)throws Exception;
    void addUserSer(MyUser myUser) throws Exception;
    MyUser selectByIdSer(int id) throws Exception;
    void upUserSer(MyUser myUser) throws Exception;
}

serviceimpl

package com.ww.ssm.service.impl;

import com.ww.ssm.mapper.MyUserMapper;
import com.ww.ssm.po.MyUser;
import com.ww.ssm.service.MyUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MyUserServiceImpl implements MyUserService {
    @Autowired
    MyUserMapper mapper;

    @Override
    public List<MyUser> selectUsersSer() throws Exception {
        return mapper.selectUsers();
    }

    @Override
    public void delUserSer(int id) throws Exception {
        mapper.delUser(id);
    }

    @Override
    public void addUserSer(MyUser myUser) throws Exception {
        mapper.addUser(myUser);
    }

    @Override
    public MyUser selectByIdSer(int id) throws Exception {
        return mapper.selectById(id);
    }

    @Override
    public void upUserSer(MyUser myUser) throws Exception {
        mapper.upUser(myUser);
    }
}

数据库
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值