记:Spring boot结合Mybatis plus上传文件到MySQL数据库的坑

1.文件转为字节数组:

public String upload(@RequestParam("file") MultipartFile file) {

    String fileName = file.getOriginalFilename().substring(0, file.getName().lastIndexOf("."));

    byte[] bytes = null;
    try {
        bytes = file.getBytes();
    } catch (Exception e) {
        e.printStackTrace();
    } 

    pictures.setName(fileName);
    pictures.setImages(bytes);
    int result = picturesService.insertPic(pictures);

    return "success";
}

2.映射文件:

需引入typeHandler.

<?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.aiqiji.lingduanwang.mapper.PicturesMapper">

    <resultMap id="BaseResultMap" type="com.aiqiji.lingduanwang.pojo.Pictures">
        <id column="name" property="name"/>
        <result column="images" property="images" typeHandler="com.aiqiji.lingduanwang.utils.CommentTypeHandler"/>
    </resultMap>

    <sql id="Base_Column_List">
        name,images
    </sql>
    <insert id="insert" parameterType="com.aiqiji.lingduanwang.pojo.Pictures">
        insert into pictures(name,images)
        values (#{name,jdbcType= VARCHAR},
                #{images,jdbcType=BLOB,typeHandler=com.aiqiji.lingduanwang.utils.CommentTypeHandler};
    </insert>
</mapper>

3.依赖:

不用mybatis-spring-boot-starter,用苞米豆的

<!--<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.4</version>
</dependency>-->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.4.2</version>
</dependency>

4.其他:

细心点,类似namespace,mapper-locations之类的地方,仔细点。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值