首先我们从一个最简单的,不涉及数据关系的功能入手——商品分类功能 商品分类管理包括:
功能拆分实现方式查询商品分类信息编写select方法查询数据库记录添加商品分类使用instert方法增加数据库记录修改商品分类使用update对数据库字段进行更新删除商品分类使用update对数据库state字段进行更新经过详细的商品分类功能拆解,我们明确了接下来的工作。有了数据之后,需要把数据展示到表现层,这里,后台管理表现层使用jsp来实现,由于时间关系,以及我对前端设计并不擅长,所以该项目,我们使用“模板之家”下载来的一个网站模板进行开发,对ssm+网站模板的整合请参考我的博客: 分布式商城项目-后台开发-SSM工程整合网站模板 首先我们分析一下在这些操作中我们会涉及到哪些字段或者说属性。 图中是数据库表,因为时间关系,我们尽量简化项目,这里暂时弃用parentid这个字段,不去考虑多级目录。
我们遵循由表及里的顺序分析,首先需要一个商品类别管理的页面,在这个页面中我们以表格的形式显示类别信息 表格中需要显示类别名称,类别状态,类型排序三个字段信息,另外,还需要一个编辑按钮,所以会是一个四列的表格,这里就不去讲页面的设计了,在之后我会给出源码。 有了页面,还需要在页面上显示记录,记录的数据就需要从数据库去获取,这里我们编写接口去获取数据库中所有的类别信息记录 需要注意的是,整个项目是分布式的,不同服务之间的数据传递是流的形式,所以要让所有的pojo类都实现Serializable接口 首先,在dao层中获取多条记录的sql语句是需要我们进行编写的,在Categorymapper接口中增加方法:
List<Category> selectCategoryList();在CategoryMapper.xml中加入sql语句:
<select id="selectCategoryList" resultType="cn.yuechenc.pojo.Category"> select * from category </select>下一步编写service接口 在interfaces模块下的新建接口CategoryService,并添加方法定义
package cn.yuechenc.ycshop.manager.interfaces; import java.util.List; import cn.yuechenc.pojo.Category; public interface CategoryService { List<Category> selectCategoryList(); }在service模块中新建CategoryServiceImpl()实现类,实现CategoryService接口
package cn.yuechenc.ycshop.manager.service.impl; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import cn.yuechenc.manager.dao.mapper.CategoryMapper; import cn.yuechenc.pojo.Category; import cn.yuechenc.ycshop.manager.interfaces.CategoryService; @Service public class CategoryServiceImpl implements CategoryService { @Autowired private CategoryMapper categoryMapper; @Override public List<Category> selectCategoryList() { return categoryMapper.selectCategoryList(); } }编写controller 在web工程中,新建CategoryController类
package cn.yuechenc.ycshop.manager.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import cn.yuechenc.pojo.Category; import cn.yuechenc.ycshop.manager.interfaces.CategoryService; @Controller @RequestMapping("/Category") public class CategoryController { @Autowired private CategoryService categoryService; @RequestMapping("getCateList") private String getCateList(ModelMap model){ List<Category> categoryList=categoryService.selectCategoryList(); model.addAttribute("cateList", categoryList); return "Category"; } }此时运行是会报错的,因为service和web不在一个服务器,必须使用dubbo进行通信,所以需要添加配置,让dubbo服务暴露接口,在spring-service.xml中添加配置
<dubbo:service interface="cn.yuechenc.ycshop.manager.interfaces.CategoryService" ref="categoryServiceImpl" timeout="30000" />在spring-mvc.xml中添加配置
<dubbo:reference interface="cn.yuechenc.ycshop.manager.interfaces.CategoryService" id="categoryServiceImpl" timeout="30000" />我们通过点击导航中的“商品类别管理”,进入到这个页面所以需要修改nav.jsp导航菜单 现在我们在数据库中添加一条在记录用作测试
现在我们得到了数据,那么需要把数据显示到页面中 在Category.jsp中做如下配置 此处使用到了jstl标签,所以需要在页面加入引用:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>然后运行工程,访问商品类别管理页面 得到如图结果,查询功能就算是基本完成了
添加商品类别,我们需要考虑,在添加时,我们会设计上图中的字段,在添加时,id字段本项目采用UUID生成, 同样,要添加商品类型,我们一样需要有一个页面,这个页面需要有一个form表单,表单中需要有,商品类别名称输入框,商品类别排序输入框,以及一个下拉框,用来选择类型状态。如图: 接下来我们需要编写一个controller,去访问添加页面; 在CategoryController中添加方法:
@RequestMapping("/toadd") private String toaddCategory(){ return "CategoryEdit"; }当然,根据页面结构,我们需要从商品类别管理界面进入添加商品页面,所以我们在商品管理页面添加一个跳转按钮: 然后我们可以运行程序了,进入商品类型管理,点击添加按钮,就可以看到添加页面了 现在,页面问题解决了,接下来,我们需要得到用户输入的信息,并且将它保存到数据库,这样就算是完成了添加。 我们要怎么去获取页面的值呢? 首先需要配置form表单的action,给他一个人路径,告诉他把数据提交到哪里?,这样form表单就会把页面的数据提交到后端,那么后端就需要方法来接收,我们写一个方法来接收页面提交的数据,在 CategoryController中添加方法:
@RequestMapping("/addCate") private String addCate(ModelMap model, Category category){ return ""; }这时候,页面提交过来的数据就会自动映射到Category类里面,此时有一点需要注意,页面控件的name属性必须和实体类属性一致: 这样,然后我们设置form表单的action属性为:
${pageContext.request.contextPath}/Category/addCate此时就可以得到页面传过来的数据了, 下一步,我们就需要把得到的数据存到数据库,这时候,我们就需要service层和dao层的支持 首先我在CategoryService,添加方法, 然后在实现类中实现该方法
@Override public int insertCategory(Category category) { return categoryMapper.insertSelective(category); }此时接口方法就编写好了》 现在回到CategoryController中,这里我们获取到了Category但是还需要一个最关键的字段就是id 这里的id我们使用UUID,为category设置了id之后,还需要设置创建时间,之后只需要调用service接口的方法,把category存到数据库即可,另外,接口中的方法insertCategory会返回一个int值,代表sql执行的行数,由此我们只需要判断int值就可以判断插入数据库的操作是否成功,如果成功,直接返回商品类别列表即可,否则需要返回提示信息到界面,提示用户没有添加成功。
@RequestMapping("/addCate") private String addCate(ModelMap model, Category category){ category.setCateid(UUID.randomUUID().toString()); Date now=new Date(); category.setCreatetime(now); int rtn=categoryService.insertCategory(category); if(rtn>0){ return "redirect:/Category/getCateList"; }else{ model.addAttribute("msg", "添加类型失败!"); return "redirect:/Category/toadd"; } }接下来,运行服务,添加一个商品类别试一下: 可以看到无论列表还是数据库中,都已经有了刚刚添加的商品类别。到这里添加商品类别的功能就完成了
很多时候,我们在服务过程中会需要去修改一些类型,其实质操作就是根据数据库的id去修改数据库中的记录 我们去修改条记录时,首先我们需要把这条记录取出来,然后才能对他进行修改,所以修改的第一步是查询出需要修改的类别,然后把它放到页面中。 而这里呢,我们通过商品类别管理的列表中的编辑按钮来进入编辑页面,通过这个动作,会把当前这条记录的id传给后端 第一步,需要通过id把这条记录查询出来,这里需要服务处的支持,在CategoryService中添加方法
Category seletcCategoryById(String id);并且在实现类中实现该方法
@Override public Category seletcCategoryById(String id) { return categoryMapper.selectByPrimaryKey(id); }接下来编写controller
@RequestMapping("/toedit") private String toeditCategory(ModelMap model,String cateid){ Category category=categoryService.seletcCategoryById(cateid); model.addAttribute("cate", category); model.addAttribute("editType", "editCate"); return "CategoryEdit"; }通过此方法就可以查询记录并返回给页面 其中Category category=categoryService.seletcCategoryById(cateid);调用Service层方法查询记录 model.addAttribute("cate", category) 通过model把数据带给前端页面model.addAttribute("editType", "editCate");后面有分析。
这时候我们需要修改商品分类页面的编辑按钮 到这里,我们点击不同类别的编辑按钮,就会得到不同的记录,下面还要把这些数据和前端控件进行绑定,才能正常显示 到此,我们去运行服务,测试一下 点击水果的编辑按钮 进入了编辑页面, 到此,编辑页面初始化完成 第二步、我们分析一下,我们需要修改的字段其实跟添加时候的字段是一样的,那么我们就可以使用同一个jsp页面,但是此时又出现了问题,form表单在提交时需要指定一个url路径,而我们修改的时候的url和添加的时候的url是不一样的,所以,我们就可以让这个url变成动态的,在我们进入这个编辑页面的时候动态的生成对应的url 首先,我们去添加类型的时候将添加的url从后端传给form表单,而修改的时候,从后端把修改的url传给form表单,这里的传递操作,就可以在初始化页面的时候完成,接下来修改页面的form表单的action属性; 这里的editType就可以从后台传过来 在controller的初始化页面方法中添加editType
@RequestMapping("/toadd") private String toaddCategory(ModelMap model){ model.addAttribute("editType", "addCate"); return "CategoryEdit"; } @RequestMapping("/toedit") private String toeditCategory(ModelMap model,String cateid){ Category category=categoryService.seletcCategoryById(cateid); model.addAttribute("cate", category); model.addAttribute("editType", "editCate"); return "CategoryEdit"; }接下来和添加一样,我们需要把修改后的数据保存到数据库, 编写service接口,添加update方法:
int updateCategory(Category category);然后再实现类中实现此方法,调用dao层操作数据库数据
@Override public int updateCategory(Category category) { return categoryMapper.updateByPrimaryKeySelective(category); }最后编写controller
@RequestMapping("/editCate") private String editCate(ModelMap model, Category category){ Date now=new Date(); category.setUpdatetime(now); int rtn=categoryService.updateCategory(category); if(rtn>0){ return "redirect:/Category/getCateList"; }else{ model.addAttribute("msg", "修改类型失败!"); return "redirect:/Category/toedit?cateid="+category.getCateid(); } }于添加的方法相比,不需要设置id,但是需要设置修改时间。
这时候再次运行程序,修改一个类别 这里对水果进行编辑 把状态改为不可用,提交 再来看数据库 状态变成了0,修改时间也有了,现在基本的商品类别管理就算是完成了。