ssm框架小总结——批量删除

jsp

layui框架:

//头工具栏事件 table.on('toolbar(test)', function(obj){ var checkStatus = table.checkStatus(obj.config.id); switch(obj.event){ //找到 批量删除的id
case 'getCheckData':
// 定义变量, 批量删除的id var data = checkStatus.data; //layer.alert(JSON.stringify(data)); //alert(data.test_id);
//建立空数组, 存放要删除的id var str="";
// 循环 id 添加到容器里去 if(data.length>0){ for(var i=0;i<data.length;i++){ str+=data[i].test_id+","; } // ajax传值 layer.confirm('是否删除这'+data.length+'条数据?',{icon:1,title:'提示'},function(index){ $.ajax({ url:'${ctxtPath}/test/delAll?str='+ str, type:'get', data: $(data.form).serialize(), dataType:'json', success:function(data,textStatus,jqXHR){ window.location.reload(); } }); layer.closr(index); }); } else{ layer.alert("请选择要删除的数据 "); } break;

 

conllert

package com.laima.car.sys;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.laima.car.common.Page;
import com.laima.car.common.SystemLog;

@Controller
@RequestMapping(value="/test")
public class TestController {

	@Autowired
	private TestService testService;
	//跳转需要的页面
	@RequestMapping(value="/testall")
	public String testall(@ModelAttribute("command")Test test, HttpServletRequest request){
		return "/sys/quertest";
	}
	//查看所有数据  (同时也有搜索查询功能)
	@ResponseBody
	@RequestMapping(value="/getseeall")
	public Map<String, Object> getseeall(Test test, HttpServletRequest request){
	
		Map<String, Object> map = new HashMap<String, Object>();   前台layui框架,获取浅谈所有数据
		Page page = testService.queryMenu(test);
		map.put("code", "0");
		map.put("msg", "");
		map.put("count", page.getRecordCount());
		map.put("data", page.getResultList());
		return map;
	}
	
	//添加跳转页面
	@RequestMapping(value="/testadd")
	public String testadd(@ModelAttribute("command")Test test){
		return "/sys/testadd";
	}
	//添加数据

	@ResponseBody
	@RequestMapping(value="/addtest", method = RequestMethod.POST)
	public Map<String, Object> addtest(Test test, HttpServletRequest request){
	
		Map<String, Object> map = new HashMap<String, Object>();
		System.out.println();
		testService.addtest(test);
		map.put("status", "0");
		return map;
	}
	//修改 跳转并回显数据
	
	@RequestMapping(value="/updatatest/{TestId}")
	public String updatatest(@PathVariable String TestId, HttpServletRequest request){
		System.out.println(TestId);
		Test test=testService.updatatestUI(TestId);
		System.out.println(test.toString());
		request.setAttribute("coun", test);
		return "/sys/testup";	
	}
	//修改 数据
	@RequestMapping(value="/updTest", method = RequestMethod.POST)
	@SystemLog(module="菜单管理", methods="/menu/toUpdMenu&/admin/updMenu", logDesc="修改菜单")
	public String updTest(Test test, HttpServletRequest request){
		testService.updTest(test);
		request.setAttribute("alertMsg", "菜单修改成功");
		return "redirect:/menu/queryMenu";
	}
	//删除数据
	@ResponseBody
	@RequestMapping(value="/delTest/{testId}", method = RequestMethod.POST)
	@SystemLog(module="菜单管理", methods="/menu/delMenu", logDesc="删除菜单")
	public Map<String, Object> delTest(@PathVariable String testId, HttpServletRequest request){
		Map<String, Object> map = new HashMap<String, Object>();
		Test test = testService.getMenu(testId);
		System.out.println(test);
		testService.delTest(test);;
		map.put("status", "0");
		return map;
	}
	//批量删除

		@ResponseBody
		@RequestMapping(value="/delAll", method = RequestMethod.GET)
		@SystemLog(module="菜单管理", methods="/menu/delMenu", logDesc="删除菜单")
		public Map<String, Object> delAll( HttpServletRequest request){
			Map<String, Object> map = new HashMap<String, Object>();
			String str=request.getParameter("str");
			// testService.getMenu(str);	
			testService.delAll(str);
			System.out.println(str);
			map.put("status", "0");
			return map;
		}


	 
	
	
}

 service层

package com.laima.car.sys;

import java.util.ArrayList;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.laima.car.common.JdbcDao;
import com.laima.car.common.Page;

@Service
@Transactional
public class TestService {
     
	@Autowired
	private TestDao testDao;
	@Autowired
	private JdbcDao jdbcDao;
	//查看所有的人员资料
	public Page queryMenu(Test test){
		List<Object> args = new ArrayList<Object>();
		String Name =  test.getTestName();
		String calss=  test.getTestClass();
		StringBuffer sql = new StringBuffer(
	
		"SELECT  test_id, test_name,test_age,test_home,test_phone,test_class FROM test_lcy where 1 = 1 ");
		if(StringUtils.hasText(Name)){
			sql.append("and test_name like ?");
			args.add("%" + Name + "%");	
		}
		if(StringUtils.hasText( calss)){
			sql.append("and test_class like ?");
			args.add("%" + calss + "%");	
		}
	    // sql.append(" order by  desc");
		return jdbcDao.querySqlPage(sql.toString(), args.toArray());
	}
	//添加数据
	public void addtest(Test test){
		testDao.save(test);
		testDao.saveAndFlush(test);
	}
	//修改 跳转并回显数据
	public Test updatatestUI(String  TestId){
	return	testDao.getOne(TestId);
		
	}
	//修改
	public void updTest(Test test){
		testDao.saveAndFlush(test);
	}
	//删除
	public void delTest(Test test){
		String sql = "delete from test_lcy where test_id = ? ";
		jdbcDao.updateSql(sql, test.getTestId());
	}
	
	public Test getMenu(String testId){
		return testDao.getOne(testId);
	}
	
	//批量删除
	public void delAll(String str){
		//String menuSql = "delete from test_lcy where test_id in ('"+ menuIds +"') ";
		str=str.replaceAll(",", "','");
		String sql = "delete from test_lcy where test_id in ('"+ str +"') ";
		jdbcDao.updateSql(sql);
		//jdbcDao.updateSql(menuSql);
	}
	
}

 分割字符串的几种方式

分割字符串“
String time = "158,9874,7506";	//数值传入
	
	  1.  time=time.replaceAll(",", "','");
		String[] times = time.split(",");
		
		
	2.	String str = "'";
		for (int i = 0; i < times.length; i++) {
			System.out.println(times[i]);
			str +=  times[i] + "',";
		}



	 3.   str = str.substring(0, str.length()-1);
	      StringBuffer s=new StringBuffer(str);
	      s.delete(s.length()-1, s.length());
          
结果:	
	String sql = "delete from table where id in("+str+")";
		System.out.println(sql);
	}}

 

posted @ 2019-10-26 15:03  腊月出去玩  阅读(668)  评论(0编辑  收藏  举报