springMvc+AJAX+JSON的增删改查
阅读原文时间:2023年07月08日阅读:2
<%@ page language="java" import="java.util.\*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" \+ request.getServerName() + ":" + request.getServerPort() \+ path + "/"; %>




My JSP 'list.jsp' starting page





 

菜品名菜品价格操作


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> queryAllFood = mdi.queryAllFood(foodname);
JSONArray arry=JSONArray.fromObject(queryAllFood);
String j=arry.toString();
os.write(j.getBytes("UTF-8"));
return null;
}

//删除
@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> queryAllFood(String foodname){
String sql="select * from food where foodName like '%"+foodname+"%'";
return jdbc.queryForList(sql);
}

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);
}
}