首页>代码>springboot2.0+mybaits-plus+redis+mysql+vue实现用户数据的增删改查简单实例>/springboot_mp/src/main/java/com/ht/springboot_mp/controller/UserController.java
package com.ht.springboot_mp.controller;

import com.alibaba.fastjson.JSONObject;
import com.ht.springboot_mp.entity.User;
import com.ht.springboot_mp.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @company 宏图
 * @User Kodak
 * @create 2019-03-22 -14:10
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;



//返回首页
    @RequestMapping("/list")
    @ResponseBody
    public ModelAndView list(){
        List<User> list=userService.selall();
        ModelAndView mv=new ModelAndView();
        mv.addObject("list",list);
        mv.setViewName("index");
        return  mv;
    }

//    查询全部
    @RequestMapping("/all")
    @ResponseBody
    public JSONObject all(){
        JSONObject json=new JSONObject();
        List<User> list=userService.selall();
        json.put("list",list);

        return json;
    }

    //去登陆
    @RequestMapping("/tologin")
    public String tologin(){
        return "login";
    }

//登陆
    @RequestMapping("/logins")
    public String login(User user){
           user= userService.sellogin(user);
           if(user!=null){
               return "index";
           }
           return null;

    }



    @ResponseBody
    @RequestMapping("/del")
    public String del(Integer id){

        if(id!=null){
            userService.del(id);
        }
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("msg","删除成功");

//        return "redirect:list";
        return jsonObject.toString();
    }


    @RequestMapping("/toupd")
    @ResponseBody
    public String toupd(Integer id,User user){
        if (id!=null){
            JSONObject jsonObject=new JSONObject();
            User u=  userService.toupd(id);
            jsonObject.put("u",u);
            return jsonObject.toString();
        }
        return null;
    }

    @RequestMapping("/add")
//    @ResponseBody
    public  ModelAndView add(User user, MultipartFile faces, HttpServletRequest request, BindingResult bindingResult) throws IOException {
        String img="";
        if(faces.getOriginalFilename()!=null&&!faces.getOriginalFilename().equals("")){//上传了文件在进来
            //处理图片名称
            String picName= UUID.randomUUID().toString()+faces.getOriginalFilename().substring(faces.getOriginalFilename().lastIndexOf("."));
            String path="D:/temp-rainy/"; //设置存储位置
            //日期格式化 sim.format日期转字符串    sim.parse字符串装日期
            SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd");
            String data=sim.format(new Date());
            //整体路径
            String dirName="/upload/"+data;
            File dirFile = new File(path+dirName);
            if(!dirFile.exists()){  //创建文件夹
                dirFile.mkdirs();
            }
            File targetFile = new File(path+dirName,picName);
            faces.transferTo(targetFile);  // 保存文件


            img=dirName+"/"+picName;

        }

        if(img==null||img.equals("")){
            user.setFace(user.getFace());
        }else {
            user.setFace(img);
        }

        ModelAndView mv=new ModelAndView();
        if(user.getId()==null){
             userService.add(user);
            mv.setViewName("redirect:list");
        }else {

            userService.upd(user);
            mv.setViewName("redirect:list");
        }
        return mv;

    }



}
最近下载更多
543666826  LV34 2024年12月28日
lingtiejing  LV15 2023年12月18日
flygrass  LV12 2023年12月5日
你爹正在加载中  LV4 2023年9月8日
龚顺龙  LV3 2023年8月7日
2017143155  LV12 2023年6月27日
czh20021029  LV1 2023年6月5日
wxy0309  LV6 2023年5月20日
bug428124  LV1 2023年3月12日
qwe123456asd  LV3 2023年3月8日
最近浏览更多
LeoloSean  LV1 1月3日
fucomcn  LV1 2024年12月28日
543666826  LV34 2024年12月28日
ma406805131  LV19 2024年12月28日
微信网友_6955249237250048  LV5 2024年12月28日
yangchengshuai  LV15 2024年10月29日
charleswang  LV7 2024年10月21日
17380184110 2024年6月18日
暂无贡献等级
zeng1206  LV7 2024年6月17日
szf123  LV12 2024年5月29日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友