首页>代码>ssm框架实现的简单增删改查功能>/MyMvc/src/main/java/edu/gyc/controller/ComputerController.java
package edu.gyc.controller;

import edu.gyc.model.Car;
import edu.gyc.model.User;
import edu.gyc.service.CarService;
import edu.gyc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class ComputerController {

    @Autowired
    private CarService carService;

    @Autowired
    private UserService userService;


    //得到全部数据
    @ResponseBody
    @RequestMapping("getCars")
    public Object  getCars() {
        List<Car> lists = carService.getAllCar();
        Map map = new HashMap();
        map.put("code", 0);
        map.put("data", lists);
        map.put("msg","success!");
        return  map;
    }

    //方法的重载
//    @ResponseBody
//    @RequestMapping("getCar")
//    public Object  getCars(int page ,int limit,String queryKey,String keyType) {
//
//        ResponseData responseData = carService.queryCars(page,limit,queryKey,keyType);
//
//        return  responseData;
//    }



//    //表格展示
//    @RequestMapping("mylist")
//    public String mylist() {
//        return "list";
//    }


   //增加行
    @RequestMapping("add")
    @ResponseBody
    public Object  add(@RequestBody Car car) {   //返回的是json数据
        System.out.println(car);
        int n= carService.addCar(car);
        Map map = new HashMap();
        if (n == 1) {
            map.put("returnCode", 200);

            map.put("msg","success!");
        }


        return map;
    }

    //单行删除
    @RequestMapping("del")
    @ResponseBody
    public Object  dels(Integer id) {
        System.out.println("Hi del:"+id);
        int n=0;
        String msg =  "删除数据id:" + id + "失败!";
        n= carService.delCar(id);
        if (n== 1) {
            msg = "删除数据id:" + id + "成功!";
        }
        Map map = new HashMap();
        map.put("returnCode", 200);
        map.put("msg", msg);
        return map;
    }


    //批量删除
    @RequestMapping("dels") //批量删除
    @ResponseBody
    public Object  dels(@RequestParam(value = "ids") List<Integer> ids) {   //接受ajax提交的数组,需要使用RequestParam指明value名称
        System.out.println("Hello dels:"+ids);
        int n=0;
        String msg = "多条数据删除成功!";
        for (int i : ids) {
            int r = carService.delCar(i);
            if (r == 1) {
                n++;
            }
        }
        if (n!= ids.size()) {
            msg = "删除数据有误,仅仅删除" + n + "条。";
        }
        Map map = new HashMap();
        map.put("returnCode", 200);
        map.put("msg", msg);
        return map;
    }









    //更新数据
    @RequestMapping("update")
    @ResponseBody
    public Object  update(@RequestBody Car car) {
        System.out.println(car);
        int n= carService.updateCar(car);
        Map map = new HashMap();
        if (n == 1) {
            map.put("returnCode", 200);

            map.put("msg","修改 success!");
        }


        return map;
    }



    //登录  注册功能
    //表单提交过来的路径
    //登录功能



    @RequestMapping("/checkLogin")

    public String checkLogin(User user, Model model){
        //调用service方法
        user = userService.checkLogin(user.getUsername(), user.getPassword());

        //若有user则添加到model里并且跳转到成功页面
        if(user != null){
            model.addAttribute("user",user);
            return "list";
        }
        return "fail";
//        logger.info("name:"+user.getUsername());
    }


    //正常访问login页面
    @RequestMapping("/login")
    public String login(){
        return "login";
    }


    //注册功能
    @RequestMapping("/regist")
    public String regist(){
        return "regist";
    }


    @RequestMapping("/doRegist")
    public String doRegist(User user,Model model){
        System.out.println(user.getUsername());
        userService.Regist(user);
        return "success";
    }

}
最近下载更多
qwer123978ca  LV1 2022年12月19日
kkykkk  LV1 2022年12月19日
111112222333312131  LV3 2022年12月9日
mikyfx  LV4 2022年11月30日
zxc131313  LV11 2022年11月18日
2442938559  LV1 2022年11月14日
2511952410  LV9 2022年9月20日
evagsd  LV6 2022年8月25日
198347470  LV1 2022年8月1日
zhang9852  LV1 2022年7月30日
最近浏览更多
moon-y 3月14日
暂无贡献等级
wjy1225  LV1 2月9日
1481917707  LV4 2022年12月31日
qwer123978ca  LV1 2022年12月19日
kkykkk  LV1 2022年12月19日
2102565387  LV1 2022年12月17日
jzh20020707  LV2 2022年12月12日
111112222333312131  LV3 2022年12月9日
微信网友_6248713511227392  LV11 2022年12月5日
2633528624  LV1 2022年12月3日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友