效果
重启tomcat,通过访问地址
http://127.0.0.1:8080/tmall_ssm/admin_property_list?cid=12
可以看到属性管理的界面
注: 这cid=12是分类的id,根据你的实际运行情况,采取不同的id值
Property
Property实体类已经和其他所有的实体类一起,在所有的****这个环节就一起自动生成好了。 不过仅仅有自动生成的实体类代码,还不足以支撑业务需要,所以在Property基础上增加了一个Category 字段。 这个属性的用途将会在编辑功能讲解步骤里进行讲解。
PropertyService
新建PropertyService,提供CRUD一套。 需要注意的是,因为在业务上需要查询某个分类下的属性,所以list方法会带上对应分类的id。
package com.how2java.tmall.service;
import com.how2java.tmall.pojo.Property;
import java.util.List;
public interface PropertyService {
void add(Property c);
void delete(int id);
void update(Property c);
Property get(int id);
List list(int cid);
}
新增PropertyServiceImpl实现PropertyService对应的方法。通过调用自动生成的PropertyMapper就可以实现大部分方法了。
值得注意的是查询的时候用到了辅助查询类:PropertyExample
它的使用也很方便,这一行表示查询cid字段
example.createCriteria().andCidEqualTo(cid);
package com.how2java.tmall.service.impl;
import com.how2java.tmall.mapper.PropertyMapper;
import com.how2java.tmall.pojo.Category;
import com.how2java.tmall.pojo.Product;
import com.how2java.tmall.pojo.Property;
import com.how2java.tmall.pojo.PropertyExample;
import com.how2java.tmall.service.CategoryService;
import com.how2java.tmall.service.PropertyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class PropertyServiceImpl implements PropertyService {
@Autowired
PropertyMapper propertyMapper;
@Override
public void add(Property p) {
propertyMapper.insert(p);
}
@Override
public void delete(int id) {
propertyMapper.deleteByPrimaryKey(id);
}
@Override
public void update(Property p) {
propertyMapper.updateByPrimaryKeySelective(p);
}
@Override
public Property get(int id) {
return propertyMapper.selectByPrimaryKey(id);
}
@Override
public List list(int cid) {
PropertyExample example =new PropertyExample();
example.createCriteria().andCidEqualTo(cid);
example.setOrderByClause("id desc");
return propertyMapper.selectByExample(example);
}
}
PropertyController
package com.how2java.tmall.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.how2java.tmall.pojo.Category;
import com.how2java.tmall.pojo.Property;
import com.how2java.tmall.service.CategoryService;
import com.how2java.tmall.service.PropertyService;
import com.how2java.tmall.util.Page;
@Controller
@RequestMapping("")
public class PropertyController {
@Autowired
CategoryService categoryService;
@Autowired
PropertyService propertyService;
@RequestMapping("admin_property_add")
public String add(Model model, Property p) {
propertyService.add(p);
return "redirect:admin_property_list?cid="+p.getCid();
}
@RequestMapping("admin_property_delete")
public String delete(int id) {
Property p = propertyService.get(id);
propertyService.delete(id);
return "redirect:admin_property_list?cid="+p.getCid();
}
@RequestMapping("admin_property_edit")
public String edit(Model model, int id) {
Property p = propertyService.get(id);
Category c = categoryService.get(p.getCid());
p.setCategory(c);
model.addAttribute("p", p);
return "admin/editProperty";
}
@RequestMapping("admin_property_update")
public String update(Property p) {
propertyService.update(p);
return "redirect:admin_property_list?cid="+p.getCid();
}
@RequestMapping("admin_property_list")
public String list(int cid, Model model, Page page) {
Category c = categoryService.get(cid);
PageHelper.offsetPage(page.getStart(),page.getCount());
List<Property> ps = propertyService.list(cid);
int total = (int) new PageInfo<>(ps).getTotal();
page.setTotal(total);
page.setParam("&cid="+c.getId());
model.addAttribute("ps", ps);
model.addAttribute("c", c);
model.addAttribute("page", page);
return "admin/listProperty";
}
}