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

}
最近下载更多
yoosuo  LV1 11月23日
fff2003  LV4 11月20日
dcgr1115  LV1 11月15日
孟豌豆的豌豆  LV1 11月15日
huyihucaicai  LV1 11月15日
毛智莹  LV1 11月15日
sanmu36  LV1 11月15日
jtxjjjjj  LV1 11月9日
最近浏览更多
2943074382 11月25日
暂无贡献等级
lisi12312 11月25日
暂无贡献等级
yoosuo  LV1 11月23日
fff2003  LV4 11月20日
dcgr1115  LV1 11月15日
孟豌豆的豌豆  LV1 11月15日
huyihucaicai  LV1 11月15日
shiyehun 11月15日
暂无贡献等级
毛智莹  LV1 11月15日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友