angular js 页面修改数据存入数据库
一、编写service,修改数据要根据ID回显数据
//根据ID查询
public Brand findById(Long id);
//修改
public int update(Brand brand);
二、编写serviceImpl
@Override
public Brand findById(Long id) {
return brandDao.selectByPrimaryKey(id);
}
@Override
public int update(Brand brand) {
return brandDao.updateByPrimaryKeySelective(brand);
}
三、编写controller,修改返回Result类型
//根据ID查询回显数据
// @RequestMapping("/findById")
@RequestMapping("/{id}")
// public Brand findById(Long id){
public Brand findById(@PathVariable(value = "id")Long id){
return brandService.findById(id);
}
//修改
@RequestMapping("/update")
public Result update(@RequestBody Brand brand){
int id = brandService.update(brand);
if (id>0){
return new Result(true,"修改成功");
}else {
return new Result(false,"修改失败");
}
}
四、编写页面html
//添加保存
$scope.save=function () {
var url="../brand/save.do";
//判断是添加还是修改,添加$scope.entity.id==null,否则执行修改
if ($scope.entity.id!=null){
url="../brand/update.do"
}
//发送请求$http.post(url,$scope.entity),第一个参数是请求地址,第二个参数是提交的数据
$http.post(url,$scope.entity).success(function (response) {
if(response.success){
//重新加载
return $scope.reloadList();
}else {
alert(response.message);
}
});
}
//根据ID回显数据
$scope.findById=function (id) {
// $http.get('../brand/findById.do?id='+id).success(function (response) {
$http.get('../brand/'+id+'.do').success(function (response) {
$scope.entity=response;
});
}
<td class="text-center">
<button type="button" class="btn bg-olive btn-xs" data-toggle="modal"
data-target="#editModal" ng-click="findById(entity.id)" >修改</button>
</td>
//ng-click="save()",根据请求地址判断调用save
<button class="btn btn-success" data-dismiss="modal" aria-hidden="true" ng-click="save()">保存</button>