优化获取上一条和下一条记录的方式。
我做了一个实现,我获得了前一个记录和下一个记录,但效率不高。当我们进入日志详细信息屏幕时,在这种情况下是一个玩家,整个搜索所有记录只检查下一个和前一个。
在这种情况下,搜索仅适用于设备,但可以通过具有过滤器的搜索引擎来获得详细信息,其中获取分页结果。如何改进上一张和下一张唱片的实现? 我想要的是不必检索所有记录,只检索您需要的记录。 这是我的实施:
我的控制器:
@Controller
public class PlayerController {
@Autowired
private PlayerService pService;
@RequestMapping(value="/players/{id}", method = RequestMethod.GET)
public final ModelAndView printPlayerDetail(@PathVariable(value = "id") Integer id, Locale locale) throws ControllerException
{
ModelAndView view = new ModelAndView("players/detail");
Player p = null;
NavigatorDTO navigator = new NavigatorDTO();
try {
p = pService.getById(id);
navigator.setId(p.getId());
navigator.setRecords(pService.getNavigator(p.getTeam().getId()));
} catch (ServiceException e) {
throw new ControllerException(this.messages.getMessage(ERROR_QUERY, null, locale), e);
}
PlayerDTO dto = new PlayerDTO();
dto.setId(p.getId());
dto.setName(p.getName());
if (p.getTeam() != null) {
dto.setTeam(p.getTeam().getId());
}
view.addObject("navigator", navigator);
view.addObject("player", dto);
return view;
}
}
我的服务:
@Service
public class PlayerServiceImpl implements PlayerService {
@Autowired
private PlayerDao pDao;
@Override
@Transactional
public List<Integer> getNavigator(Integer teamId) throws ServiceException {
List<Integer> result = new ArrayList<Integer>();
try {
List<Player> players = pDao.findByTeanm(teamId);
for (Player p : players) {
result.add(p.getId());
}
} catch (FacadeException e) {
throw new ServiceException(e);
}
return result;
}
}
我的导航类:
public final class NavigatorDTO implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private List<Integer> records = new ArrayList<Integer>();
public Integer getId() {
return id;
}
public void setId(final Integer id) {
this.id = id;
}
public List<Integer> getRecords() {
return records;
}
public void setRecords(final List<Integer> records) {
this.records = records;
}
/**
* Get next id
* @return
*/
public Integer getNext() {
if ( id!=null ) {
Integer actualPosition = records.indexOf(id);
try {
return records.get(actualPosition + 1);
} catch (Exception e) {
return null;
}
} else {
return null;
}
}
/**
* Get previous id
* @return
*/
public Integer getPrevious() {
if (id != null){
Integer actualPosition = records.indexOf(id);
try {
return records.get(actualPosition - 1);
} catch (Exception e) {
return null;
}
} else {
return null;
}
}
/**
* Get first id
* @return
*/
public Integer getFirst(){
if (id != null) {
try {
return records.get(0);
} catch (Exception e) {
return null;
}
} else {
return null;
}
}
/**
* Get last id
* @return
*/
public Integer getLast(){
if (id != null) {
try{
return records.get(records.size() - 1);
} catch (Exception e){
return null;
}
} else {
return null;
}
}
/**
* Get total records
* @return Total
*/
public int getTotalrecords(){
return (records == null) ? 1 : records.size();
}
/**
* Get actual position
* @return
*/
public int getActualPosition(){
return (records == null) ? 1 : records.indexOf(id) + 1;
}
}
答案 0 :(得分:1)
OFFSET分页,这可能就是你正在做的本质上很慢。
请参阅http://use-the-index-luke.com/sql/partial-results/fetch-next-page
不幸的是,JPA并不真正支持密钥集分页(我认为)。所以你必须使用原始SQL。