select :

  返回对象:

     <select  id = " "  resultType= "对象的全类名"  />

  List:

    <select  id = " "  resultType = "list泛型中的值" />

  Map:

    1.返回一条记录 Map<String,Object>

                 key -> 字段

              value -> 值

                                     <select id = " "  resultType="map" />

    2.返回多条记录 Map<Integer,Employee>

             key -> @MapKey("column")标注在接口方法上,表名哪个字段作为Key

               value -> 对象

             <select id = " " resultTyppe="对象的全类名" />

  ResultMap:

    一对一:

    1.级联属性 

     <resultMap id="myEmpAndDept" type="org.maple.pojo.Employee">
   <id column="id" property="id"/>
   <result column="last_name" property="name"/>
   <result column="email" property="email"/>
   <result column="gender" property="gender"/>
   <result column="did" property="dept.id"/>
   <result column="deptName" property="dept.deptName"/>
  </resultMap>

     <select id="getEmpAndDeptById" resultMap="myEmpAndDept">

     2.association

      <resultMap id="myEmpAndDept2" type="org.maple.pojo.Employee">
   <id column="id" property="id"/>
   <result column="last_name" property="name"/>
   <result column="email" property="email"/>
   <result column="gender" property="gender"/>
      <association property="dept" javaType="org.maple.pojo.Department">
   <id column="did" property="id"/>
   <result column="deptName" property="deptName"/>
      </association>
   </resultMap>

     3.association 分布查询  

     <association property="dept" select="org.maple.mapper.DepartmentMapper.getDeptById" column="d_id"/>

    一对多

      collection查询

        <!-- 要通过id查询部门信息和所有员工 -->
     <resultMap id="myDept" type="org.maple.pojo.Department">
      <id column="id" property="id"/>
      <result column="dept_name" property="deptName"/>
      <!--定义集合时,使用collection属性-->
      <collection property="emps" ofType="org.maple.pojo.Employee">
     <id column="eId" property="id"/>
      <result column="last_name" property="name"/>
     <result column="gender" property="gender"/>
     <result column="email" property="email"/>
      </collection>
    </resultMap>       collection分部查询
        <resultMap id="myDeptStep" type="org.maple.pojo.Department">
      <id column="id" property="id"/>
     <result column="dept_name" property="deptName"/>
     <collection property="emps" select="org.maple.mapper.EmployeeMapper.findEmpsById" column="id"></collection>
      </resultMap>

update,delete,insert:返回Integer,Long,Boolean,void

1.返回List

   返回集合类型,resultType中写集合中元素的类型

    /**
* 返回List集合
*/
List<Employee> getEmpsByNameLike(String name);
    <!--如果返回的是集合,resultType需要写集合中元素的类型-->
<select id="getEmpsByNameLike" resultType="org.maple.pojo.Employee">
SELECT id,last_name name,email,gender
FROM tbl_employee
WHERE last_name like concat('%',#{name},'%');
</select>
Employee{id=1, name='tom', gender=0, email='tom@atguigu.com'}
Employee{id=8, name='rose', gender=1, email='rose@qq.com'}
Employee{id=9, name='zhaozhihao', gender=1, email='chenquan@qq.com'}
Employee{id=12, name='nihao', gender=1, email='nihao@qq.com'}

2.返回Map

1.返回的一条记录的map

  key:字段名

  value:表中的名字

    /**
* 返回一条记录map,key就是列名,值就是表中的值
*/
Map<String,Object> getEmpByIdReturnMap(Integer id);
    <!--如果返回的是map,resultType需要写map,mybatis为常用类起了别名-->
<select id="getEmpByIdReturnMap" resultType="map">
SELECT id,last_name name,email,gender
FROM tbl_employee
WHERE id = #{id}
</select>
{gender=0, name=tom, id=1, email=tom@atguigu.com}

2.返回多条记录的map

  Map<column,Employee>

  key:哪个字段作为key,使用@MapKey("column")

  value:封装对象

    /**
* 多条记录封装Map,Map<Integer,Employee>
* @MapKey 告诉mybaits使用哪个属性封装成map的key
*/
@MapKey("id")
Map<Integer,Employee> getEmpByNameLikeReturnMap(String name);
    <!--如果返回的是map,封装成emp,resultType需要写Employee-->
<select id="getEmpByNameLikeReturnMap" resultType="org.maple.pojo.Employee">
SELECT id,last_name name,email,gender
FROM tbl_employee
WHERE last_name like concat('%',#{name},'%');
</select>
{1=Employee{id=1, name='tom', gender=0, email='tom@atguigu.com'},
8=Employee{id=8, name='rose', gender=1, email='rose@qq.com'},
9=Employee{id=9, name='zhaozhihao', gender=1, email='chenquan@qq.com'},
12=Employee{id=12, name='nihao', gender=1, email='nihao@qq.com'}}

3.ResultMap

1.简单的使用

    /**
* 根据id查询员工
*/
Employee getEmpById(Integer id);
<mapper namespace="org.maple.mapper.EmployeeMapperPlus">

    <!--resultMap 自定义封装规则-->
<!--
id:唯一id,方便引用
type:自定义规则的java类型
-->
<resultMap id="MyEmp" type="org.maple.pojo.Employee">
<!--
id 主键的封装规则,底层会有优化
column 数据库中字段
property javaBean中的字段
-->
<id column="id" property="id"/>
<result column="last_name" property="name"/>
<!--其他不指定的列,如果名字一样,会自动封装。但是写resultMap建议都写上-->
<result column="email" property="email"/>
<result column="gender" property="gender"/>
</resultMap> <!--简单的使用resultMap-->
<select id="getEmpById" resultMap="MyEmp">
SELECT id,last_name,email,gender
FROM tbl_employee
WHERE id = #{id}
</select> </mapper>

2.多表使用(一对一)

public class Employee {

    private Integer id;

    private String name;

    private Character gender;

    private String email;

    private Department dept;
}
public class Department {

    private Integer id;
private String deptName;
}

  2.1 第一种规则

  级联属性的方式

    /**
* 根据id查出员工和部门信息
*/
Employee getEmpAndDeptById(Integer id);
  <!--1.级联属性的方式-->
   <resultMap id="myEmpAndDept" type="org.maple.pojo.Employee">
<id column="id" property="id"/>
<result column="last_name" property="name"/>
<result column="email" property="email"/>
<result column="gender" property="gender"/>
<result column="did" property="dept.id"/>
<result column="deptName" property="dept.deptName"/>
</resultMap> <select id="getEmpAndDeptById" resultMap="myEmpAndDept">
SELECT e.id,e.last_name,e.email,e.gender,d.id did,d.dept_name deptName
FROM tbl_employee e
INNER JOIN tbl_dept d
ON e.d_id = d.id
WHERE e.id = #{id}
</select>

  2.2 第二种规则(association定义对象

    association: property 是 Employee对象中的属性,javaType是指该属性属于哪个javaBean

    <!--2.使用association-->
<resultMap id="myEmpAndDept2" type="org.maple.pojo.Employee">
<id column="id" property="id"/>
<result column="last_name" property="name"/>
<result column="email" property="email"/>
<result column="gender" property="gender"/>
<association property="dept" javaType="org.maple.pojo.Department">
<id column="did" property="id"/>
<result column="deptName" property="deptName"/>
</association>
</resultMap>

  2.3 第三种规则(分部查询,association

    假设之前部门的mapper中已经定义了通过部门id查找部门信息,那么我们就可以通过分部查询来查询出员工信息和部门信息

    /**
* 根据id查出员工和部门信息,分部查询
*/
Employee getEmpAndDeptByIdStep(Integer id);
    <!--使用association进行分布查询-->
<resultMap id="myEmpAndDeptStep" type="org.maple.pojo.Employee">
<id column="id" property="id" />
<result column="last_name" property="name"/>
<result column="email" property="email"/>
<result column="gender" property="gender"/>
<!--property:Employee中的属性
select:调用DepartmentMapper中的查询方法 命名空间+方法
column:传给select 方法哪个参数
-->
<association property="dept" select="org.maple.mapper.DepartmentMapper.getDeptById" column="d_id"/>
</resultMap> <select id="getEmpAndDeptByIdStep" resultMap="myEmpAndDeptStep">
select id,last_name,gender,email,d_id from tbl_employee where id = #{id}
</select>

  分部查询的好处,可以实现懒加载:当只需要调用员工的信息时,第二条sql语句就不会发出,知道需要调用部门的信息,才会发sql语句

  通过在全局配置文件中添加:

     <settings>
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"/>
</settings>

3.多表的使用(一对多)

public class Department {

    private Integer id;
private String deptName;
private List<Employee> emps;
}

1. 嵌套结果集的方式

    /**
* 根据id查询部门信息和所有员工
*/
Department getDeptAndEmpsById(Integer id);
    <!-- 要通过id查询部门信息和所有员工 -->
<resultMap id="myDept" type="org.maple.pojo.Department">
<id column="id" property="id"/>
<result column="dept_name" property="deptName"/>
<!--定义集合时,使用collection属性-->
<collection property="emps" ofType="org.maple.pojo.Employee">
<id column="eId" property="id"/>
<result column="last_name" property="name"/>
<result column="gender" property="gender"/>
<result column="email" property="email"/>
</collection>
</resultMap> <select id="getDeptAndEmpsById" resultMap="myDept">
SELECT d.id,d.dept_name,e.id eId,e.last_name,e.gender,e.email
FROM tbl_dept d
LEFT JOIN tbl_employee e
ON d.id = e.d_id
WHERE d.id = #{id}
</select>

2.分部查询的方式

    /**
* 根据id 分部查询出部门信息和所有员工
*/
Department getDeptAndEmpsByIdStep(Integer id);
    <!--分部查询-->
<resultMap id="myDeptStep" type="org.maple.pojo.Department">
<id column="id" property="id"/>
<result column="dept_name" property="deptName"/>
<collection property="emps" select="org.maple.mapper.EmployeeMapper.findEmpsById" column="id"></collection>
</resultMap> <select id="getDeptAndEmpsByIdStep" resultMap="myDeptStep">
SELECT id,dept_name
FROM tbl_dept
WHERE id = #{id}
</select>

    

<MyBatis>入门五 查询的返回值处理的更多相关文章

  1. MyBatis查询,返回值Map或List<Map>

    一.返回值Map 1.mapper.xml <select id="selectUserMapLimitOne" resultType="java.util.Has ...

  2. mybatis的增删改查返回值小析(六)

    本文验证了通过mybatis访问数据库时的,增删改查的返回值情况. 直接看代码. 1.service层 /** *@Author: Administrator on 2020/3/12 15:15 * ...

  3. 获取动态SQL查询语句返回值(sp_executesql)

    在写存储过程时经常会遇到需要拼接SQL语句的情况,一般情况下仅仅是为了执行拼接后的语句使用exec(@sql)即可. 而今天的一个存储过程却需要获取动态SQL的查询结果. 需求描述:在某表中根据Id值 ...

  4. MyBatis insert/delete/update 的返回值

    insert,返回值是:新插入行的主键(primary key):需要包含<selectKey>语句,才会返回主键,否则返回值为null. <insert id="inse ...

  5. mybatis调用存储过程获得取返回值

    总体思路:map传值 controller: Map<String,Object> m=new HashMap<String,Object>(); m.put("na ...

  6. C# lambda查询带返回值

    问题来源: <深入理解C#(第3版)> 11页 具体如下: var lists=new List<string>{"111","222" ...

  7. Python3入门系列之-----return返回值,我终于懂了

    前言 初学者学习return的用法有点蒙,不知道它的作用是什么?返回的是什么?在什么时候要用?小伙伴也可能会遇到和我同样的困扰,给大家举个例子,马上就明白了. 同一段代码,函数中带return和没有r ...

  8. MyBatis参数条件查询传入的值为0时的判断

    MyBatis条件查询对字段判断是否为空一般为: <if test="testValue!=null and testValue != ''"> and test_va ...

  9. spring+mybatis下delete和insert返回值-2147482646

    <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate"&g ...

随机推荐

  1. 123 c#调用oracle存储过程返回数据集 --通过oracle存储过程返回数据集

    c#调用oracle存储过程返回数据集 2008-12-20 10:59:57|  分类: net|字号 订阅   CREATE OR REPLACE PACKAGE pkg_tableTypeIS  ...

  2. 美国诚实签经验——中英文行程单、往返机票、用英语面试的申请者通过率>用中文面试的申请者的通过率、一直保持着微笑,看上去很自信,也很诚恳、户口簿带上最好

    在排队等待时据我的观察,用英语面试的申请者通过率>用中文面试的申请者的通过率.一家人申请通过率>单个人通过率:商务签证通过率>旅游签证通过率 一.    面签材料 1.    必备材 ...

  3. Java 工程与 Eclipse 高级用法

    0. 显示各种视图(perspective)及工具窗口 [Window]⇒ [Perspective]⇒ [Open perspective]⇒ other(如下图所示): [Window]⇒ [Sh ...

  4. Opencv+Zbar二维码识别(一维码校正)

    一维码由一组规则排列的黑色线条.白色线条以及对应的字符组成.对倾斜的(没有严重形变)一维码的角度校正,可以根据其黑白相间.排列规则的特点,计算傅里叶频谱,通过傅里叶频谱中直线的倾斜角度计算空间域图像一 ...

  5. 模块化编程:AMD规范

    目前,通行的Javascript模块规范共有两种:ComonJS和AMD. CommonJS node.js的模块系统,就是参照CommonJS规范实现的.在ConmonJS中,有一个全局方法requ ...

  6. ipvs和ipvsadm

    ipvs和ipvsadm ipvs:内核中的协议栈上实现 ipvs是LVS软件核心,是运行在LB上的,这是个基于ip层的负载均衡. ipvs的总体结构主要有ip包处理,负载均衡算法,系统配置和管理三个 ...

  7. sql语句如何查询当天,一周,一月的数据的语句

    sql查询当天,一周,一个月数据的语句 --查询当天:   select * from info where DateDiff(dd,datetime,getdate())=0 --查询24小时内的: ...

  8. Cmake编译protobuf

    编译指令,在powershell中执行 :      .\protoc.exe .\ive.proto --cpp_out .      .\protoc.exe .\ive.proto --csha ...

  9. [AHOI2006] 文本编辑器editor

    Description 这些日子,可可不和卡卡一起玩了,原来可可正废寝忘食的想做一个简单而高效的文本编辑器.你能帮助他吗?为了明确任务目标,可可对"文本编辑器"做了一个抽象的定义: ...

  10. Spring 侵入式和非侵入式

    1.非侵入式的技术体现 允许在应用系统中自由选择和组装Spring框架的各个功能模块,并且不强制要求应用系统的类必须从Spring框架的系统API的某个类来继承或者实现某个接口. 2.如何实现非侵入式 ...