用单表查询和Java程序,简便实现“多表查询”效果
需求:显示商品信息
商品表中分类ID(category1_id代表一级分类,以此类推)
分类表
商品表中有商品分类id,要想显示商品分类名称,需要联表查询分类表,为了提高效率,我们可以分别查询两张表,再异步返回商品分类名称
简单来说就是设置一个数组,将查询到的分类表数据以分类ID作为数组下标,分类名称作为数组值存入数组,前端显示数据时,将商品表查询到的分类ID作为数组下标显示出分类名称
代码实现
- goodsController .js
$scope.itemCatList=[];//商品分类列表
//查询商品分类表
$scope.findItemCatList=function(){
itemCatService.findAll().success(
function(response){
for(var i=0;i<response.length;i++){
$scope.itemCatList[response[i].id]=response[i].name;
}
}
);
}
- goods.html 初始化调用
<body class="hold-transition skin-red sidebar-mini" ng-app="pinyougou" ng-controller="goodsController" ng-init="findItemCatList()">
<td>{{itemCatList[entity.category1Id]}}</td>
<td>{{itemCatList[entity.category2Id]}}</td>
<td>{{itemCatList[entity.category3Id]}}</td>