首页>代码>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";
    }

}
最近下载更多
xiaotao555555  LV3 6月22日
gdw532  LV1 3月27日
无耻之徒1241412  LV1 2024年12月25日
taoshen95  LV16 2024年12月17日
2421796355  LV1 2024年12月10日
248727703  LV1 2024年7月14日
wanglinddad  LV55 2024年6月21日
965718  LV1 2024年6月21日
最近浏览更多
xiaotao555555  LV3 6月22日
vincemokea  LV5 5月5日
gdw532  LV1 3月27日
六595323 2024年12月25日
暂无贡献等级
无耻之徒1241412  LV1 2024年12月25日
taoshen95  LV16 2024年12月17日
2421796355  LV1 2024年12月10日
微信网友_7265127836766208 2024年11月22日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友