justsosoc
justsosoc
采纳率66.7%
2020-12-16 09:44

请问springboot写表单提交图片到数据库然后再显示出来该怎么写呢?

我现在是除了图片上传到数据库之外都做好了,用的是mapper,我一直查说用io但是实在不知道怎么加,纯小白,用了别人的想套都不会

我的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.example.demo.mapper.ManhuaMapper">

    <!--根据queryfirend来查询全部-->
    <select id="querymanhua"  resultMap="manhuacategoryMap" >
        select  * from manhua a,manhuacategory b WHERE a.manhuacategoryid=b.id
    </select>
    <!--resultMap中的type表示返回什么数据类型的对象-->
    <resultMap id="manhuacategoryMap" type="com.example.demo.pojo.Manhua">
        <!--主键字段-->
        <!--其中property代表com.example.demo.pojo.Good中的属性名,column代表数据表中的列名-->
        <id property="manhuaid" column="manhuaid"></id>
        <!--非主键字段-->
        <result property="manhuapicpath" column="manhuapicpath"/>
        <result property="manhuaname" column="manhuaname"/>
        <result property="writername" column="writername"/>
        <result property="manhuastyle" column="manhuastyle"/>
        <result property="manhuastatus" column="manhuastatus"/>
        <result property="collection" column="collection"/>
        <result property="updatetime" column="updatetime"/>
        <result property="introduce" column="introduce"/>
        <result property="picid" column="picid"/>
        <result property="picowner" column="picowner"/>

        <!--关联字段,这里一对一关联-->
        <association property="manhuacategory" javaType="com.example.demo.pojo.ManhuaCategory">
            <result property="id" column="id"/>
            <result property="name" column="name"/>
        </association>
    </resultMap>

        <!--根据querymanhuabyid来查询-->
    <select id="querymanhuaById" parameterType="Integer" resultMap="manhuacategoryMap">
        select * from manhua where manhuaid=#{manhuaid};
    </select>
    <!--根据deletemanhua id删除-->
    <delete id="deletemanhua">
        delete from manhua where manhuaid=#{manhuaid};
    </delete>
    <!--    addmanhua来增加漫画-->
    <insert id="addmanhua">
        insert into manhua(manhuapicpath,manhuaname,writername,manhuacategoryid,manhuastyle,manhuastatus,collection,updatetime,introduce,picid,picowner) values
        (#{manhua.manhuapicpath},#{manhua.manhuaname},#{manhua.writername},#{manhuacategoryid},#{manhua.manhuastyle},#{manhua.manhuastatus},#{manhua.collection}
        ,#{manhua.updatetime},#{manhua.introduce},#{manhua.picid},#{manhua.picowner});
    </insert>
<!--    &lt;!&ndash;修改友情链接&ndash;&gt;-->
<!--    <update id="updatefriend">-->
<!--        update friend set wangzhanmingchen=#{friend.wangzhanmingchen},wangzhandizhi=#{friend.wangzhandizhi} where id=#{id};-->
<!--    </update>-->


</mapper>

实体类两个

package com.example.demo.pojo;


import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.sql.Date;


@AllArgsConstructor
@NoArgsConstructor
@Data
public class Manhua implements Serializable {
    private Integer manhuaid;
    private String manhuaname;
    private ManhuaCategory manhuacategory;
    private String manhuastyle;
    private String manhuastatus;
    private String updatetime;
    private Integer collection;
    private String manhuapicpath;
    private String writername;
    private String introduce;
    private Integer picid;
    private String picowner;


    private static final long serialVersionUID = 8990798455744465533L;

}
package com.example.demo.pojo;


import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@AllArgsConstructor
@NoArgsConstructor
@Data
public class ManhuaCategory {
    private Integer id;
    private String name;
}

mapper接口两个

package com.example.demo.mapper;

import com.example.demo.pojo.ManhuaCategory;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;

import java.util.List;
@Component
public interface ManhuaCategoryMapper {
    @Select("select id,name from manhuacategory")
    List<ManhuaCategory> queryCategories();
}
package com.example.demo.mapper;



import com.example.demo.pojo.Friend;
import com.example.demo.pojo.Img;
import com.example.demo.pojo.Manhua;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.List;
@Component
@Mapper
public interface ManhuaMapper {
    //1.查询所有漫画
    List<Manhua> querymanhua();
    //2.根据id查询漫画
    Manhua querymanhuaById(Integer mahuaid);
    //3.删除漫画
    int deletemanhua(Integer manhuaid);
    //4.漫画增加
     int addmanhua(@Param("manhua") Manhua manhua, @Param("manhuacategoryid") Integer manhuacategoryid);
}

dao两个

package com.example.demo.dao;


import com.example.demo.mapper.ManhuaMapper;
import com.example.demo.pojo.Manhua;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
@Component
public class ManhuaDao {
    @Autowired
    ManhuaMapper manhuaMapper;

    //1.查询全部漫画
    public List<Manhua> getManhuas() {
        System.out.println(manhuaMapper.querymanhua());
        return manhuaMapper.querymanhua();
    }
    //2.通过id查询漫画
    public Manhua getManhuaById(Integer manhuaid) {
        return manhuaMapper.querymanhuaById(manhuaid);
    }
    //3.通过id删除漫画
    public void remove(Integer manhuaid){
        manhuaMapper.deletemanhua(manhuaid);
    }
    //3.增加一个漫画
    public void add(Manhua manhua) {
        manhuaMapper.addmanhua(manhua,manhua.getManhuacategory().getId());
    }
}
package com.example.demo.dao;

import com.example.demo.mapper.ManhuaCategoryMapper;
import com.example.demo.pojo.ManhuaCategory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collection;

@Component
public class ManhuaCategoryDao {
    @Autowired
    ManhuaCategoryMapper manhuaCategoryMapper;

    public Collection<ManhuaCategory> getCategories() {
        //return categories.values();
        return manhuaCategoryMapper.queryCategories();
    }
}

controller

package com.example.demo.controller;


import com.example.demo.dao.ManhuaCategoryDao;
import com.example.demo.dao.ManhuaDao;

import com.example.demo.mapper.ManhuaMapper;
import com.example.demo.pojo.Manhua;
import com.example.demo.pojo.ManhuaCategory;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Controller
public class ManhuaController {
    private final ManhuaMapper manhuaMapper;
    @Autowired
    ManhuaDao manhuaDao ;
    @Autowired
    ManhuaCategoryDao manhuaCategoryDao;

    public ManhuaController(ManhuaMapper manhuaMapper) {
        this.manhuaMapper = manhuaMapper;
    }

    //查询
    @RequestMapping("/manhuas")
    public String list(Model model) {
        Collection<Manhua> manhuas = manhuaDao.getManhuas();
        model.addAttribute("manhuas", manhuas);
        return "/manhuamage";

    }
    //删除
    @GetMapping("/delmanhua/{manhuaid}")
    public String deletemanhua(
            @PathVariable("manhuaid") Integer manhuaid
    ){
        //查出原来选中的那条数据
        manhuaDao.remove(manhuaid);
        return "redirect:/manhuas";
    }
    //增加
    @RequestMapping("/addmanhua")
    public String toAddPage(Model model) {
        Collection<ManhuaCategory> manhuacategories = manhuaCategoryDao.getCategories();
        model.addAttribute("manhuacategories",manhuacategories);
        return "/addmanhua";
    }

    @PostMapping("/addmanhua")
    public String addFriend(Manhua manhua) {
        System.out.println(manhua);
        manhuaDao.add(manhua);
        return "redirect:/manhuas";
    }



}
  • 点赞
  • 收藏
  • 复制链接分享

6条回答

  • qq_43046055 Scofield Lee 4月前

    图片上传可以用spring Multiplefile对象接收,通过该对象拿到流对象,然后通过流操作写到本地。数据库只是保存上传后文件的存储路径。也可以使用文件服务器,像阿里云OSS等等,最终你需要保存了只是图片的存放路径,而不是图片本身。

    点赞 1 评论 复制链接分享
  • qq_31122833 小达哥的垃圾桶 4月前

    弄一个文件服务器,把文件上传到文件服务器--返回文件访问地址,把访问地址保存进数据库

    点赞 1 评论 复制链接分享
  • qq_31122833 小达哥的垃圾桶 4月前

    前端通过访问文件地址,获取文件

    点赞 评论 复制链接分享
  • weixin_42607427 是以筠呀 4月前

    是的

    点赞 评论 复制链接分享
  • weixin_42607427 是以筠呀 4月前

    🉑️

    点赞 评论 复制链接分享
  • weixin_42607427 是以筠呀 4月前
    • 发布风格
    点赞 评论 复制链接分享

相关推荐