MyBatis查询结果resultType返回值类型详细介绍resultMap,parameterType


原文链接,点击这里

parameterType的使用注意事项

参考文章,点击这里
  对于这种一个参数的这种其实没有必要写parameterType,而且还有就是多个参数的时候也没有必要写parameterType,也就是说,其实该参数的存在是不是特别必要的。其中@Param和parameterType 二者存其一即可。看名字就能知道ParameterType是按照类型进行匹配,而@Param是按照名字进行匹配,因此可以联想下,多个参数的时候按名字匹配即可,对于按照类型就不能匹配了。如下是该参数标识的类型对应的别名
通过测试发现自定义对象的parameterType必须写上,否则SQL无法执行
而数组、List、Map这类JDK自带类型不写parameterType也没有问题

一、返回一般数据类型

比如要根据 id 属性获得数据库中的某个字段值。

mapper 接口:

    // 根据 id 获得数据库中的 username 字段的值
    String getEmpNameById(Integer id);

SQL 映射文件:

    <!-- 
        指定 resultType 返回值类型时 String 类型的,
        string 在这里是一个别名,代表的是 java.lang.String 

        对于引用数据类型,都是将大写字母转小写,比如 HashMap 对应的别名是 'hashmap'
        基本数据类型考虑到重复的问题,会在其前面加上 '_',比如 byte 对应的别名是 '_byte'
    -->
    <select id="getEmpNameById" resultType="string">
        select username from t_employee where id = #{id}
    </select>

二、返回 JavaBean 类型

比如根据某个字段获得数据库中的信息,把查询的结果信息封装成某个 JavaBean 类型的数据。

mapper 接口:

    // 根据 id 查询信息,并把信息封装成 Employee 对象
    Employee getEmpById(Integer id);

SQL 映射文件:

    <!-- 
        通过 resultType 指定查询的结果是 Employee 类型的数据  
        只需要指定 resultType 的类型,MyBatis 会自动将查询的结果映射成 JavaBean 中的属性
    -->
    <select id="getEmpById" resultType="employee">
        select * from t_employee where id = #{id}
    </select>

三、返回List类型

有时候我们要查询的数据不止一条,比如:模糊查询,全表查询等,这时候返回的数据可能不止是一条数据,对于多数据的处理可以存放在List集合中。

mapper 接口:

    // 假如是全表查询数据,将查询的数据封装成 Employee 类型的集合
    List<Employee> getAllEmps();

SQL 映射文件:

    <!--
        注意这里的 resultType 返回值类型是集合内存储数据的类型,不是 'list'
    -->
    <select id="getAllEmps" resultType="employee">
        select * from t_employee
    </select>

四、返回Map类型

MyBatis 还支持将查询的数据封装成Map。

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

mapper 接口:

    //  根据 id 查询信息,并把结果信息封装成 Map 
    Map<String, Object> getEmpAsMapById(Integer id);

SQL 映射文件:

    <!-- 
        注意这里的 resultType 返回值类型是 'map'
     -->
    <select id="getEmpAsMapById" resultType="map">
        select * from t_employee where id = #{id}
    </select>

下面把查询的结果数据贴出来供大家参考:
这里写图片描述

  1. 如果查询的结果是多条数据,我们也可以把查询的数据以{表中某一字段名, JavaBean}方式来封装成Map。

mapper 接口:

    // 查询所有员工的信息,把数据库中的 'id' 字段作为 key,对应的 value 封装成 Employee 对象
    // @MapKey 中的值表示用数据库中的哪个字段名作 key
    @MapKey("id")
    Map<Integer, Employee> getAllEmpsAsMap();

SQL 映射文件:

    <!--
        注意 resultType 返回值类型,不再是 'map',而是 Map 的 value 对应的 JavaBean 类型
    -->
    <select id="getAllEmpsAsMap" resultType="employee">
        select * from t_employee
    </select>

下面是查询的结果 (只截取了一部分):
这里写图片描述

MyBatis 允许查询的结果封装成Map,这种机制是极好的。

五、扩展

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

总结. 这篇博文主要介绍了在开发中常用的几种数据返回值类型,希望能够为你提供帮助。

6.关联:association和collection

级联的优点是获取关联数据十分方便,但是级联过多会增加数据库系统的复杂度,同时降低系统的性能,如果级联超过三层的话就不能使用级联
association:一对一关联(has one)
collection:一对多关联(has many)
其中resultMap ,它的属性id代表它的标识,type代表使用哪种类作为其映射的类,可以是别名或者全限定名。
JavaType和ofType都是用来指定对象类型的,但是JavaType是用来指定pojo中属性的类型,而ofType指定的是 映射到list集合属性中pojo的类型 。
例子:

public class User {
     private int id ;
     private String username ;
     private String mobile ;
     privateList<Post>posts;
}

< resultMap type = "User" id = "resultUserMap" >
          < result property = "id" javaType = "int" column = "user_id" />
          < result property = "username" javaType = "string" column = "username" />
          < result property = "mobile"   column = "mobile" />
                        <!--javatype指定的是user对象的属性的类型(例如id,posts),而oftype指定的是映射到list集合属性中pojo的类型(本例指的是post类型)-->
          < collection property = "posts"    ofType = "com.spenglu.Post"    javaType = "java.util.ArrayList" column = "userid" >
              < id property = "id" column = "post_id" javaType = "int" jdbcType = "INTEGER" />    
            < result property = "title" column = "title" javaType = "string" jdbcType = "VARCHAR" />
            < result property = "content" column = "content" javaType = "string" jdbcType = "VARCHAR" />
          </ collection >
     </ resultMap >


用法
老师和学生:一对多,学生和老师:多对一
参考文章1,点击这里
参考文章2,点击这里

6.1嵌套的resultMap

这种方法的本质就是把教师实体映射从association元素中提取出来,用一个resultMap元素表示。然后association元素再引用这个resultMap元素。

<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"  
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="net.zaodk.mybatis.mapper.StudentOperationMapper"> 
 <!-- 定义java Bean的属性与数据库的列之间的映射 --> 
 <resultMap type="Teacher" id="teacherResultMap"> 
  <id property="id" column="t_id"/> 
   <result property="name" column="t_name"/> 
   <result property="gender" column="t_gender"/> 
   <result property="researchArea" column="research_area"/> 
 </resultMap> 
 
 <resultMap type="Student" id="studentResultMap"> 
  <id column="id" property="id" /> 
  <result column="name" property="name" /> 
  <result column="gender" property="gender" /> 
  <result column="major" property="major" /> 
  <result column="grade" property="grade"/> 
  <!-- 引用teacherResultMap --> 
  <association property="supervisor" resultMap="teacherResultMap"/> 
 </resultMap> 
 
 <!-- SQL语句中以"#{}"的形式引用参数 --> 
 <select id="getById" parameterType="int" resultMap="studentResultMap"> 
  SELECT st.id,st.name,st.gender,st.major,st.grade,t.id t_id,t.name t_name, 
  t.gender t_gender,t.research_area  
  FROM student st, teacher t 
  WHERE st.supervisor_id = t.id 
  AND st.id=#{id} 
 </select> </mapper>

6.2 嵌套的select语句

这种方式是使用一条单独的select语句来加载关联的实体(本例中就是教师实体),然后在association元素中引用此select语句(注:此方法会产生N+1问题,尽量不要用嵌套的select语句)

<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"  
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="net.zaodk.mybatis.mapper.StudentOperationMapper"> 
 <!-- 定义java Bean的属性与数据库的列之间的映射 --> 
 <resultMap type="Teacher" id="supervisorResultMap"> 
  <id property="id" column="t_id"/> 
   <result property="name" column="t_name"/> 
   <result property="gender" column="t_gender"/> 
   <result property="researchArea" column="research_area"/> 
 </resultMap> 
 
 <resultMap type="Student" id="studentResultMap"> 
  <id column="id" property="id" /> 
  <result column="name" property="name" /> 
  <result column="gender" property="gender" /> 
  <result column="major" property="major" /> 
  <result column="grade" property="grade"/> 
  <!-- 引用teacherResultMap --> 
  <association property="supervisor" column="supervisor_id" select="selectSupervisor"/> 
 </resultMap> 
 
 <!-- SQL语句中以"#{}"的形式引用参数 --> 
 <select id="getById" parameterType="int" resultMap="studentResultMap"> 
  select id,name,gender,major,grade,supervisor_id from student where id =#{id} 
 </select> 
 
 <select id="selectSupervisor" parameterType="int" resultMap="supervisorResultMap"> 
  select id,name,gender,research_area 
  from teacher where id = #{id} 
 </select> </mapper>
  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值