Mybatis——返回值(resultType&resultMap)详解

「爱情、让人受尽委屈。」 2024-03-24 00:29 128阅读 0赞

之前的文章里面有对resultType和resultMap的简单介绍这一期出点详细的

resultType:

1,返回值为简单类型。 直接使用resultType=“类型”,如string,Integer等。

  1. String getEmpNameById(Integer id);
  2. <!--
  3. 指定 resultType 返回值类型时 String 类型的,
  4. string 在这里是一个别名,代表的是 java.lang.String
  5. 对于引用数据类型,都是将大写字母转小写,比如 HashMap 对应的别名是 'hashmap'
  6. 基本数据类型考虑到重复的问题,会在其前面加上 '_',比如 byte 对应的别名是 '_byte'
  7. -->
  8. <select id="getEmpNameById" resultType="string">
  9. select username from t_employee where id = #{id}
  10. </select>

2.返回值为List类型。

使用resultType=“list元素的类型”,一般是实体类如User,也可以是Map,对应返回值类型是List , List>,不管是哪种,最终结果会根据接口返回值类型自动将多个 resultType指定的类型的元素(User或以一条记录为一个Map)组装成List。

  1. List<User> getUser(String age);
  2. <select id="getUser" resultType="User">
  3. select * from user
  4. where
  5. age = #{age}
  6. </select>
  7. List<Map<String,Object>> findUserList();
  8. <select id="findUserList" parameterType="int" resultType="java.util.Map">
  9. SELECT * FROM user
  10. </select>

用java.util.List也是可以的,java.util.Map也是可以的至于为什么我没有没有研究过

3.返回值为Map类型。(使用map要注意查询结果的条数,多条会报错)

  1. 如果查询的结果是一条,我们可以把查询的数据以{表字段名, 对应的值}方式存入到Map中。

    Map getEmpAsMapById(Integer id);

    1. <select id="getEmpAsMapById" resultType="map">
    2. select * from t_employee where id = #{id}
    3. </select>
  2. 如果查询的结果是多条数据,我们也可以把查询的数据以{表中某一字段名, JavaBean}方式来封装成Map

    // 查询所有员工的信息,把数据库中的 ‘id’ 字段作为 key,对应的 value 封装成 Employee 对象

    1. // @MapKey 中的值表示用数据库中的哪个字段名作 key
    2. @MapKey("id")
    3. Map<Integer, Employee> getAllEmpsAsMap();
    1. <select id="getAllEmpsAsMap" resultType="employee">
    2. select * from t_employee
    3. </select>

扩展. 上面返回结果的形式都是基于查询 (select) 的,其实对于增删改的操作也可以返回一定类型的数据,比如BooleanInteger等。

resultMap:

属于自定义的映射,用于由于各种原因数据库的字段名跟实体类的字段名不一致

1.Emp实体类的属性:

  1. private Integer empId;
  2. private String empName;
  3. private Integer age;
  4. private String gender;

2.表字段名:











emp_id emp_name age gender dept_id

3.映射文件配置:

  1. <!-- Emp getEmpById(@Param("emp_id") Integer emp_id);-->
  2. <select id="getEmpById" resultType="Emp">
  3. select * from t_emp where emp_id = #{emp_id}
  4. </select>

4.执行结果发现empId=null,empName=null

原因:数据库表字段名中的emp_id,emp_name与实体类的属性名empId,empName不一致,由数据库不能映射到实体类属性对应的属性名

1. 方式一 字段名设置别名

  1. select emp_id empid from emp where emp_id = #{emp_id}

2. 方式二 下划线映射为驼峰(在配置文件中配置)

  1. <!--引入properties文件,此时就可以${属性名}的方式访问属性值-->
  2. <properties resource="jdbc.properties"/>
  3. <!--配置mybatis自动转换为驼峰式命名-->
  4. <settings>
  5. <setting name="mapUnderscoreToCamelCase" value="true"/>
  6. <!--开启延迟加载-->
  7. <setting name="lazyLoadingEnabled" value="true" />
  8. </settings>

3. 方式三 自定义映射resultMap resultMap: 设置自定义的映射关系 id: 唯一标识–>resultMap=” “ type: 处理映射关系的实体类的类型 标签: id: 处理主键和实体类中属性的映射关系 result: 处理普通字段和实体类中属性的映射关系 column: 设置映射关系中的字段名,必须是sql中的某字段 property: 设置映射关系中的属性的属性名,必须为实体类中的属性名

  1. <resultMap id="empDeptMapResultMapOne" type="Emp">
  2. <id property="eid" column="eid"></id>
  3. <result property="empName" column="emp_name"></result>
  4. <result property="age" column="age"></result>
  5. <result property="sex" column="sex"></result>
  6. <result property="email" column="email"></result>
  7. <result column="did" property="dept.did"></result>
  8. <result column="dname" property="dept.dname"></result>
  9. </resultMap>
  10. <select id="getEmpAndDept" resultMap="empDeptMapResultMapTwo">
  11. select emp.*,dept.* from emp left join dept on emp.did = dept.did where emp.eid = #{eid}
  12. </select>

发表评论

表情:
评论列表 (有 0 条评论,128人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Nancy 返回详解

    简介 Nancy 是一个轻量级的,简单粗暴的framework用来构建基于HTTP的各种服务,兼容.Net和Mono。它的返回值也是多种多样的,适应各种不同的情况。包括...