1、参考之前的案例,该案例中已经实现了mysql数据库的数据信息的单条插入和查询功能
2、实现数据信息的插入功能在VideoInfoMapper类中增加侑喏嵋甲插入数据的功能函数package image.dao;import or爿讥旌护g.apache.ibatis.annotations.Insert;import org.apache.ibatis.annotations.Mapper;import org.apache.ibatis.annotations.Param;import org.apache.ibatis.annotations.Select;@Mapperpublic interface VideoInfoMapper { @Select("select * from Video_info where id=#{id}") VideoInfo findById(@Param("id") int id); @Insert("insert into video_info(fileName,filePath,author,createDate) values(#{fileName},#{filePath},#{author},sysdate())") int insertVideoInfo(@Param("fileName") String fileName,@Param("filePath") String filePath,@Param("author") String author); }
3、在VideoService类中增加数据插入的函数insertVideoInfo,需要使用到注解@Transactionalpackage image.dao;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;@Service("videoService")public class VideoService {//自动绑定mapper@Autowiredprivate VideoInfoMapper videoInfoMapper;//查询数据public VideoInfo getVideoInfo(int id){return videoInfoMapper.findById(id);}//插入数据@Transactionalpublic int insertVideoInfo(String fileName,String filePath,String author){return videoInfoMapper.insertVideoInfo(fileName, filePath, author);}}
4、在VideoInfoController 控制器类中,增加数据插入的Restful web service服务接口insertVideo:/video/addpa艘早祓胂ckage image;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;import image.dao.VideoInfo;import image.dao.VideoService;@RestController@RequestMapping("/video")public class VideoInfoController { final static Logger logger = LoggerFactory.getLogger(VideoInfoController.class); @Autowired private VideoService videoService; @RequestMapping("/info/{id}") public VideoInfo getVideoInfo(@PathVariable("id") int id) { return videoService.getVideoInfo(id); } @RequestMapping("/add") public int insertVideo(@RequestParam String fileName,@RequestParam String filePath,@RequestParam String author) { int res =-1; res=videoService.insertVideoInfo(fileName, filePath, author); return res; } }
5、在FileUploadController控制器中修改handleFileUploadVideo方法,在上传文件时调用/video/add restful服务接口 @PostMapping("/uploadVideo") @ResponseBody public String handleFileUploadVideo(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes) { System.out.print("handleFileUploadVideo>>>>>>>>>>>"); storageService.store(file); // String postUrl = "http://localhost:8080/addVideo"; String postUrl = "http://localhost:8080/video/add"; MultiValueMap<String,Object> request =new LinkedMultiValueMap<String,Object>(); String filename = StringUtils.cleanPath(file.getOriginalFilename()); request.add("fileName",filename); request.add("filePath","/img/"+filename); request.add("author","fantasy"); RestTemplate restTemplate = new RestTemplate(); ResponseEntity<String> rst = restTemplate.postForEntity(postUrl, request, String.class); System.out.println("Rest result:"+rst); return file.getOriginalFilename(); } @PostMapping("/uploadVideo") @ResponseBody public String handleFileUploadVideo(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes) { System.out.print("handleFileUploadVideo>>>>>>>>>>>"); storageService.store(file); // String postUrl = "http://localhost:8080/addVideo"; String postUrl = "http://localhost:8080/video/add"; MultiValueMap<String,Object> request =new LinkedMultiValueMap<String,Object>(); String filename = StringUtils.cleanPath(file.getOriginalFilename()); request.add("fileName",filename); request.add("filePath","/img/"+filename); request.add("author","fantasy"); RestTemplate restTemplate = new RestTemplate(); ResponseEntity<String> rst = restTemplate.postForEntity(postUrl, request, String.class); System.out.println("Rest result:"+rst); return file.getOriginalFilename(); }
6、在页面中端到端实现视频信息的入库、修改和删除等操作1) 在页面中通过http://localhost:8080/uploadVideo 页面进行视频文件上传2)视频文件上传完成后,到mysql数据库中查询video_info表,验证数据是否插入到数据库中