比如班级和学生就是一对多,查就是按照学生表去查询
多对多,三张表,关系表加外键
@Api(tags = "购物车Api接口") | 用于为API或API的某个部分提供一个标签或分类 |
@ApiOperation("新增购物车记录") | 用于为API操作提供一个简短的描述或标签 |
@ApiImplicitParams({ @ApiImplicitParam(name = "page", value = "页码", defaultValue = "1"), @ApiImplicitParam(name = "size", value = "条数", defaultValue = "2"), @ApiImplicitParam(name = "userId", value = "用户id") }) |
用于为API参数提供更多元数据和描述集合。 |
@ApiImplicitParam(name = "page", value = "页码", defaultValue = "1") | 用于为API参数提供更多元数据和描述 |
@ApiModel("回收类") | 用于为API模型或模型类提供一个描述或标签 |
@ApiModelProperty("商品名称") | 描述API模型属性的元数据 |
value | 用于提供模型属性的描述。例如,@ApiModelProperty(value = "商品名称") |
required | 用于指定模型属性是否必需。如果设置为 true ,则该属性是必需的。如果设置为 false ,则该属性是可选的。例如,@ApiModelProperty(value = "商品描述", required = true) |
example | 用于提供模型属性的示例值。这对于展示如何使用该属性非常有用。例如,@ApiModelProperty(value = "商品ID", example = "123456") |
notes | 用于提供关于模型属性的额外说明或备注。例如,@ApiModelProperty(value = "商品ID", notes = "请使用唯一的商品ID") |
dataType | 用于指定模型属性的数据类型。例如,@ApiModelProperty(value = "商品价格", dataType = "double") |
普通参数 | 直接传(或json) |
请求参数名与形参变量名不匹配 | 使用@RequestParam绑定参数关系 |
实体类参数 | 直接传属性(非json) |
嵌套pojo对象 | 按照对象层次结构关系接收 address.city |
数组参数 | key相同这样去传递(k1 值1;k2 值2) |
集合保存普通参数 | 使用@RequestParam(属性注解)绑定参数关系,其他跟数组一样 |
注意:不能传json数据
http://localhost:8080/api/doc.html#/home
自动配置
@GetMapping("/test1")
public String testDate(@DateTimeFormat(pattern = "HH:mm:ss") Date createDate) {
return "接受客服端String类型的时间类型 转化Date类型";
}
@Data
public class UserQo {
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date createDate;
}
@PostMapping("/test2")
public String testDateJson(@RequestBody UserQo userQo) {
System.out.println(userQo.getCreateDate().toString());
return "Json时间处理";
}
统一时间json方式
@Controller
@RequestMapping("/upload")
public class UploadController {
@RequestMapping(value = "/image", method = RequestMethod.POST)
@ResponseBody
public String uploadImage(@RequestParam("file") MultipartFile file) {
String fileName = file.getOriginalFilename();
String path = "D:/uploads/" + fileName;
try {
File dest = new File(path);
if (!dest.getParentFile().exists()) {
dest.getParentFile().mkdirs();
}
file.transferTo(dest);
return "success";
} catch (Exception e) {
e.printStackTrace();
return "error";
}
}
}
在控制层中,我们创建一个名为UploadController的类来处理上传请求。在该类中,我们需要添加一个方法,该方法用于处理上传请求并返回上传结果。在该方法中,我们需要使用SpringMVC提供的 MultipartResolver 类来解析上传的文件,获取文件信息,并将文件保存到服务器上
@Controller
@RequestMapping("/download")
public class DownloadController {
@RequestMapping(value = "/image/{id}", method = RequestMethod.GET)
public ResponseEntity downloadImage(@PathVariable("id") int id) {
Image image = imageService.getImageById(id);
String fileName = image.getName();
String path = "D:/uploads/" + fileName;
try {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
headers.setContentDispositionFormData("attachment", fileName);
File file = new File(path);
return new ResponseEntity(FileUtils.readFileToByteArray(file), headers, HttpStatus.CREATED);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
我们使用了 imageService.getImageById(id) 方法来获取要下载的图片的信息,这个方法可以自己实现或借助MyBatis框架读取数据库获取数据。
其中,id 为图片在数据库中的对应主键,response 为响应对象,conn 为数据库连接对象。通过操作 ResultSet 对象可以得到图片数据的输入流。
CREATE TABLE tb_image(
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
`name` varchar(255) NOT NULL COMMENT '文件名',
`image` longblob NOT NULL COMMENT '图片数据',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='图片表';
public void addImage(String name, String imagePath) {
try {
FileInputStream fis = new FileInputStream(imagePath);
PreparedStatement pstmt = conn.prepareStatement("INSERT INTO tb_image(name,image) VALUES(?,?)");
pstmt.setString(1, name); // 设置文件名
pstmt.setBinaryStream(2, fis, fis.available()); // 设置图片数据
pstmt.executeUpdate();
pstmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void getImage(int id, HttpServletResponse response) {
try {
PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM tb_image WHERE id=?");
pstmt.setInt(1, id);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
String name = rs.getString("name");
Blob imageBlob = rs.getBlob("image");
InputStream is = imageBlob.getBinaryStream();
ServletOutputStream out = response.getOutputStream();
response.setContentType("image/jpeg");
response.setHeader("Content-Disposition", "attachment;filename=" + name);
byte[] b = new byte[1024];
int len;
while ((len = is.read(b)) > 0) {
out.write(b, 0, len);
}
is.close();
out.flush();
out.close();
}
rs.close();
pstmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}
ResponseResult
对象,其中包含新生成的文件名。@PostMapping("/image/upload")
@ApiOperation("上传车辆信息相关图片")
@ApiResponses({@ApiResponse(code = 2000, message = "成功", response = String.class), @ApiResponse(code = 4000, message = "未知错误)")})
public ResponseResult upload(@RequestBody MultipartFile pic,HttpServletRequest request){
String originalFilename = pic.getOriginalFilename();
String realPath=request.getServletContext().getRealPath("WEB-INF/upload/carInfoImg");
String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
String fileName = UUID.randomUUID().toString()+substring;
File dir = new File(realPath);
if (!dir.exists()){
dir.mkdirs();
}
try {
pic.transferTo(new File(realPath,fileName));
} catch (IOException e) {
e.printStackTrace();
}
return ResponseResult.success(fileName);
}
FileInputStream
来读取该文件。 @GetMapping("/image/download")
@ApiOperation("图片下载回显")
@ApiResponses({@ApiResponse(code = 2000, message = "成功", response =File.class), @ApiResponse(code = 4000, message = "未知错误)")})
public void download(String pname, HttpServletResponse response,HttpServletRequest request){
try {
String realPath=request.getServletContext().getRealPath("WEB-INF/upload/carInfoImg/");
//获取图片文件
FileInputStream inputStream = new FileInputStream(new File(realPath+pname));
//输出流
ServletOutputStream outputStream = response.getOutputStream();
response.setContentType("image/jpeg");
int len = 0;
byte[] bytes = new byte[1024];
while ((len = inputStream.read(bytes)) != -1){
outputStream.write(bytes,0,len);
outputStream.flush();
}
outputStream.close();
inputStream.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
$(function(
$("#uploadBtn").click(function(){
// 获取上传文件
var uploadPhoto = $("#upload")[0].files[0];
// 利用FormDate对象封装文件数据
var formdata = new FormData();
formdata.append("uploadPhoto",uploadPhoto);
$.ajax({
url:'regUpload',
type:'post',
processData:false,
contentType:false,//设置false,将请求数据类型设置为multipart/form-data
data:formdata,
success:function(data){
if(data.result){
alert("上传成功!");
$("#imageSpan").html("");
$("#imgPath").val(data.msg);
}else{
alert("上传失败!原因:"+data.msg);
}
}
});
});
));
@RequestMapping("regUpload")
@ResponseBody
public Result regUpload(MultipartFile uploadPhoto, HttpServletRequest request) throws IOException {
// 业务处理
// 1、获取文件在服务器存储中的实际路径
String realPath = request.getServletContext().getRealPath("/uploadImage/");
File pathFile = new File(realPath);
if(!pathFile.exists()){
pathFile.mkdirs();
}
// 2、获取文件原始文件名,随机生成文件名
String originalFilename = uploadPhoto.getOriginalFilename();
String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
String fileName = UUID.randomUUID()+suffix;
// 3、实际路径拼接随机文件名,将文件存储至目标路径
uploadPhoto.transferTo(new File(realPath+fileName));
// 4、存储文件的原始名、随机名、文件类型至数据库
String contentType = uploadPhoto.getContentType();
PhotoImage photoImage = new PhotoImage("/uploadImage/"+fileName, originalFilename, fileName, contentType );
System.out.println("============================");
int flag = 0 ;
try{
flag =photoImageService.insertImage(photoImage);
}catch (Exception e){
e.printStackTrace();
}
if(flag==1){
// 5、返回上传结果、存储路径+文件名
Result result = new Result(true,"uploadImage/"+fileName);
return result;
}else{
return new Result(false,"图片存储失败!");
}
}
@RequestMapping("downloadFile")
public void downloadFile(String filename,HttpServletRequest req, HttpServletResponse resp) throws IOException {
// Step2 后台接收请求,先设置响应头,表明为下载请求
resp.setHeader("Content-Disposition", "attachment;filename="+filename);
// Step3 获取文件的在硬盘上的绝对路径
String realPath = req.getServletContext().getRealPath("/uploadImage/");
// Step4 利用FileUtils将文件转成byte数组
File file = new File(realPath,filename);
byte[] bytes = FileUtils.readFileToByteArray(file);
// Step5 从相应对象中获取输出流,将byte数组写出
ServletOutputStream os = resp.getOutputStream();
os.write(bytes);
// Step6 清除输出流的缓存、关闭输出流
os.flush();
os.close();
}
AND d.city LIKE CONCAT('%', #{user.userDetail.city}, '%')
total看总条数,用user还是List