mybatis返回List<Map>

来源:互联网 发布:明解c语言入门pdf下载 编辑:程序博客网 时间:2024/05/22 02:01

Mybatis返回如List形式的
POJO 类
————————
public class Design {

private int design_id;  private String idCreator ;    private String picName; private String picUrl;  private String showText;        private Timestamp releaseTime;  private Timestamp endTime  ; 

}

public class FormData {

        private int data_id ;           private String design_id ;            private String user_id ;            private  Timestamp  createTime;          private int isDelete ;           private String field1;           private String field2;            private String field3;        private String field4;        private String field5;

}


Controller 类


public class JoinController extends BaseController{
@Resource(name=”joinService”)
private JoinManager joinManager;
@ResponseBody
@RequestMapping( value=”getList.do” , produces= “application/json;charset=UTF-8”)
public Object getList( @RequestParam (value=”rd_session”) String rd_session ){

            String openId = null ;            try {                openId =    joinManager.getOpenIdByRd(rd_session);            } catch (Exception e) {                e.printStackTrace();            }            System.out.println(openId);            List<Map>  designMap = null;            try {                designMap  =  joinManager.findDesignListByOpenid( openId  );            } catch (Exception e) {                      e.printStackTrace();            }            System.out.println( "designMap:"+designMap);                JSONArray jsa = new JSONArray();            JSONObject js = null;             for (Map<String,Object> mp : designMap) {                js = new JSONObject();                  for (String k : mp.keySet()  ){                      if ( k.equals("url")){                        js.put(k,  Const.WEBSITE+mp.get(k));                                           }else{                        js.put(k,  mp.get(k));                                    }                  }                    jsa.add(js);                  System.out.println(jsa);            }            return jsa ;  }}

————————————————————————————————————————————————————————————
JoinManager 接口
————————————
public interface JoinManager {

    // 通过rd_session获取  openidpublic String getOpenIdByRd(String rd_session) throws Exception;    //  获取参与的  表单的 design_id  Listpublic List<Map>  findDesignListByOpenid(String openId) throws Exception;

}
——————————————————————————————————————————————————————————————
JoinService 实现类
——————————

@Service(“joinService”)
public class JoinService implements JoinManager {

@Resource(name = "daoSupport")    private DaoSupport dao;/** * 获取openid */@Overridepublic String getOpenIdByRd(  String rd_session) {       return   dao.getOpenIdByRd(  "com.jy.entity.system.Join.findOpenId" ,rd_session) ;}@Overridepublic List<Map>  findDesignListByOpenid(String openId) throws Exception {            return   (List<Map>) dao.findForList("com.jy.entity.system.Join.findDesignListByOpenid",  openId );}

}
——————————————————————————————————————————————————————————————
JoinMapper.xml
——————————

     <select id="findDesignListByOpenid"   parameterType="String"   resultType="java.util.HashMap"   >          select               dt.data_id  as sid, dg.design_id  as sid , dg.title as title , dg.showText  as text, dg.picUrl as url         from             form_data  dt     INNER JOIN   form_design   dg    on   dt.design_id  = dg.design_id        where               dt.user_id  = #{openId}         </select>

PS:
1. 返回map时select列中最好设置别名。(经验证,当不设置别名时,你需要这个来取数据map.get(“count(*)”))。
select
count(*) as amount ,dt.data_id as sid, dg.design_id as sid , dg.title as title , dg.showText as text, dg.picUrl as url
from

  1. 在mybatis中,无论你指定还是不指定返回类型,mybatis都会默认的先将查询回的值放入一个hashMap中(如果返回的值不止一条就是一个包含hashMap的list)。这其中的区别在于,如果你指定了返回类型,mybatis将会根据返回类型的实体类来从hashMap中获取值并set到这个实体类中。如果不指定就默认返回一个HashMap