品牌列表的增删改查

页面代码

<script type="text/javascript">
		var app=angular.module('pinyougou',['pagination']);
		app.controller('brandController',function ($scope,$http) {
		    $scope.findAll=function() {
                $http.get('../brand/findAll.do').success(
                    function (response) {
                        $scope.list = response;
                    }
                );
            }

            $scope.paginationConf={
				currentPage:1,
				totalItems:10,
				itemsPerPage:10,
				perPageOptions:[10,20,30,40,50],
				onChange:function () {
					$scope.reloadList();
                }
            };

            $scope.reloadList=function(){
                $scope.search($scope.paginationConf.currentPage , $scope.paginationConf.itemsPerPage);
            }

            $scope.findPage=function (page,size) {
                $http.get('../brand/findPage.do?page='+page+'&size='+size).success(
                    function(response) {
						$scope.list=response.rows;
						$scope.paginationConf.totalItems=response.total;
                    }
				);
            }

            $scope.save=function () {
                var methodName='add';
                if($scope.entity.id!=null){
					methodName='update';
				}
				$http.post("../brand/"+methodName+".do",$scope.entity).success(
				    function (response) {
						if(response.success){
							$scope.reloadList();
						}else {
						    alert(response.message);
						}
                    }
				);
            }
            
            $scope.findOne=function (id) {
				$http.get("../brand/findOne.do?id="+id).success(
				    function (response) {
						$scope.entity=response;
                    }
				);
            }

            $scope.selectIds=[];

            $scope.updateSelection=function ($event,id) {
                if($event.target.checked){
                    $scope.selectIds.push(id);
				}else {
                    var index=$scope.selectIds.indexOf(id);
                    $scope.selectIds.splice(index,1);
				}
            }

            $scope.delete=function () {
				$http.get("../brand/delete.do?ids="+$scope.selectIds).success(
				    function (response) {
						if(response.success){
						    $scope.reloadList();
						}else {
						    alert(response.message);
						}
                    }
				);
            }

            $scope.searchEntity={};

            $scope.search=function (page,size) {
                $http.post('../brand/search.do?page='+page+'&size='+size,$scope.searchEntity).success(
                    function(response) {
                        $scope.list=response.rows;
                        $scope.paginationConf.totalItems=response.total;
                    }
                );
            }
            
        });


	</script>

brandservice

public interface BrandService {

	public List<TbBrand> findAll();

	public PageResult findPage(int pageNum,int pageSize);

	public PageResult findPage(TbBrand brand,int pageNum,int pageSize);

	public void add(TbBrand brand);

	public TbBrand findOne(Long id);

	public void update(TbBrand brand);

	public void delete(Long[] ids);
	
}

brandService

@Service
public class BrandServiceImpl implements BrandService {

	@Autowired
	private TbBrandMapper brandMapper;
	
	@Override
	public List<TbBrand> findAll() {

		return brandMapper.selectByExample(null);
	}

	@Override
	public PageResult findPage(int pageNum, int pageSize) {
		PageHelper.startPage(pageNum,pageSize);

		Page<TbBrand> page = (Page<TbBrand>) brandMapper.selectByExample(null);

		return new PageResult(page.getTotal(),page.getResult());
	}

	@Override
	public PageResult findPage(TbBrand brand, int pageNum, int pageSize) {
		PageHelper.startPage(pageNum,pageSize);

		TbBrandExample example=new TbBrandExample();

		Criteria criteria = example.createCriteria();

		if(brand!=null){
			if(brand.getName()!=null && brand.getName().length()>0){
				criteria.andNameLike("%"+brand.getName()+"%");
			}
			if(brand.getFirstChar()!=null && brand.getFirstChar().length()>0){
				criteria.andFirstCharLike("%"+brand.getFirstChar()+"%");
			}
		}

		Page<TbBrand> page = (Page<TbBrand>) brandMapper.selectByExample(example);

		return new PageResult(page.getTotal(),page.getResult());
	}

	@Override
	public void add(TbBrand brand) {
		brandMapper.insert(brand);
	}

	@Override
	public TbBrand findOne(Long id) {
		return brandMapper.selectByPrimaryKey(id);
	}

	@Override
	public void update(TbBrand brand) {
		brandMapper.updateByPrimaryKey(brand);
	}

	@Override
	public void delete(Long[] ids) {
		for (Long id:ids) {
			brandMapper.deleteByPrimaryKey(id);
		}
	}

}

brandController

@RestController
@RequestMapping("/brand")
public class BrandController {

	@Reference
	private BrandService brandService;
	
	@RequestMapping("/findAll")
	public List<TbBrand> findAll(){
		return brandService.findAll();		
	}

	@RequestMapping("/findPage")
	public PageResult findPage(int page,int size){
		return brandService.findPage(page,size);
	}

	@RequestMapping("/add")
	public Result add(@RequestBody TbBrand brand){
		try{
			brandService.add(brand);
			return new Result(true,"添加成功");
		}catch (Exception ex){
			ex.printStackTrace();
			return new Result(false,"添加失败");
		}
	}

	@RequestMapping("/findOne")
	public TbBrand findOne(Long id){
		return brandService.findOne(id);
	}

	@RequestMapping("/update")
	public Result update(@RequestBody TbBrand brand){
		try{
			brandService.update(brand);
			return new Result(true,"修改成功");
		}catch (Exception e){
			e.printStackTrace();
			return new Result(false,"修改失败");
		}
	}

	@RequestMapping("/delete")
	public Result delete(Long[] ids){
		try{
			brandService.delete(ids);
			return new Result(true,"删除成功");
		}catch (Exception e){
			e.printStackTrace();
			return new Result(false,"删除失败");
		}
	}

	@RequestMapping("/search")
	public PageResult search(@RequestBody TbBrand brand,int page,int size){
		return brandService.findPage(brand,page,size);
	}
}

猜你喜欢

转载自blog.csdn.net/Liaoyuh/article/details/81436270