/**
* 查询通讯录好友列表
*
*
*/
@SuppressWarnings("finally")
@Override
@ResponseBody
public JSONArray queryAddressBook(String userphone) {
JSONObject resultJobj = new JSONObject();
JSONArray tempArray = new JSONArray();
try {
log.info("查看通讯录列表业务.......");
/*
* 通过逗号来分隔出每一个手机号码 并用for循环将号码存入到ids中
*/
String str = userphone;
String[] strs = str.split(",");
int j = strs.length;
ArrayList<String> ids = new ArrayList<String>();
ArrayList<String> userlist = new ArrayList<String>();
for (int i = 0; i < j; i++) {
ids.add(strs[i]);
}
/*
* 传递list对象到sql中 并用for循环将每一号码存入resultjobj中最后返回到客户端
*/
ArrayList<User> list = userDao.queryAddressBookList(ids);
if (list != null && list.size() > 0) {
for (int i = 0; i < list.size(); i++) {
userlist.add(list.get(i).getUserphone());
resultJobj.put("userid", list.get(i).getUserid() + " ");
resultJobj.put("userphone", list.get(i).getUserphone()+ " ");
resultJobj.put("isfriend","YES");
tempArray.add(resultJobj);
}
} else {
log.info("查询通讯录好友列表失败");
resultJobj.put("result", "2");
resultJobj.put("desc", "查询通讯录好友列表返回为空。");
}
//比对数据库中没有的userphone
for (int i = 0; i < strs.length; i++) {
if (!(userlist.contains(ids.get(i)))) {
resultJobj.put("userid"," ");
resultJobj.put("userphone", ids.get(i) + " ");
resultJobj.put("isfriend", "NO");
tempArray.add(resultJobj);
}
}
} catch (Exception e) {
log.error("好友申请列表业务失败,服务器内部错误:" + e);
e.printStackTrace();
resultJobj.put("result", "1");
resultJobj.put("desc", "服务器内部错误");
} finally {
log.info("查询申请用户接口响应的Json类型的结果字符串:" + tempArray.toString());
return tempArray;
}
* 查询通讯录好友列表
*
*
*/
@SuppressWarnings("finally")
@Override
@ResponseBody
public JSONArray queryAddressBook(String userphone) {
JSONObject resultJobj = new JSONObject();
JSONArray tempArray = new JSONArray();
try {
log.info("查看通讯录列表业务.......");
/*
* 通过逗号来分隔出每一个手机号码 并用for循环将号码存入到ids中
*/
String str = userphone;
String[] strs = str.split(",");
int j = strs.length;
ArrayList<String> ids = new ArrayList<String>();
ArrayList<String> userlist = new ArrayList<String>();
for (int i = 0; i < j; i++) {
ids.add(strs[i]);
}
/*
* 传递list对象到sql中 并用for循环将每一号码存入resultjobj中最后返回到客户端
*/
ArrayList<User> list = userDao.queryAddressBookList(ids);
if (list != null && list.size() > 0) {
for (int i = 0; i < list.size(); i++) {
userlist.add(list.get(i).getUserphone());
resultJobj.put("userid", list.get(i).getUserid() + " ");
resultJobj.put("userphone", list.get(i).getUserphone()+ " ");
resultJobj.put("isfriend","YES");
tempArray.add(resultJobj);
}
} else {
log.info("查询通讯录好友列表失败");
resultJobj.put("result", "2");
resultJobj.put("desc", "查询通讯录好友列表返回为空。");
}
//比对数据库中没有的userphone
for (int i = 0; i < strs.length; i++) {
if (!(userlist.contains(ids.get(i)))) {
resultJobj.put("userid"," ");
resultJobj.put("userphone", ids.get(i) + " ");
resultJobj.put("isfriend", "NO");
tempArray.add(resultJobj);
}
}
} catch (Exception e) {
log.error("好友申请列表业务失败,服务器内部错误:" + e);
e.printStackTrace();
resultJobj.put("result", "1");
resultJobj.put("desc", "服务器内部错误");
} finally {
log.info("查询申请用户接口响应的Json类型的结果字符串:" + tempArray.toString());
return tempArray;
}
}
这个项目持久层应用的是Mybatis技术,所以在user.xml配置文件中的配置如下
<!-- 根据string查询用户通讯录-->
<select id="queryAddressBookList" resultType="com.pinke.helpapp.user.beans.User">
select userid,userphone from t_userinfo
<where>
userphone in
<foreach collection="list" index="index" item="item" open="("
separator="," close=")">
#{item}
</foreach>
</where>
</select>
一开始查找了很多资料,才发现自己少<where>