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之类的地方,仔细点。