sprintboot带有附件的实体上传、下载、修改、删除

1、entity-------实体

package com.h3c.rois.anops.score.gift.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;

@Entity
@Table(name="gift")
public class GiftEntity
{
  private static final long serialVersionUID = -6339822845706129318L;

  @Id
  @GeneratedValue(strategy=GenerationType.IDENTITY)
  @Column(name="id")
  private Long id;

//描述信息
  @Column(name="name")
  private String name;

  @Lob
  @JsonIgnore
  @Basic(fetch=FetchType.LAZY)//对应数据库的longblob
  @Column(name="picture", columnDefinition="longblob", nullable=true)
  private byte[] picture;

  @Column(name="stock")
  private int stock;

  @Column(name="create_time")
  private long createTime;

  @Column(name="need_integral")
  private int needIntegral;

//上传的附件的名称
  @Column(name="pic_name")
  private String picName;

  @Column(name="is_delete")
  private int isDelete;

  public Long getId()
  {
    return this.id;
  }

  public void setId(Long id) {
    this.id = id;
  }

  public String getName() {
    return this.name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public byte[] getPicture() {
    return this.picture;
  }

  public void setPicture(byte[] picture) {
    this.picture = picture;
  }

  public int getStock() {
    return this.stock;
  }

  public void setStock(int stock) {
    this.stock = stock;
  }

  public long getCreateTime() {
    return this.createTime;
  }

  public void setCreateTime(long createTime) {
    this.createTime = createTime;
  }

  public int getNeedIntegral() {
    return this.needIntegral;
  }

  public void setNeedIntegral(int needIntegral) {
    this.needIntegral = needIntegral;
  }

  public String getPicName() {
    return this.picName;
  }

  public void setPicName(String picName) {
    this.picName = picName;
  }

  public int getIsDelete() {
    return this.isDelete;
  }

  public void setIsDelete(int isDelete) {
    this.isDelete = isDelete;
  }
}

2、model

package com.h3c.adesk.cbms.score.gift.model;

import java.io.Serializable;

public class GiftModel
  implements Serializable
{
  private static final long serialVersionUID = 1L;
  private Long id;
  private String giftName;
  private byte[] picture;
  private Integer stock;
  private Long createTime;
  private String createTimeStr;
  private int needIntegral;
  private String picName;
  private Integer isDelete;

  public GiftModel()
  {
  }

  public GiftModel(Long id, String giftName, byte[] picture, Integer stock, Long createTime, int needIntegral)
  {
    this.id = id;
    this.giftName = giftName;
    this.picture = picture;
    this.stock = stock;
    this.createTime = createTime;
    this.needIntegral = needIntegral;
  }

  public Long getId() {
    return this.id;
  }

  public void setId(Long id) {
    this.id = id;
  }

  public String getGiftName() {
    return this.giftName;
  }

  public void setGiftName(String giftName) {
    this.giftName = giftName;
  }

  public byte[] getPicture() {
    return this.picture;
  }

  public void setPicture(byte[] picture) {
    this.picture = picture;
  }

  public Integer getStock() {
    return this.stock;
  }

  public void setStock(Integer stock) {
    this.stock = stock;
  }

  public Long getCreateTime() {
    return this.createTime;
  }

  public void setCreateTime(Long createTime) {
    this.createTime = createTime;
  }

  public String getCreateTimeStr() {
    return this.createTimeStr;
  }

  public void setCreateTimeStr(String createTimeStr) {
    this.createTimeStr = createTimeStr;
  }

  public int getNeedIntegral() {
    return this.needIntegral;
  }

  public void setNeedIntegral(int needIntegral) {
    this.needIntegral = needIntegral;
  }

  public String getPicName() {
    return this.picName;
  }

  public void setPicName(String picName) {
    this.picName = picName;
  }

  public Integer getIsDelete() {
    return this.isDelete;
  }

  public void setIsDelete(Integer isDelete) {
    this.isDelete = isDelete;
  }
}

3、mapper

package com.h3c.adesk.cbms.anopsmysql.mapper;

import com.h3c.adesk.cbms.score.gift.model.GiftModel;
import java.util.List;
import org.springframework.beans.factory.annotation.Qualifier;

@Qualifier("anopsmysqlSqlSessionTemplate")
public abstract interface GiftMapper
{
  public abstract void deleteGift(Long paramLong);

  public abstract void insertGift(GiftModel paramGiftModel);

  public abstract void updateGift(GiftModel paramGiftModel);

  public abstract GiftModel getGiftModelById(Long paramLong);

  public abstract List<GiftModel> getGiftModelList();
}

4、mapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.h3c.adesk.cbms.anopsmysql.mapper.GiftMapper">
    <resultMap id="GiftResultMap" type="com.h3c.adesk.cbms.score.gift.model.GiftModel">
        <id column="id" jdbcType="INTEGER" property="id" />
        <result column="name" jdbcType="VARCHAR" property="giftName" />
        <result column="stock" jdbcType="INTEGER" property="stock" />
        <result column="create_time" jdbcType="BIGINT" property="createTime" />
        <result column="need_integral" jdbcType="INTEGER" property="needIntegral" />
        <result column="pic_name" jdbcType="VARCHAR" property="picName" />
    </resultMap>

    <resultMap id="GiftResultMapWithBlob" type="com.h3c.adesk.cbms.score.gift.model.GiftModel">
        <id column="id" jdbcType="INTEGER" property="id" />
        <result column="picture" property="picture" typeHandler="org.apache.ibatis.type.BlobTypeHandler" />
        <result column="name" jdbcType="VARCHAR" property="giftName" />
        <result column="stock" jdbcType="INTEGER" property="stock" />
        <result column="create_time" jdbcType="BIGINT" property="createTime" />
        <result column="need_integral" jdbcType="INTEGER" property="needIntegral" />
        <result column="pic_name" jdbcType="VARCHAR" property="picName" />
    </resultMap>
    <!-- æ°å¢ -->
    <insert id="insertGift" parameterType="com.h3c.adesk.cbms.score.gift.model.GiftModel">
		insert into gift(name,picture,stock,create_time,need_integral,pic_name,is_delete)
		values (#{giftName},#{picture},#{stock},#{createTime},#{needIntegral},#{picName},#{is_delete});
	</insert>

    <!--å é¤-->
    <delete id="deleteGift" parameterType="java.lang.Long">
	    delete from gift
	    where id = #{id,jdbcType=INTEGER}
	</delete>

    <!--ä¿®æ¹-->
    <!--String name, byte[] picture, int stock, Long createTime-->
    <update id="updateGift" parameterType="com.h3c.adesk.cbms.score.gift.model.GiftModel">
        update gift
        <set>
            <if test="giftName != null">
                name = #{giftName},
            </if>
            <if test="picture != null">
                picture = #{picture},
            </if>
            <if test="stock != null">
                stock = #{stock},
            </if>
            <if test="createTime != null">
                create_time = #{createTime},
            </if>
            <if test="needIntegral != null">
                need_integral = #{needIntegral},
            </if>
            <if test="picName != null">
                pic_name = #{picName},
            </if>
            <if test="isDelete != null">
                is_delete = #{isDelete},
            </if>
        </set>
        where id = #{id}
    </update>

    <select id="getGiftModelById" resultMap="GiftResultMapWithBlob">
		select * from gift where id = #{id};
	</select>

    <select id="getGiftModelList" resultMap="GiftResultMapWithBlob">
        select * from gift where is_delete=0 order by create_time desc
	</select>

</mapper>

5、service

package com.h3c.adesk.cbms.score.gift.service;

import com.h3c.adesk.cbms.score.gift.model.GiftModel;
import java.util.List;

public abstract interface GiftService
{
  public abstract void save(GiftModel paramGiftModel);

  public abstract GiftModel getGiftModelById(Long paramLong);

  public abstract void update(GiftModel paramGiftModel);

  public abstract void deleteGiftModelById(Long paramLong);

  public abstract List<GiftModel> getList();
}

6、serviceImpl

package com.h3c.adesk.cbms.score.gift.service.serviceimpl;

import com.h3c.adesk.cbms.anopsmysql.mapper.GiftMapper;
import com.h3c.adesk.cbms.score.gift.model.GiftModel;
import com.h3c.adesk.cbms.score.gift.service.GiftService;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class GiftServiceImpl
  implements GiftService
{

  @Resource
  GiftMapper giftMapper;

  @Transactional
  public void save(GiftModel giftModel)
  {
    this.giftMapper.insertGift(giftModel);
  }

  @Transactional(readOnly=true)
  public GiftModel getGiftModelById(Long id)
  {
    return this.giftMapper.getGiftModelById(id);
  }

  @Transactional
  public void update(GiftModel giftModel)
  {
    this.giftMapper.updateGift(giftModel);
  }

  @Transactional
  public void deleteGiftModelById(Long id)
  {
    this.giftMapper.deleteGift(id);
  }

  public List<GiftModel> getList()
  {
    return this.giftMapper.getGiftModelList();
  }
}

7、controller

package com.h3c.adesk.cbms.score.gift.controller;

import com.h3c.adesk.cbms.score.gift.model.GiftModel;
import com.h3c.adesk.cbms.score.gift.service.GiftService;
import com.h3c.adesk.cbms.vo.GiftView;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/gift")
public class GiftController {
    private static Log logger = LogFactory.getLog(GiftController.class);
    @Resource
    GiftService giftService;
    /**
     * 获取禮物列表
     * @return List
     */
    @RequestMapping(value = "/getGiftList/list", method = RequestMethod.GET)
    public List<GiftModel> getGiftList() {
        logger.info("start get hot activity list");
        List<GiftModel> giftModelList = giftService.getList();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        giftModelList.forEach(giftModel -> {
            giftModel.setCreateTimeStr(sdf.format(new Date(giftModel.getCreateTime())));
        });
        return giftModelList;

    }

    /**
     * 新增礼物
     * @param giftView giftView
     * @param response HttpServletResponse
     * @throws IOException IOException
     * @throws ParseException ParseException
     */
    @RequestMapping(value = "/addGift", method = RequestMethod.POST)
    public void addGift(GiftView giftView, HttpServletResponse response)
            throws IOException, ParseException {
        byte[] image = giftView.getImage().getBytes();
        GiftModel giftModel = new GiftModel();
        BeanUtils.copyProperties(giftView, giftModel);
        long time = System.currentTimeMillis();
        giftModel.setCreateTime(time);
        giftModel.setPicture(image);
        giftModel.setPicName(giftView.getImage().getOriginalFilename());
        giftModel.setIsDelete(0);
        giftService.save(giftModel);
        response.sendRedirect("/gift");
    }

    /**
     * 获取礼物
     * @param id 热门活动表Id
     * @return GiftModel GiftModel
     */
    @RequestMapping(value = "/gift/{id}", method = RequestMethod.GET)
    public GiftModel getGiftById(@PathVariable Long id) {
        logger.info("start get gift by id");
        GiftModel giftModel = giftService.getGiftModelById(id);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = sdf.format(giftModel.getCreateTime());
        giftModel.setCreateTimeStr(time);
        return giftModel;
    }

    /**
     * 编辑
     * @param giftView giftView
     * @param response HttpServletResponse
     * @throws IOException IOException
     * @throws ParseException ParseException
     */
    @RequestMapping(value = "/editGift", method = RequestMethod.POST)
    public void editGiftById(GiftView giftView, HttpServletResponse response)
            throws IOException, ParseException {
        logger.info("start editGiftById");
        byte[] image = giftView.getImage().getBytes();
        GiftModel giftModel = new GiftModel();
        BeanUtils.copyProperties(giftView, giftModel);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        giftModel.setPicName(giftService.getGiftModelById(giftView.getId()).getPicName());
        if (image.length > 0) {
            giftModel.setPicture(image);
            giftModel.setPicName(giftView.getImage().getOriginalFilename());
        }
        giftService.update(giftModel);
        response.sendRedirect("/gift");
    }

    /**
     * 删除
     * @param id 热门活动表Id
     * @param response response
     * @throws IOException IOException
     */
    @RequestMapping(value = "/delGift/{id}", method = RequestMethod.DELETE)
    public void deleteGiftById(@PathVariable Long id, HttpServletResponse response) throws IOException {
        logger.info("start deleteGiftById");
        GiftModel giftModel = giftService.getGiftModelById(id);
        giftModel.setIsDelete(1);
        giftService.update(giftModel);
    }

    /**
     * 获取图片
     * @param id Id
     * @param response HttpServletResponse
     */
    @RequestMapping(value = "/getGift/img/{id}", method = RequestMethod.GET,
            produces = {MediaType.IMAGE_PNG_VALUE, MediaType.IMAGE_JPEG_VALUE, MediaType.IMAGE_GIF_VALUE})
    public void getGiftImage(@PathVariable Long id, HttpServletResponse response) {
        logger.info("start get Gift img");
        GiftModel giftModel = giftService.getGiftModelById(id);
        try (OutputStream os = response.getOutputStream();) {
            os.write(giftModel.getPicture());
        } catch (IOException e) {
            logger.error("getGiftImage error", e);
        }
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

寅灯

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

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

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

打赏作者

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

抵扣说明:

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

余额充值