封装处理表单删除操作的实现方法
@实现背景:2016.11.16 关于表单增删改查操作,此法可减少代码开发量,目前在前后台都只需要修改一个文件即可
@以下代码只写出关键部分代码,无关部分省略
@黄色高亮为关键代码提示
@只需要更改加粗部分代码即可实现基本单表删除操作
二话不说,代码实现:
1.(agrList.html)前端表单基本样式如下:_
<table class="table table-bordered">
<thead>
<tr>
<th>农机编号</th>
<th>状态(是否在线)</th>
<th>机型</th>
<th>生产单位</th>
<th>产地</th>
<th>生产日期</th>
<th>当前版本</th>
<th>监控模块组</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="agr in agrList | filter:searchFilter(search)">
<td>{{agr.agr_num}}</td>
<td>{{(agr.agr_state==1)?"在线":"离线"}}</td>
<td>{{agr.agr_type}}</td>
<td>{{agr.product_company}}</td>
<td>{{agr.product_area}}</td>
<td>{{agr.product_time}}</td>
<td>{{agr.agr_version}}</td>
<td>{{agr.name}}</td><!-- 模块组名称 -->
<td>
<a href="javascript:void(0);" class="table-link" style="margin:0;" ng-click="edit(agr)">
<span class="fa-stack">
<i class="fa fa-square fa-stack-2x"></i>
<i class="fa fa-pencil fa-stack-1x fa-inverse"></i>
</span>
</a>
<a href="javascript:void(0);" class="table-link danger" style="margin:0;" ng-click="del(agr)">
<span class="fa-stack">
<i class="fa fa-square fa-stack-2x"></i>
<i class="fa fa-trash-o fa-stack-1x fa-inverse"></i>
</span>
</a>
</td>
</tr>
</tbody>
</table>
2.(AgrController.js)需修改
'use strict';
MyApp.controller('AgrController', function(AgrRDService, AgrTypeService, $scope, $location, $cookieStore) {
$scope.findAgrList = function(){
AgrRDService.findAgrList($scope);
}
//删除
$scope.del = function(agr){
var **controllerName="scope.findAgrList()";**//重新刷新列表函数
var **surl="agr/delAgr";**//后台路由
if(confirm("确定要删除吗?")){
AgrRDService.del(agr.id,controllerName,surl,$scope);//调用统一函数,此处不作任何修改
}
}
........
3.(AgrRDService.js)
'use strict';
MyApp.factory('AgrRDService', function($resource, $http, $location, $cookieStore) {
return new AgrRDService($resource, $http, $location, $cookieStore);
});
this.del = function(id,controllerName,surl,scope){
var Resource = resource(surl, {}, actions);
Resource.save({id:id}, function(data) {
if(data.response==200){
// findAgrRDList(scope);//刷新重载页面(原先每一个都需要独立写入代码)
eval(controllerName);
}
if(data.response==300){
alert("删除失败");
}
});
}
4.(AgrController.java)后台,需修改
@Controller
@RequestMapping("/agr")
public class AgrController extends BaseController {
@Autowired
private IAgrService agrService;
@RequestMapping(value = **"/delAgrRD"**, method = RequestMethod.POST)
public @ResponseBody
Map<String,Object> delAgrRD(@RequestBody **AgrUploadClass upload**)
{
Map<String, Object> jsonMap = new HashMap<String, Object>();
**String tableName="agr_info";//传入表名**
int res=agrService.delById(upload.getId(),tableName);
if(res>0){
jsonMap.put("response", 200);
}
else
{
jsonMap.put("response", 300);
}
return jsonMap;
}
@RequestMapping(value = "/delAgr", method = RequestMethod.POST)
public @ResponseBody
Map<String,Object> delAgr(@RequestBody AgrInfo upload)
{
Map<String, Object> jsonMap = new HashMap<String, Object>();
String tableName="agr_info";//传入表名
int res=agrService.delById(upload.getId(),tableName);
if(res>0){
jsonMap.put("response", 200);
}
else
{
jsonMap.put("response", 300);
}
return jsonMap;
}
。。。。。。
5.(IAgrService.java)
public interface IAgrService {
List<AgrInfo> findAgrRDList(AgrInfo agr);
List<AgrInfo> findAgrRDHisList();
int delById(Integer id, String tableName);
}
6.(AgrServiceImpl.java)
@Service
@Transactional
public class AgrServiceImpl implements IAgrService {
@Autowired
private SqlSessionTemplate sqlSessionTemplate;
@Override
public List<AgrInfo> findAgrRDList(AgrInfo agr) {
return sqlSessionTemplate.getMapper(AgrInfoMapper.class).findAgrRDList(agr);
}
@Override
public int delById(Integer id,String tableName) {
return sqlSessionTemplate.getMapper(AgrInfoMapper.class).delById(id,tableName);
}
}
7.(AgrInfoMapper.java)
public interface AgrInfoMapper {
List<AgrInfo> findAgrRDList(AgrInfo agr);
// int delById(Integer id, String tableName);这个是以前的代码,每一个删除操作都要创建几对端口和实现
int delById(@Param("id")Integer id,@Param("tableName")String tableName);
}
8.(AgrInfoMapper.xml)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.angular_springmvc.dao.AgrInfoMapper" >
<delete id="delById" statementType="STATEMENT">
delete from ${tableName} where id=${id}
</delete>
....
本人新手,请高手们多指教!