springboot的增删改查controller层代码

package com.wjh.controller;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wjh.dao.CommodityMapper;
import com.wjh.dao.UserMapper;
import com.wjh.dao.model.Commodity;
import com.wjh.dao.model.CommodityExample;
import com.wjh.dao.model.CommodityExample.Criteria;
import com.wjh.dao.model.User;
import com.wjh.untl.FileUtils;

@Controller
public class CommodityController {

@Autowired
CommodityMapper comm;

@Autowired
UserMapper us;

@RequestMapping("login")
@ResponseBody
public int login(HttpSession session,User user) {
	User usr = us.login(user);
	
	if(usr!=null) {
		return 1;
	}else {
		return 0;
	}
	
}

@RequestMapping("/list") 
public String list(CommodityExample example,Model model,Commodity co,Integer current,Integer size) { 
	if(current == null) { 
		current = 1; 
	} 
	if(size == null) { 
		size = 4; 
	} 
	PageHelper.startPage(current, size); 
	if(co.getName()!= null) { 
		Criteria criteria =  example.createCriteria(); 
		
		 criteria.andNameLike("%"+co.getName()+"%"); 
	} 
	List<Commodity> list =comm.selectByExample(example); 
	PageInfo<Commodity> pageInfo = new  PageInfo<Commodity>(list); 
	model.addAttribute("current", current); 
	model.addAttribute("pageInfo", pageInfo); 
	model.addAttribute("co", co); 
	return "list"; 
}
/**
 * 单删
 * @return
 */
@RequestMapping("/del") 
@ResponseBody
public int del(int id) { 
	int i = comm.deleteByPrimaryKey(id);
	return i; 
}

/**
 * 批删
 * @return
 */
@RequestMapping("/dels") 
@ResponseBody
public int dels(String id) { 
	comm.dels(id);
	return 1; 
}



/**
 * 跳转添加页面
 * @return
 */
@RequestMapping("/toadd") 
public String toadd() { 
	return "add"; 
}



/** 
 * 添加 
 * @param br 
 * @return 
 */ 
@RequestMapping("/add") 
public String add(Commodity co,MultipartFile  file,HttpServletRequest request) throws 
 IllegalStateException, IOException{
	
	String str = FileUtils.upload(file, request); 
    String logo="http://localhost:8888/upload/"+str; 
    co.setCreationtime(new Date());
    co.setTurnovertime(new Date());
    co.setLogo(logo); 
	comm.insertSelective(co); 
	return "redirect:list"; 
}



@RequestMapping("/toupdate") 
public String toupdate(int id,Model model) { 
	Commodity co = comm.selectByPrimaryKey(id);
	model.addAttribute("co", co);
	return "update"; 
}

@RequestMapping("/update") 
public String update(Commodity co,MultipartFile file,HttpServletRequest request) throws 
 IllegalStateException ,IOException 
{  
	
	String str = FileUtils.upload(file, request); 
	String logo="http://localhost:8888/upload/"+str; 
	co.setLogo(logo);
	co.setTurnovertime(new Date());
	comm.updateByPrimaryKeySelective(co);
	
	return "redirect:list"; 
}

}

猜你喜欢

转载自blog.csdn.net/qq_43630415/article/details/85136218