mybatis-查询(自动封装得resultType)-12

resultType是自动封装的

查询list

public List<Employee> getEmpsByLastName(String lastName);
<select id="getEmpsByLastName" resultType="com.stayreal.mybatis.Employee">
    select * from tbl_employee where last_name like #{lastName}
    </select>
List<Employee> emps = mapper.getEmpsByLastName("Jerry");

查询Map

单条记录封装:

public  Map<String,Object>  getEmpByIdReturnMap(Integer id);
<select id="getEmpByIdReturnMap" resultType="map">
        select * from tbl_employee where id = #{id}
 </select>
 Map<String,Object> map = mapper.getEmpByIdReturnMap(1);
         for(String k:map.keySet()){
             System.out.println(k+"-"+map.get(k));
         }
  }

多条记录封装:

// 返回map  多条记录
    @MapKey("id")//  告诉mybatis封装这个map的时候使用那个属性作为主键
    public Map<Integer,Employee> getEmpByLastNameLikeReturnMap(String lastName);
<select id="getEmpByLastNameLikeReturnMap" resultType="com.stayreal.mybatis.Employee">
        select * from tbl_employee where last_name like #{lastName}
    </select>
Map<Integer,Employee> map = mapper.getEmpByLastNameLikeReturnMap("Jerry");
            System.out.println(map);
/**
多条map的打印结果
{1=Employee{id=1, lastName='Jerry', email='Jerry@qq.com', gender='1'}, 3=Employee{id=3, lastName='Jerry', email='Jerry@qq.com', gender='1'}, 4=Employee{id=4, lastName='Jerry', email='Jerry@qq.com', gender='1'}, 5=Employee{id=5, lastName='Jerry', email='Jerry@qq.com', gender='1'}}}
*/       
阅读更多
文章标签: mybatis
个人分类: mybatis
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭