public List<Sort> findAll() throws Exception{
List<Sort> sorts = new ArrayList<Sort>();
Connection conn = null;
try{
conn = ConnectionUtils.getConnection();
PreparedStatement prep =
conn.prepareStatement("SELECT s_name ,sort1.id ,name ,prodDesc from sort left join sort1 on sort.id = sort1.sort_id order by sort.id asc");
ResultSet rs = prep.executeQuery();
while(rs.next()){
Sort sort = new Sort();
sort.setId(rs.getInt("id"));
sort.setSname(rs.getString("s_name"));
sort.setName(rs.getString("name"));
sorts.add(sort);
}
}catch(Exception e){
e.printStackTrace();
throw e;
}finally{
ConnectionUtils.closeConnection(conn);
}
return sorts;
}
查询出来是{"sort":[{"id":1,"sname":"食品","name":"面包"},{"id":3,"sname":"食品","name":"糖"},{"id":2,"sname":"衣服","name":"衬衫"},{"id":4,"sname":"衣服","name":"毛衣"}]} 我想变成让它食品在一个数组 衣服在一个数组 怎么改public class Sort{
public List <A> shipin =new ArrayList<A>();
public List <A> yifu=new ArrayList<A>();
}
public class B{
public Sort sort=new Sort();
}
public class A{
String id;
pubic name;
}
声明以个B对象,如果是衣服就把对象放到衣服集合,食品放到食品 集合,之后把B对象转成json就是:
{"sort":{"食品":[{"id":1,"name":"面包},{"id":3,"name":"糖"}],"衣服":[{"id":2,"name":"衬衫},{"id":4,"name":"毛衣"}]} }