前言:
看不清眼前路,只能上前一步。
正文:
将工程改造为SOA架构之后,实现查询如何来进行操作,请听小编娓娓道来。
以商品分类查询为例:
第一,创建pojo
package cn.e3mall.common.pojo;
import java.io.Serializable;
public class EasyUITreeNode implements Serializable{
private long id;
private String text;
private String state;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
}
值得关注:切记要序列化
第二,创建接口
package cn.e3mall.service;
import java.util.List;
import cn.e3mall.common.pojo.EasyUITreeNode;
public interface ItemCatService {
List<EasyUITreeNode> getItemCatlist(long parentId);
}
第三,创建实现接口的service
package cn.e3mall.service.impl;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.e3mall.common.pojo.EasyUITreeNode;
import cn.e3mall.mapper.TbItemCatMapper;
import cn.e3mall.pojo.TbItemCatExample.Criteria;
import cn.e3mall.pojo.TbItemCat;
import cn.e3mall.pojo.TbItemCatExample;
import cn.e3mall.service.ItemCatService;
@Service
public class ItemCatServiceimpl implements ItemCatService{
@Autowired
private TbItemCatMapper itemCatMapper;
@Override
public List<EasyUITreeNode> getItemCatlist(long parentId){
//根据parentId查询子节点列表
TbItemCatExample example = new TbItemCatExample();
Criteria criteria = example.createCriteria();
//设置查询条件
criteria.andParentIdEqualTo(parentId);
//执行查询
List<TbItemCat> list = itemCatMapper.selectByExample(example);
//创建返回结果list
List<EasyUITreeNode> resultList = new ArrayList<>();
//把列表转换成EasyUITreeNode列表
for (TbItemCat tbItemCat:list) {
EasyUITreeNode node = new EasyUITreeNode();
//设置属性
node.setId(tbItemCat.getId());
node.setText(tbItemCat.getName());
node.setState(tbItemCat.getIsParent()?"closed":"open");
//添加到结果列表
resultList.add(node);
}
//返回结果
return resultList;
}
}
第四,发布服务--e3-manager-service/src/main/resources/spring/applicationContext-service.xml
<dubbo:service interface="cn.e3mall.service.ItemCatService" ref="itemCatServiceimpl" timeout="600000"/>
第五,引入服务--e3-manager-web/src/main/resources/spring/springmvc.xml
<dubbo:reference interface="cn.e3mall.service.ItemCatService" id="itemCatService" />
第六,本地install,pojo和service
第七,创建controller
package cn.e3mall.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import cn.e3mall.common.pojo.EasyUITreeNode;
import cn.e3mall.service.ItemCatService;
/**
* 商品分类管理Controller
* @author linda
*
*/
@Controller
public class ItemCatController {
@Autowired
private ItemCatService itemCatService;
@RequestMapping("/item/cat/list")
@ResponseBody
public List<EasyUITreeNode> getItemCatList(
@RequestParam(name="id", defaultValue="0")Long parentId){
//调用服务查询节点列表
List<EasyUITreeNode> list = itemCatService.getItemCatlist(parentId);
return list;
}
}
至此,实现商品分类的查询便完成啦,先启动服务,再启动web。即可看到效果。
结语: