本文介绍了MyBatis SelectList输出CopyOnWriteArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请耐心等待新手问题,因为我想同时学习MyBatis和java。我有一个应用程序,我需要使用线程安全的变量。基于一些研究和我如何使用应用程序的想法,我已经在一个Vector上安装了一个CopyOnWriteArrayList。



当我从mybatis sql中调用selectList会话,有没有办法告诉它创建一个CopyOnWriteArrayList作为它的返回而不是一个ArrayList?不可否认,我的代码配置这是两行,而不是一个,但在我的一些东西说,必须有一个更好的方法和/或我不是第一个遇到这个。

 列表< Team> teams = session.selectList(getTeamsByGameID,gameID); 
List< Team> arrayListReturn = new CopyOnWriteArrayList< Team>(teams);
return arrayListReturn;

提前感谢,

方案

我知道有两种处理方式。



选项1 :使用Mapper类并指定类型

 定义一个Mapper接口。 public interface TeamMapper {
CopyOnWriteArrayList< Team> getTeamsByGameID();
}

您的mapper xml文件保持不变。执行查询的代码更改为:

  TeamMapper m = session.getMapper(TeamMapper.class); 
List< Team> lt = m.getTeamsByGameID();
System.out.println(lt.getClass());
// =>打印出class java.util.concurrent.CopyOnWriteArrayList



选项2 :创建一个ResultHandler并将其传递到 session.select()方法中。



这里使用ResultHandler接口。该接口需要你重写一个方法, handleResult ,它给出了每个结果从数据库返回的查询正在进行。



在你的case,你的ResultHandler看起来像这样:

  public class TeamResultHandler implements ResultHandler {

私人列表< Team> teams = new CopyOnWriteArrayList< Team>();

@Override
public void handleResult(ResultContext rc){
countries.add((Team)rc.getResultObject());
}

//提供getter,以便在完成时可以检索
public List< Team> getTeamList(){
return teams;
}
}

而不是使用 selectList ,如上所示,现在将使用 session.select(String,ResultHandler),如下所示:

  TeamResultHandler rh = new TeamResultHandler(); 
session.select(getTeamsByGameID,rh);
List< Team> lt = rh.getTeamList();
return lt;

这个解决方案比你的解决方案更冗长(在查询代码中需要额外的类和三行)而不是2),但它只创建一个List,而不是两个,所以你必须决定最适合你的需求。



此外,ResultHandlers可以有用的额外的东西 - 确保结果以某种方式排序或过滤或其他,如果你需要的话。


Please be patient with the newbie question as I'm trying to learn MyBatis and java at the same time. I have an application in which I need to use threadsafe variables. Based on some research and my ideas of how the application will be used, I've settled on a CopyOnWriteArrayList over a Vector.

When I call a selectList from the mybatis sql session, is there any way to tell it to create an CopyOnWriteArrayList as its return rather than an ArrayList? Admittedly, my code to configure this is two lines instead of one, but something in me says that there's got to be a better way and/or I'm not the first one to have encountered this.

List<Team> teams = session.selectList("getTeamsByGameID", gameID);
List<Team> arrayListReturn = new CopyOnWriteArrayList<Team>(teams);
return arrayListReturn;

Thanks in advance,

解决方案

I know of two ways to handle this.

Option 1: Use a Mapper class and specify the type of List to return there.

Define a Mapper interface:

public interface TeamMapper {
  CopyOnWriteArrayList<Team> getTeamsByGameID();
}

Your mapper xml file stays the same. The code to do the query changes to:

TeamMapper m = session.getMapper(TeamMapper.class);
List<Team> lt = m.getTeamsByGameID();
System.out.println(lt.getClass());
  //=> prints out "class java.util.concurrent.CopyOnWriteArrayList"


Option 2: Create a ResultHandler and pass that into the session.select() method.

Here you use the ResultHandler interface. That interface requires you to override one method, handleResult, which is given each result that comes back from the database as the query is in progress.

In your case, your ResultHandler would look something like this:

public class TeamResultHandler implements ResultHandler {

  private List<Team> teams = new CopyOnWriteArrayList<Team>();

  @Override
  public void handleResult(ResultContext rc) {
    countries.add((Team) rc.getResultObject());
  }

  // provide a getter so you can retrieve it when finished
  public List<Team> getTeamList() {
    return teams;
  }
}

Instead of using selectList as you do above, you would now use the session.select(String, ResultHandler), like so:

TeamResultHandler rh = new TeamResultHandler();
session.select("getTeamsByGameID", rh);
List<Team> lt = rh.getTeamList();
return lt;

This solution is more verbose than your solution (requires an extra class and three lines in your query code, rather than 2), but it only creates one List, rather than two, so you'll have to decide which fits your needs best.

In addition, ResultHandlers can be useful for additional things - making sure results are sorted in a certain way or filtered or something else, in case you need that.

这篇关于MyBatis SelectList输出CopyOnWriteArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 09:22