zoukankan      html  css  js  c++  java
  • mybatis文件映射之select操作返回Map

    1、返回的Map键为列所对应的名称,值就是具体的值

    EmployeeMapper.java

    public Map<String,Object> getEmpByIdReturnMap(Integer id);

    EmployeeMapper.xml

        <select id="getEmpByIdReturnMap" resultType="map">
            select id,last_name lastName,gender,email from tbl_employee where id=#{id}
        </select>

    注意返回值resultType直接写map即可,mybatis会自动进行映射。

    输出:

    {lastName=xiximayou, gender=1, id=1, email=xiximayou@qq.com}

    2、多条记录封装成一个Map,且键为主键的值,值为每条记录对应的值。

    EmployeeMapper.java

        @MapKey("id")
        public Map<Integer,Object> getEmpByLastNameLikeReturnMap(String lastName);

    需要告诉mybatis使用那个属性值作为键的值。

    EmployeeMapper.xml

        <select id="getEmpByLastNameLikeReturnMap" resultType="com.gong.mybatis.bean.Employee">
            select id,last_name lastName,gender,email from tbl_employee where last_name like #{lastName}
        </select>

    此时要返回的值是Map中存储的值的类型。

    输出:

    {1=Employee [id=1, lastName=xiximayou, gender=1, email=xiximayou@qq.com]}

  • 相关阅读:
    makefile vpath变量
    博客园 文章和随笔区别
    Linux OpenGL 实践篇-6 光照
    HTC Vive 叠影器无法创建设备
    Mybatis注解
    MyBatis缓存
    MyBatis关联映射
    动态sql
    MyBatis智能标签
    Mybatis模糊查询及自动映射
  • 原文地址:https://www.cnblogs.com/xiximayou/p/12215805.html
Copyright © 2011-2022 走看看