实体类
@Data @NoArgsConstructor @AllArgsConstructor public class Keywords { private Integer id; private String keyword; }
所需要的vo类
@Data @AllArgsConstructor @NoArgsConstructor public class KeywordsVo { private String keyword; private Integer number; }
@Data @AllArgsConstructor @NoArgsConstructor public class KeywordsVo2 { private Integer code; private String msg; private Integer count; private List<KeywordsVo> data; }
mapper层
<?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.gazikel.cvpr.mapper.KeywordsMapper"> <insert id="insertKeyword"> insert into keywords(keyword) values (#{keyword}) </insert> <select id="getKeywordsTop" parameterType="integer" resultType="com.gazikel.cvpr.vo.KeywordsVo"> SELECT keyword, count(keyword) as number FROM keywords GROUP BY keyword ORDER BY count(keyword) DESC limit #{top} </select> </mapper>
service层
package com.gazikel.cvpr.service; import com.gazikel.cvpr.vo.KeywordsVo; import java.util.List; public interface KeywordsService { // 根据论文name 读取关键词 public void getKeyWords(String name); // 选出前几名的热词 public List<KeywordsVo> getKeywordsTop(Integer top); }
serviceImpl
package com.gazikel.cvpr.service.impl; import com.gazikel.cvpr.mapper.KeywordsMapper; import com.gazikel.cvpr.pojo.Keywords; import com.gazikel.cvpr.service.KeywordsService; import com.gazikel.cvpr.vo.KeywordsVo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class KeywordsServiceImpl implements KeywordsService { @Autowired private KeywordsMapper keywordsMapper; @Override public void getKeyWords(String name) { String[] split = name.split("\s"); for (int i=0; i < split.length; i++) { int flag = keywordsMapper.insertKeyword(split[i]); } } @Override public List<KeywordsVo> getKeywordsTop(Integer top) { return keywordsMapper.getKeywordsTop(top); } }
controller层
package com.gazikel.cvpr.controller; import com.gazikel.cvpr.pojo.Keywords; import com.gazikel.cvpr.service.KeywordsService; import com.gazikel.cvpr.vo.KeywordsVo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import java.util.HashMap; import java.util.List; import java.util.Map; @Controller public class KeywordsController { @Autowired KeywordsService keywordsService; @RequestMapping("/getKeywords") @ResponseBody public Map<String, Integer> getKeywords(@RequestParam("number") Integer number) { Map<String, Integer> map = new HashMap<>(); List<KeywordsVo> keywordsTop = keywordsService.getKeywordsTop(number); for (KeywordsVo keyword : keywordsTop) { // System.out.println(keyword); map.put(keyword.getKeyword(), keyword.getNumber()); } return map; } }