Mybatis传入参数map,读取map

如果想输出多张表中的内容或者不是一张表里面的内容(聚集函数),而且又不想resultMap成一个实体,那就用map吧(研究了一下午的成果...)

<select id="getUaMapByTimestamp" parameterType="hashmap"  resultType="hashmap">  
      select tName=t.type_Name, countName=count(t.type_Name)
      from Fault f, device_info d ,device_type t
      where f.handle_status = 1 and f.repair = 0 and f.device_id = d.device_id and t.type_id = d.type_id
      <if test="statDateFrom != null">and f.happen_time >=]]> #{statDateFrom}if>
     <if test="statDateTo != null">and f.happen_time <=]]> #{statDateTo}if>
       group by t.type_Name 
    select>

这样就能是搜索结果变成hashmap类型(别问我题目是map,怎么在讲hashmap...)

java代码:

@Repository
public class FaultDao extends AbstractBasicDaoImpl implements IFaultDao {

    private static final String    NAMESPACE = "com.device.entity.Fault";
    
    protected SqlSession mySqlSession;
    
    
    public SqlSession getMySqlSession() {
        return mySqlSession;
    }

    @Autowired
    public void setMySqlSession(SqlSession mySqlSession) {
        this.mySqlSession = mySqlSession;
    }


    @Override
    protected String getNamespace() {
        return NAMESPACE;
    }


    @Override
    public List findStatisticDevicetype(Map testMap) {
        List aa = new ArrayList();
        aa = mySqlSession.selectList("getUaMapByTimestamp",testMap);
        return aa;
    }

}

关键部分是声明和注入本类自己的sqlSession实例,由于本类继承AbstractBasicDaoImpl,而AbstractBasicDaoImpl又继承相关类,如果不自己声明sqlSession实例的话,默认会调用父类是sqlSession实例,而由于类之间的继承关系,最后selectList的类型会变成List!!!

一个下午的小成果,给自己一朵小红花❀奖励~

 

 

你可能感兴趣的:(Mybatis传入参数map,读取map)