菜品名 | 菜品价格 | 操作 |
---|
cltroller层
package cn.et.springmvc.lesson06.controller;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import cn.et.springmvc.lesson06.dao.MyFoodDapImpl;
@Controller
public class MyFoodController {
@Autowired
MyFoodDapImpl mdi;
//查询
@RequestMapping(value="/popo",method=RequestMethod.GET)
public String queryFood(String foodname, OutputStream os) throws UnsupportedEncodingException, IOException{
List
//删除
@RequestMapping(value="/deleteFood/{foodid}",method=RequestMethod.DELETE)
public String deleteFood(@PathVariable String foodid, OutputStream os) throws UnsupportedEncodingException, IOException{
try{
mdi.deleteFood(foodid);
os.write("1".getBytes("UTF-8"));
}catch(Exception e){
os.write("0".getBytes("UTF-8"));
}
return null;
}
//修改
@RequestMapping(value="/updateFood/{foodid}",method=RequestMethod.PUT)
public String updateFood(@PathVariable String foodid,String foodname,String price, OutputStream os) throws UnsupportedEncodingException, IOException{
try{
mdi.updateFood(foodid, foodname, price);
//返回以代表成功
os.write("1".getBytes("UTF-8"));
}catch(Exception e){
//返回0代表失败
os.write("0".getBytes("UTF-8"));
}
return null;
}
//增加
@RequestMapping(value="/saveFood",method=RequestMethod.POST)
public String saveFood(String foodname,String price, OutputStream os) throws UnsupportedEncodingException, IOException{
try{
mdi.saveFood(foodname, price);
os.write("1".getBytes("UTF-8"));
}catch(Exception e){
os.write("0".getBytes("UTF-8"));
}
return null;
}
}
dao层
package cn.et.springmvc.lesson06.dao;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
@Repository
public class MyFoodDapImpl {
@Autowired
JdbcTemplate jdbc;
public List
public void deleteFood(String foodid){
String sql="delete from food where foodid="+foodid;
jdbc.execute(sql);
}
public void saveFood(String foodname,String price){
String sql="insert into food(foodName,price) values('"+foodname+"','"+price+"')";
jdbc.execute(sql);
}
public void updateFood(String foodid,String foodname,String price){
String sql="update food set foodName='"+foodname+"',price='"+price+"' where foodid="+foodid;
jdbc.execute(sql);
}
}
手机扫一扫
移动阅读更方便
你可能感兴趣的文章