1、数据库建表语句:其中红色部分为存储图片的字段;
CREATE TABLE `show_picture` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
`type` varchar(20) DEFAULT NULL COMMENT '类型,作品:WORK ;客片:GUEST',
`title` varchar(20) DEFAULT NULL COMMENT '标题',
`description` varchar(200) DEFAULT NULL COMMENT '描述',
`style_type` int(11) DEFAULT NULL COMMENT '对应风格分类',
`place_type` int(11) DEFAULT NULL COMMENT '对应场景分类',
`enjoy_type` int(11) DEFAULT NULL COMMENT '对应欣赏分类',
`picture` longblob COMMENT '对应图片',
`for_first_pic` longblob COMMENT '对应首页图片(只对服务报价推荐到首页有效)',
`for_related_pic` longblob COMMENT '对应相关作品详细页面图片',
`create_time` datetime DEFAULT NULL COMMENT '创建时间',
`sort` int(11) DEFAULT '0' COMMENT '排序',
`var_temp` varchar(20) DEFAULT NULL COMMENT '字符型备用字段',
`int_temp` int(11) DEFAULT NULL COMMENT '数字型备用字段',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
2、自动生成实体类
public class ShowPicture {
private Integer id;
private String type;
private String title;
private String description;
private Integer styleType;
private Integer placeType;
private Integer enjoyType;
private Date createTime;
private Integer sort;
private String varTemp;
private Integer intTemp;
}
这是基本字段,具体存储图片的为blog类型,自动生成代码时,blog类型的字段会自动生成子类继承基本类
public class ShowPictureWithBLOBs extends ShowPicture {
private byte[] picture;
private byte[] forFirstPic;
private byte[] forRelatedPic;
}
set、get方法自己完善;
3、因为前台传到controller中的附件要以MultipartFile类型,所以设置一个同样继承基本类的子类,和ShowPictureWithBLOBs字段一样,字段类型为MultipartFile,具体如下:
public class ShowPictureWithBLOBsUseMultipartFile extends ShowPicture {
private MultipartFile pictureFile;
private MultipartFile forFirstPicFile;
private MultipartFile forRelatedPicFile;
}
4、前台图片上传控件
其中,要注意的是form表单类型要multipart类型,如下