ssm高级注解开发(商品增删改查,图片上传等)

本文详述了SSM框架的高级注解开发,包括商品查询、添加、删除、修改功能,图片上传,JSON交互,RESTful API设计,异常处理和拦截器的使用。通过实例介绍了如何使用包装类型POJO参数绑定,处理批量操作,设置图片存储目录,配置JSON转换器,以及实现RESTful风格的URL。此外,还涉及到了自定义异常类和全局异常处理器的编写,以及模拟登录认证的实现。
摘要由CSDN通过智能技术生成

前言

在学习了Spring4+SpringMVC+mybatis整合以及ssm简单使用后,我们已经基本掌握ssm开发的基础知识,下面将进行ssm高级注解开发,包括的知识有:包装类型pojo参数绑定(即类中不是简单类型的属性,而是另外的pojo),list参数绑定,数据回显,上传图片,json交互,RESTful架构,拦截器等

此次开发是基于上面两篇文章中的项目实现的,上面两篇文章的项目源码请到我的github项目下载

添加按商品名称查询功能

我们已经在mapper包下的ItemsCustomMapper中定义了根据名称来查询的sql片段,
接下来,我们需要在controller类中的queryItems方法绑定ItemsQueryVo对象(即包装类型pojo对象)

原来的queryItems方法

@RequestMapping("/queryItems")
public ModelAndView queryItems() throws Exception{
    List<ItemCustom> itemsList=itemsService.findItemsList(null);

改为


@RequestMapping("/queryItems")
public ModelAndView queryItems(ItemsQueryVo itemsQueryVo) throws Exception{
    //形参上传入了ItemsQueryVo对象,并将形参传入service的findItemsList方法中
    List<ItemCustom> itemsList=itemsService.findItemsList(itemsQueryVo);
在itemsList.jsp页面中对应添加商品名称的查询条件(input输入框)
<form class="layui-form" action="${pageContext.request.contextPath}/items/queryItems.action" method="post">
    <label class="layui-form-label">查询条件</label>
    <div class="layui-input-block">
        <label class="layui-form-label">商品名称</label>
        <div class="layui-input-inline">
<%-- 注意:这里传入的是ItemsQueryVo类下的pojo类ItemCustom下的name属性 --%>
            <input class="layui-input" type="text" name="itemCustom.name">
        </div>
        <div class="layui-input-inline">
            <button class="layui-btn" type="submit" value="查询">查询</button>
        </div>
    </div>
</form>

这里只是取ItemsQueryVo类下的ItemCustom的name属性,若有多层包装类,则按照
类名.类名.(...).属性来命名form表单所要传入参数
然后,只要在商品显示页面的商品名称输入框输入所要查找的商品名称,就可以看到相应结果

新增添加商品信息功能

在mapper包下的ItemsMapper.xml下已经编写好往数据库插入记录的insert语句,
由于我这里使用的是sqlserver的id自增类型,不允许再插入id值,需要修改一下
insert语句(使用mysql的自增类型则不用修改),如下:


将body中form表单部分改为
<form class="layui-form" action="${pageContext.request.contextPath}/items/queryItems.action" method="post">
    <label class="layui-form-label">查询条件</label>
    <div class="layui-input-block">
        <label class="layui-form-label">商品名称</label>
        <div class="layui-input-inline">
<%-- 注意:这里传入的是ItemsQueryVo类下的pojo类ItemCustom下的name属性 --%>
            <input class="layui-input" type="text" name="itemCustom.name">
        </div>
        <div class="layui-input-inline">
            <button class="layui-btn" type="submit" value="查询">查询</button>
        </div>
    </div>
</form>

这里只是取ItemsQueryVo类下的ItemCustom的name属性,若有多层包装类,则按照
类名.类名.(...).属性来命名form表单所要传入参数
然后,只要在商品显示页面的商品名称输入框输入所要查找的商品名称,就可以看到相应结果

新增添加商品信息功能

在mapper包下的ItemsMapper.xml下已经编写好往数据库插入记录的insert语句,
由于我这里使用的是sqlserver的id自增类型,不允许再插入id值,需要修改一下
insert语句(使用mysql的自增类型则不用修改),如下:

<!--<insert id="insert" parameterType="po.Items">-->
  <!--insert into items (id, name, price, -->
    <!--detail, pic, createtime-->
    <!--)-->
  <!--values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{price,jdbcType=NUMERIC}, -->
    <!--#{detail,jdbcType=VARCHAR}, #{pic,jdbcType=VARCHAR}, #{createtime,jdbcType=TIMESTAMP}-->
    <!--)-->
<!--</insert>-->
<!--<insert id="insertSelective" parameterType="po.Items">-->
  <!--insert into items-->
  <!--<trim prefix="(" suffix=")" suffixOverrides=",">-->
    <!--<if test="id != null">-->
      <!--id,-->
    <!--</if>-->
    <!--<if test="name != null">-->
      <!--name,-->
    <!--</if>-->
    <!--<if test="price != null">-->
      <!--price,-->
    <!--</if>-->
    <!--<if test="detail != null">-->
      <!--detail,-->
    <!--</if>-->
    <!--<if test="pic != null">-->
      <!--pic,-->
    <!--</if>-->
    <!--<if test="createtime != null">-->
      <!--createtime,-->
    <!--</if>-->
  <!--</trim>-->
  <!--<trim prefix="values (" suffix=")" suffixOverrides=",">-->
    <!--<if test="id != null">-->
      <!--#{id,jdbcType=INTEGER},-->
    <!--</if>-->
    <!--<if test="name != null">-->
      <!--#{name,jdbcType=VARCHAR},-->
    <!--</if>-->
    <!--<if test="price != null">-->
      <!--#{price,jdbcType=NUMERIC},-->
    <!--</if>-->
    <!--<if test="detail != null">-->
      <!--#{detail,jdbcType=VARCHAR},-->
    <!--</if>-->
    <!--<if test="pic != null">-->
      <!--#{pic,jdbcType=VARCHAR},-->
    <!--</if>-->
    <!--<if test="createtime != null">-->
      <!--#{createtime,jdbcType=TIMESTAMP},-->
    <!--</if>-->
  <!--</trim>-->
<!--</insert>-->

<!-- 因为id在sqlserver设置为自增型的id,不需要再插入,这里改写一下insert的sql语句,将上面一段注释掉 -->
<!-- 即在insert中不插入id值 -->
<insert id="insert" parameterType="po.Items">
  insert into items (name, price,
  detail, pic, createtime
  )
  values (#{name,jdbcType=VARCHAR}, #{price,jdbcType=NUMERIC},
  #{detail,jdbcType=VARCHAR}, #{pic,jdbcType=VARCHAR}, #{createtime,jdbcType=TIMESTAMP}
  )
</insert>
<insert id="insertSelective" parameterType="po.Items">
  insert into items
  <trim prefix="(" suffix=")" suffixOverrides=",">
    <if test="id != null">
      id,
    </if>
    <if test="name != null">
      name,
    </if>
    <if test="price != null">
      price,
    </if>
    <if test="detail != null">
      detail,
    </if>
    <if test="pic != null">
      pic,
    </if>
    <if test="createtime != null">
      createtime,
    </if>
  </trim>
  <trim prefix="values (" suffix=")" suffixOverrides=",">
    <if test="id != null">
      #{id,jdbcType=INTEGER},
    </if>
    <if test="name != null">
      #{name,jdbcType=VARCHAR},
    </if>
    <if test="price != null">
      #{price,jdbcType=NUMERIC},
    </if>
    <if test="detail != null">
      #{detail,jdbcType=VARCHAR},
    </if>
    <if test="pic != null">
      #{pic,jdbcType=VARCHAR},
    </if>
    <if test="createtime != null">
      #{createtime,jdbcType=TIMESTAMP},
    </if>
  </trim>
</insert>

修改完成后,在service包下的ItemsService新增addItems方法


//添加商品
public void addItems(ItemCustom itemCustom) throws Exception;

在service/impl包下的ItemsServiceImpl重写addItems方

@Override
public void addItems(ItemCustom itemCustom) throws Exception{
    // 添加商品
    //使用itemsMapper中的insert方法来插入记录
    itemsMapper.insert(itemCustom);
}

在controller包下的ItemsController设置相应的跳转路径

//跳转到添加商品的jsp页面
@RequestMapping("/addItems")
public String add() throws Exception{
    return "addItems";
}

// 提交添加商品请求
@RequestMapping("/addItemsSubmit")
public String addSuccess(ItemCustom itemCustom) throws Exception{
    //编写添加商品的功能
    itemsService.addItems(itemCustom);

    return "forward:addItems.action";
}

在controller中指定了addItems.jsp页面,路径是WEB-INF/jsp/addItems.jsp

<%--
  Created by IntelliJ IDEA.
  User: 光玉
  Date: 2018/4/10
  Time: 20:39
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="utf-8" %>

<html>
<head>
    <title>添加商品</title>
    <script type="text/javascript" src="<%= request.getContextPath()%>/plugin/layui/layui.all.js"></script>
    <link rel="stylesheet" href="<%= request.getContextPath()%>/plugin/layui/css/layui.css">
</head>

<!-- 由于在前端itemsList.jsp使用弹出层将表单传入,
    提交按钮也绑定到弹出层的“添加”按钮,这里便不编写“提交”按钮 -->
<body style="background-color: #7e6c75">
<form id="formSubmit" class="layui-form" action="${pageContext.request.contextPath}/items/addItemsSubmit.action" method="post">
    <div class="layui-form-item">
        <label class="layui-form-label">商品名称</label>
        <div class="layui-input-block">
            <input class="layui-input" type="text" name="name" placeholder="必须填写此字段"/>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">商品价格</label>
        <div class="layui-input-block">
            <input class="layui-input" type="text" name="price" placeholder="必须填写此字段"/>
        </div>
    </div>

    <%--<div class="layui-form-item">
        <label class="layui-form-label">商品图片</label>
        <div class="layui-input-inline">
            <input class="layui-input" type="text" name="pic"/>
        </div>
    </div>--%>

    <div class="layui-form-item">
        <label class="layui-form-label">生产日期</label>
        <div class="layui-input-block">
            <input class="layui-input" type="text" name="createtime" placeholder="必填,格式为yyyy-MM-dd HH:mm:ss"/>
        </div>
    </div>

    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">商品详情</label>
        <div class="layui-input-block">
            <textarea class="layui-textarea" name="detail"></textarea>
        </div>
    </div>

    <%--<div class="layui-form-item">--%>
        <%--<div class="layui-input-block">--%>
            <%--<button class="layui-btn" type="submit">提交</button>--%>
            <%--<button type="reset" class="layui-btn layui-btn-primary">重置</button>--%>
        <%--</div>--%>
    <%--</div>--%>
</form>

<script>
    layui.use('form', function () {
        var form = layui.form;
        form.render();
    });
</script>

</body>
</html>

由于下面还要编写批量删除和批量修改功能,所以显示页面itemsList.jsp在编写完批量删除功能后再给出修改后的代码

新增批量删除功能

同样的道理,需要编写ItemsService,ItemsServiceImpl和ItemsController
ItemsService下添加如下代码:

//删除商品
public void deleteItemsById(Integer id) throws Exception;

ItemsServiceImpl下添加如下代码:

@Override
public void deleteItemsById(Integer id) throws Exception{
    // 删除商品
    //使用itemsMapper中的deleteByPrimaryKey方法来删除商品
    itemsMapper.deleteByPrimaryKey(id);
}

ItemsController下添加如下代码:

// 批量删除商品
@RequestMapping("/deleteItems")
public String deleteItems(Integer[] item_id) throws Exception{
    // 传入参数类型为商品的id数组
    // 根据id批量删除商品
    for(int i=0;i<item_id.length;i++){
        itemsService.deleteItemsById(item_id[i]);
    }
    // 完成删除操作后重定向回显示页面
    return "forward:queryItems.action";
}

注意:这里传入的是Integer类型的数组,即绑定数组类型
其实和简单类型一样,这里没有用@RequestParam,所以形参名称必须和
前端jsp传过来的参数名称一样(这里是checkbox复选框的name属性)

  • 2
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值