电商项目(Day06)

1.店铺信息编辑

  • 编写shopDao
    //更新店铺信息 返回1成功 -1失败
    int updateShop(Shop shop);

    //通过shopid查询店铺
    Shop queryByShopId(long shopId);
  • 编写shopDao.xml 通过定义resultMap来实现数据库中所需数据一一注入。使用SQL92语法进行多表查询。
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.tian.dao.ShopDao">
    <resultMap id="shopMap" type="com.tian.pojo.Shop">
        <id column="shop_id" property="shopId"/>
        <result column="shop_name" property="shopName"/>
        <result column="shop_desc" property="shopDesc"/>
        <result column="shop_addr" property="shopAddr"/>
        <result column="phone" property="phone"/>
        <result column="shop_img" property="shopImg"/>
        <result column="priority" property="priority"/>
        <result column="create_time" property="createTime"/>
        <result column="last_edit_time" property="lastEditTime"/>
        <result column="enable_status" property="enableStatus"/>
        <result column="advice" property="advice"/>
        <association property="area" column="area_id" javaType="com.tian.pojo.Area">
            <id column="area_id" property="areaId"/>
            <result column="area_name" property="areaName"/>
        </association>
        <association property="shopCategory" column="shop_category_id" javaType="com.tian.pojo.ShopCategory">
            <id column="shop_category_id" property="shopCategoryId"/>
            <result column="shop_category_name" property="shopCategoryName"/>
        </association>
        <association property="owner" column="user_id" javaType="com.tian.pojo.PersonInfo">
            <id column="user_id" property="userId"/>
            <result column="name" property="name"/>
        </association>
    </resultMap>

    <insert id="insertShop" useGeneratedKeys="true" keyColumn="shop_id" keyProperty="shopId">
            INSERT INTO o2o.tb_shop( owner_id, area_id, shop_category_id, shop_name, shop_desc, shop_addr, phone
                                   , shop_img, priority, create_time, last_edit_time, enable_status, advice)
            values (#{owner.userId}, #{area.areaId}, #{shopCategory.shopCategoryId}, #{shopName},
                    #{shopDesc}, #{shopAddr}, #{phone}, #{shopImg}, #{priority}, #{createTime}, #{lastEditTime},
                    #{enableStatus},
                    #{advice})
     </insert>

    <update id="updateShop" parameterType="com.tian.pojo.Shop">
        update o2o.tb_shop
        <set>
            <if test="shopName!=null">shop_name=#{shopName},</if>
            <if test="shopDesc!=null">shop_desc=#{shopDesc},</if>
            <if test="shopAddr!=null">shop_addr=#{shopAddr},</if>
            <if test="phone!=null">phone=#{phone},</if>
            <if test="shopImg!=null">shop_img=#{shopImg},</if>
            <if test="priority!=null">priority=#{priority},</if>
            <if test="lastEditTime!=null">last_edit_time=#{lastEditTime},</if>
            <if test="enableStatus!=null">enable_status=#{enableStatus},</if>
            <if test="advice!=null">advice=#{advice},</if>
            <if test="area!=null">area_id=#{area.areaId},</if>
            <if test="shopCategory!=null">shop_category_id=#{shopCategory.shopCategoryId}</if>
        </set>
        where shop_id=#{shopId}
    </update>

    <select id="queryByShopId" resultMap="shopMap" parameterType="Long">
        SELECT
            s.shop_id,
            s.shop_name,
            s.shop_desc,
            s.shop_addr,
            s.phone,
            s.shop_img,
            s.priority,
            s.create_time,
            s.last_edit_time,
            s.enable_status,
            s.advice,
            a.area_id,
            a.area_name,
            sc.shop_category_id,
            sc.shop_category_name
        FROM
            o2o.tb_shop s,
            o2o.tb_shop_category sc,
            o2o.tb_area a
        WHERE
            s.area_id=a.area_id
        AND
            s.shop_category_id=sc.shop_category_id
        AND
            s.shop_id = #{shopId}
    </select>
</mapper>
  • 编写shopService
//修改shop
    ShopExecution modifyShop(Shop shop, InputStream shopImgInputStream,String fileName) throws ShopOperationException;

    //通过id找到shop
    Shop getByShopId(long shopId);
  • 编写shopServiceImpl
package com.tian.service.impl;

import com.tian.controller.shopadmin.ShopManagementController;
import com.tian.dao.ShopDao;
import com.tian.dto.ShopExecution;
import com.tian.enums.ShopStateEnum;
import com.tian.exceptions.ShopOperationException;
import com.tian.pojo.Shop;
import com.tian.service.ShopService;
import com.tian.util.ImageUtil;
import com.tian.util.PathUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.io.InputStream;
import java.util.Date;

@Service
public class ShopServiceImpl implements ShopService {

    @Autowired
    private ShopDao shopDao;

    @Override
    @Transactional  //该注解声明了这是sql中的事务方法 当抛出RuntimeException时该方法执行的数据库操作会被回滚掉
    public ShopExecution addShop(Shop shop, File shopImg) {
        if (shop==null){
            return new ShopExecution(ShopStateEnum.NULL_SHOP);
        }else {
            //判断是否具有店铺初始信息(未完成)

            try {
                //附初始值
                shop.setEnableStatus(0);
                shop.setCreateTime(new Date());
                shop.setLastEditTime(new Date());
                int effectedNum = shopDao.insertShop(shop);
                //当店铺创建失败就会抛出异常
                if (effectedNum<=0){
                    throw new ShopOperationException("店铺创建失败");
                }else {
                    if (shopImg != null){
                        //存储图片
                        try {
                            addShopImg(shop,shopImg);
                        }catch (Exception e){
                            throw new ShopOperationException("addShopImg error:"+e.getMessage());
                        }
                        //更新店铺的图片地址
                        effectedNum = shopDao.updateShop(shop);
                        if (effectedNum<=0){
                            throw new ShopOperationException("更新图片地址失败");
                        }
                        return new ShopExecution(ShopStateEnum.CHECK,shop);
                    }
                }

            }catch (Exception e){
                throw new ShopOperationException("addShop error:"+e.getMessage());
            }
        }
        return new ShopExecution(ShopStateEnum.INNER_ERROR);


    }

    @Transactional
    @Override
    public ShopExecution modifyShop(Shop shop, InputStream shopImgInputStream, String fileName) throws ShopOperationException {

        if (shop == null||shop.getShopId()==null){
            return new ShopExecution(ShopStateEnum.NULL_SHOP);
        }else {
            try {
                //1.判断是否需要处理图片
                if (shopImgInputStream!=null && fileName!=null && !"".equals(fileName)){
                    Shop tempShop = shopDao.queryByShopId(shop.getShopId());
                    if (tempShop.getShopImg()!=null){
                        ImageUtil.deleteFileOrPath(tempShop.getShopImg());
                    }
                    //处理图片流
                    File file = new File(fileName);
                    ShopManagementController.inputStreamToFile(shopImgInputStream,file);
                    addShopImg(shop,file);
                }
                //2.更新店铺信息
                shop.setLastEditTime(new Date());
                int effectedNum = shopDao.updateShop(shop);
                if (effectedNum <= 0 ){
                    return new ShopExecution(ShopStateEnum.INNER_ERROR);
                }else {
                    shop = shopDao.queryByShopId(shop.getShopId());
                    return new ShopExecution(ShopStateEnum.SUCCESS,shop);
                }
            }catch (Exception e){
                throw new ShopOperationException("修改信息出错"+e.getMessage());
            }

        }

    }

    @Override
    public Shop getByShopId(long shopId) {
        return shopDao.queryByShopId(shopId);
    }

    private void addShopImg(Shop shop, File shopImg) {
        //获取shop图片目录的相对值路径
        String dest = PathUtil.getShopImagePath(shop.getShopId());

        String shopImgAddr = ImageUtil.generateThumbnail(shopImg,dest);
        shop.setShopImg(shopImgAddr);
    }
}
  • 编写删除文件的工具方法
 //一旦修改图片就会将原先的图片删除掉 storePath是文件路径还是目录路径,如果是文件则删除该文件,如果是目录则删除该目录下的所有文件
    public static void deleteFileOrPath(String storePath){
        File fileOrPath = new File(storePath);
        if (fileOrPath.exists()){
            if (fileOrPath.isDirectory()){
                File files[] = fileOrPath.listFiles();
                for (File file : files) {
                    file.delete();
                }
            }
            fileOrPath.delete();
        }
    }
  • 测试shopDao
@Test
    public void testQueryShopById(){
        long shopId = 29L;
        Shop shop = shopDao.queryByShopId(shopId);
        System.out.println(shop);
    }
  • 测试shopService
 @Test
    public void testUpdateShop() throws ShopOperationException, FileNotFoundException {
        Shop shop = shopService.getByShopId(29L);
        shop.setShopName("凯飞店");
        File shopImg = new File("F:\\学习资料\\美女图片\\2021031707550769.jpeg");

        InputStream is = new FileInputStream(shopImg);
        ShopExecution shopExecution = shopService.modifyShop(shop, is, "2021031707550769.jpeg");
        System.out.println(shopExecution.getShop());


    }

2.前后端交互实现

  • 配置后端controller层。
@RequestMapping(value = "/modifyshop",method = RequestMethod.POST)
    @ResponseBody //声明自动转化为json
    private Map<String,Object> modifyShop(HttpServletRequest request){
        Map<String,Object> modelMap = new HashMap<>();

        //判断验证码是否正确
        if (!CodeUtil.checkVerifyCode(request)){
            modelMap.put("success",false);
            modelMap.put("errMsg","输入了错误的验证码");
            return modelMap;
        }

        //1.接受并转化相应的参数,包括店铺信息以及图片信息
        String shopStr = HttpServletRequestUtil.getString(request, "shopStr");
        ObjectMapper mapper = new ObjectMapper();
        Shop shop = null;
        try {
            shop=mapper.readValue(shopStr,Shop.class);
        } catch (Exception e) {
            modelMap.put("success",false);
            modelMap.put("errMsg",e.getMessage());
            return modelMap;
        }

        //获取spring能够处理的CommonsMultipartFile
        CommonsMultipartFile shopImg = null;

        //创建解析器
        CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());

        //通过解析器判断当前的reques中是否存在图片流,如无流直接返回错误信息
        if (commonsMultipartResolver.isMultipart(request)){
            MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
            shopImg = (CommonsMultipartFile) multipartHttpServletRequest.getFile("shopImg");
        }

        //2.修改店铺信息
        if (shop != null && shop.getShopId()!=null){
            PersonInfo owner = new PersonInfo();

            File shopImgFile = new File(PathUtil.getImgBasePath()+ ImageUtil.getRandomFileName()+shopImg.getOriginalFilename());
            try {
                shopImgFile.createNewFile();
            } catch (IOException e) {
                modelMap.put("success",false);
                modelMap.put("errMsg",e.getMessage());
                return modelMap;
            }

            ShopExecution se = null;
            try {
                if (shopImg == null){
                    se = shopService.modifyShop(shop,null,null);
                }else {
                    se = shopService.modifyShop(shop,shopImg.getInputStream(),shopImgFile.getName());
                }

                if (se.getState() == ShopStateEnum.SUCCESS.getState()){
                    modelMap.put("success",true);
                }else {
                    modelMap.put("success",false);
                    modelMap.put("errMsg",se.getStateInfo());
                }
            } catch (IOException e) {
                throw new ShopOperationException("图片修改出错"+e.getMessage());
            }

            return modelMap;
        }else {
            modelMap.put("success",false);
            modelMap.put("errMsg","请输入店铺信息");
            return modelMap;
        }

    }


 

 @RequestMapping(value = "/getshopbyid",method = RequestMethod.GET)
    @ResponseBody
    private Map<String,Object> getShopById(HttpServletRequest request){
        Map<String,Object> modelMap = new HashMap<>();
        Long shopId = HttpServletRequestUtil.getLong(request,"shopId");
        if (shopId>-1){
            try {
                Shop shop = shopService.getByShopId(shopId);
                List<Area> areaList = areaService.getAreaList();
                modelMap.put("shop",shop);
                modelMap.put("areaList",areaList);
                modelMap.put("success",true);
            }catch (Exception e){
                modelMap.put("success",false);
                modelMap.put("errMsg",e.toString());
            }

        }else {
            modelMap.put("success",false);
            modelMap.put("errMsg","empty shopId");
        }
        return modelMap;
    }
  • 设置前端js代码,从而实现代码的复用。
//根据传递过来的key进行匹配 通过正则表达式 匹配出合适的参数名,返回参数中的值
function getQueryString(name){
    let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    let r = window.location.search.substring(1).match(reg);
    if (r != null){
        return decodeURIComponent(r[2]);
    }
    return '';
}

$(function (){
    let shopId = getQueryString('shopId');
    let isEdit = shopId?true:false;
    let initUrl = '/shop/getshopinitinfo';
    let registerShopUrl = '/shopadmin/registershop';
    let shopInfoUrl ='/shop/getshopbyid?shopId='+shopId;
    let editShopUrl = '/shopadmin/modifyshop';
    if (!isEdit){
        getShopInitInfo();
    }else {
        getShopInfo(shopId);
    }


    function getShopInfo(shopId){
        $.getJSON(shopInfoUrl,function (data){
            if (data.success){
                let shop = data.shop;
                $('#shop-name').val(shop.shopName);
                $('#shop-addr').val(shop.shopAddr);
                $('#shop-phone').val(shop.phone);
                $('#shop-desc').val(shop.shopDesc);
                let shopCategory = '<option data-id="'
                +shop.shopCategory.shopCategoryId+'"selected>'
                +shop.shopCategory.shopCategoryName + '</option>';
                let tempAreaHtml ='';
                data.areaList.map(function (item,index){
                    tempAreaHtml += '<option data-id="'+item.areaId+'">'
                    +item.areaName+'</option>';
                });
                $('#shop-category').html(shopCategory);
                $('#shop-category').attr('disabled','disabled');
                $('#area').html(tempAreaHtml);
                $('#area option[data-id="'+shop.area.areaId+'"]').attr('selected','selected');



            }
        });
    }

    function getShopInitInfo(){

        $.getJSON(initUrl,function (data){
            //当返回的json中success属性为true时执行下一步
            if (data.success){
                let tempHtml = '';
                let tempAreaHtml = '';

                //从JSON中获取数据商铺类型信息
                data.shopCategoryList.map(function (item,index){
                    tempHtml += '<option data-id="'+item.shopCategoryId+'">'+item.shopCategoryName+'</option>';
                });

                //从JSON中获取区域信息
                data.areaList.map(function (item,index){
                    tempAreaHtml += '<option data-id="' +item.areaId+'">'+item.areaName+'</option>';
                });
                //在指定id后插入数据
                $('#shop-category').html(tempHtml);
                $('#area').html(tempAreaHtml);
            }
        });

    }
    //当点击提交后触发监听事件
    $('#submit').click(function (){
        //将前端数据封装
        let shop = {};
        if (isEdit){
            shop.shopId=shopId;
        }
        shop.shopName = $('#shop-name').val();
        shop.shopAddr = $('#shop-addr').val();
        shop.phone = $('#shop-phone').val();
        shop.shopDesc = $('#shop-desc').val();
        shop.shopCategory={
            shopCategoryId:$('#shop-category').find('option').not(function (){
                return !this.selected;
            }).data('id')
        };
        shop.area={
            areaId:$('#area').find('option').not(function (){
                return !this.selected;
            }).data('id')
        };
        let shopImg = $('#shop-img')[0].files[0];
        let fromData = new FormData();
        fromData.append('shopImg',shopImg);
        fromData.append('shopStr',JSON.stringify(shop));

        //验证码
        let verifyCodeActual = $('#j_captcha').val();
        if (!verifyCodeActual){
            $.toast('请输入验证码');
            return;
        }
        fromData.append('verifyCodeActual',verifyCodeActual);

        //使用ajax来向后端传递数据
        $.ajax({
            url:(isEdit?editShopUrl:registerShopUrl),
            type:'POST',
            data:fromData,
            contentType:false,
            processData: false,
            cache:false,
            success:function (data){
                if (data.success){
                    $.toast('提交成功!');
                }else {
                    $.toast('提交失败!'+data.errMsg);
                }
            }
        })
    });
})

小结:在js代码中我们可以通过url携带的参数的不同来动态的实现页面的复用,在mybatis中我们可以通过设置resultMap标签来实现多表查询数据的注入。在sql语句中如果出现了相同的列名,那么就必须取别名否则无法显示第二次出现的列的数据。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Devotes

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值