basic.jsp页面修改"修改"链接的地址
进入BasicDataController中的basicUpdate方法中,此方法不用调整
@RequestMapping("/basicUpdate")
public String basicUpdate(Integer id,Model m){
basicService.getUpdateInfo(id,m);
return "basic/basicUpdate";
}
然后就如service中的getUpdateInfo方法
@Override
public void getUpdateInfo(Integer id, Model m) {
BasicDataExample example = new BasicDataExample();
example.createCriteria().andParentIdIsNull();
// 查询所有的父类型
List<BasicData> parents = basicDataMapper.selectByExample(example );
m.addAttribute("parents", parents);
if(id != null && id > 0){
// 表示是更新数据,根据id查询出对应的数据信息
BasicData data = basicDataMapper.selectByPrimaryKey(id);
m.addAttribute("basic", data);
}
}
进入basicUpdate.jsp页面回写数据
<form action="/basic/saveOrUpdate">
<ul class="forminfo">
<input type="hidden" name="baseId" value="${basic.baseId }">
<li><label>基础数据</label>
<input name="baseName" type="text" value="${basic.baseName }"
class="dfinput" />
<i>基础数据不能超过30个字符</i>
</li>
<li><label>描述</label>
<input name="baseDesc" type="text" value="${basic.baseDesc }"
class="dfinput" />
</li>
<li><label>所属类型</label>
<div class="vocation">
<select class="select1" name="parentId">
<option value="0">--本身就是大类--</option>
<c:forEach items="${ parents}" var="parent">
<option value="${parent.baseId }" ${parent.baseId eq basic.parentId?"selected":"" }>
${parent.baseName }
</option>
</c:forEach>
</select>
</div>
<i></i>
</li>
<li><label> </label>
<input name="" type="submit"
class="btn" value="确认保存" /></li>
</ul>
</form>
提交后controller中代码,
@RequestMapping("/saveOrUpdate")
public String saveOrUpdate(BasicData bd){
if(bd.getParentId()==0){
bd.setParentId(null);
}
if(bd.getBaseId()!=null && bd.getBaseId() > 0){
// 表示更新数据
basicService.updateBasicData(bd);
}else{
// 表示添加数据
basicService.addBasicData(bd);
}
return "redirect:/basic/query";
}
访问测试
修改功能没问题,验证可自行加上~
basic.jsp删除菜单按钮修改地址
控制器处理请求方法
@RequestMapping("/delete")
public String delete(Integer id){
basicService.deleteBasicData(id);
return "redirect:/basic/query";
}
service实现方法
@Override
public void deleteBasicData(int id) {
basicDataMapper.deleteByPrimaryKey(id);
}
注意删除基础数据的时候如果删除的也类别数据,我们需要一并将对应的具体数据也给删除掉,所以要修改下deleteByPrimaryKey的sql语句
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
delete from t_basicdata
where base_id = #{baseId,jdbcType=INTEGER}
or parent_id = #{baseId,jdbcType=INTEGER}
</delete>
测试
ok~