多语言展示
当前在线:716今日阅读:60今日分享:41

Spring boot+mybatis+mysql实现数据批量查询

我们在构建WEB应用时,往往是需要查询后台数据库的表记录,查询按返回结果类型可以大概分为单条记录和多条记录返回,这里我们结合Spring boot、mybatis和MySQL实现数据库的的批量查询实践。
工具/原料
1

Eclipse

2

Spring boot

3

MySQL

方法/步骤
1

我们选择之前的一篇案例为基础进行该案例的深入实践如引用的案例经验是实现的功能:在页面中选择视频文件mp4然后上传,服务器会将视频文件保存并在mysql数据库中插入一条视频信息记录。

2

为了测试方便,我么先使用页面上传15个以上的视频,这样在mysql中就存在15条以上的记录,便于我们进行展示Spring boot+mybatis批量查询的效果实践。

3

在Mybatis的mapper接口类中,完成三种数据批量查询的接口:1)List返回值方式2)List>返回值方式3)分页查询方式 @Select('select * from video_info') List getVideoInfo(); @Select('select * from video_info') List> getVideoList(); @Select('select * from video_info limit #{startIndex},#{endIndex}') List> getVideoListByPage(@Param('startIndex') int startIndex,@Param('endIndex') int endIndex);

4

在DAO Service服务类VideoService中提供以下三个查询函数 //查询表所有数据 public List getVideoInfoList() { return videoInfoMapper.getVideoInfo(); } //查询表所有数据 public List> getVideoInfoListMap() { return videoInfoMapper.getVideoList(); } //分页查询数据 public List> getVideoInfoListByPage(int startIndex,int endIndex) { return videoInfoMapper.getVideoListByPage(startIndex, endIndex); }

5

在视频Restful Web Service服务类VideoInfoController中提供3中方式的数据查询API接口:1) /video/list2)/video/listmap3)/video/listpage/{startIndex}/{endIndex} @RequestMapping('list') public List getVideoInfoList() { return videoService.getVideoInfoList(); } @RequestMapping('listmap') public List> getVideoInfoListMap() { return videoService.getVideoInfoListMap(); } @RequestMapping('listpage/{startIndex}/{endIndex}') public List> getVideoInfoListByPage(@PathVariable('startIndex') int startIndex,@PathVariable('endIndex') int endIndex) { return videoService.getVideoInfoListByPage(startIndex, endIndex); }

6

在Video视频控制器类中提供页面跳转功能链接,增加两种链接1)/home 跳转到home.html页面,数据查询结果使用List返回2)/home2跳转到home.html页面,数据查询结果使用List>返回3) /home3跳转到home.html页面,采用分页查询方式返回List>对象 @RequestMapping('/home') public String getHomePage(Model model) { RestTemplate restTemplate = new RestTemplate(); String url='http://localhost:8080/video/list'; List videoList=restTemplate.getForObject(url, List.class); model.addAttribute('files', videoList);   return 'home'; } @RequestMapping('/home2') public String getHomePage2(Model model) { RestTemplate restTemplate = new RestTemplate(); String url='http://localhost:8080/video/listmap'; List> videoList=restTemplate.getForObject(url, List.class); model.addAttribute('files', videoList);   return 'home'; } @RequestMapping('/home3') public String getHomePage3(Model model,@RequestParam('startIndex') int startIndex,@RequestParam('endIndex') int endIndex) { RestTemplate restTemplate = new RestTemplate(); String url='http://localhost:8080/video/listpage/'+startIndex+'/'+endIndex; List> videoList=restTemplate.getForObject(url, List.class); model.addAttribute('files', videoList);   return 'home'; }

7

在template目录下创建home.html页面,将查询结果显示在页面中:                      视频文件列表       

 

推荐信息