ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

05 映射文件-select

2021-10-02 19:35:16  阅读:157  来源: 互联网

标签:封装 name 映射 05 dept tbl id select


select

返回 List

<!--    public List<Employee> getEmpByLastNameLike(String lastName);-->
<!--    resultType:如果返回的是一个集合,要写集合中元素的类型-->
    <select id="getEmpByLastNameLike" resultType="com.atguigu.mybatis.bean.Employee">
        select * from tbl_employee where last_name like #{lastname}
    </select>

记录封装map

    //返回一条记录的map,key就是列名,值就是对应的值
    public Map<String,Object> getEmpByIdReturnMap(Integer id);

    //返回多条记录封装一个map,Map<Integer,Employee>:键是这条记录的主键,值是记录封装后的java对象
    @MapKey("id")//封装这个map的时候使用哪个属性作为map的key
    public Map<Integer,Employee> getEmpByLastNameLikeReturnMap(String lastName);

<!--    public Map<String,Object> getEmpByIdReturnMap(Integer id);-->
    <select id="getEmpByIdReturnMap" resultType="map">
        select * from tbl_employee where id=#{id}
    </select>

<!--    public Map<Integer,Employee> getEmpByLastNameLikeReturnMap(String lastName);-->
    <select id="getEmpByLastNameLikeReturnMap" resultType="com.atguigu.mybatis.bean.Employee">
        select * from tbl_employee where last_name like #{lastName}
    </select>

自定义resultMap

    <!-- 自定义javaBean的封装规则
    type:自定义规则的java类型
    id:唯一id  方便引用
    -->

    <resultMap type="com.atguigu.mybatis.bean.Employee" id="MyEmp">
        <!--指定主键列的封装规则
        column:指定哪一列
        property:指定对应的javaBean属性
        -->
        <id column="id" property="id"/>
        <!--定义普通列封装会做-->
        <result column="last_name" property="lastName"/>
        <!--其他不指定的列会自动封装,我们要写resulMao就把全部的映射规则都写上-->
        <result column="email" property="email"/>
        <result column="gender" property="gender"/>
    </resultMap>

<!--    public Employee getEmpById(Integer id);-->

    <select id="getEmpById" resultMap="MyEmp">
        select * from tbl_employee where id = #{id}
    </select>

场景一

    <!--
        场景1:查询emplpyee的同时查询员工对应的部门
        一个员工有与之对应的部门信息
    -->

    <resultMap type="com.atguigu.mybatis.bean.Employee" id="MyDiEmp">
        <id column="id" property="id"></id>
        <result column="last_name" property="lastName"/>
        <result column="email" property="email"/>
        <result column="gender" property="gender"/>
        <result column="did" property="dept.id"/>
        <result column="dept_name" property="dept.departmentName"/>
    </resultMap>

		<!--使用assocation定义单个对象的封装规则-->
   		<resultMap type="com.atguigu.mybatis.bean.Employee" id="MyDiEmp2">
        <id column="id" property="id"></id>
        <result column="last_name" property="lastName"/>
        <result column="gender" property="gender"/>
        <!--association 可以指定联合的javaBean对象
            property:指定哪个属性是联合对象
            javaType:指定这个属性对象的类型
        -->
        <association property="dept" javaType="com.atguigu.mybatis.bean.Department">
            <id column="did" property="id"/>
            <result column="dept_name" property="dept.departmentName"/>
        </association>


<!--    public Employee getEmpAndDept(Integer id);-->
    <select id="getEmpAndDept" resultMap="">
        SELECT e.id id,e.last_name last_name,e.gender gender,e.d_id d_id,
               d.id did,d.dept_name dept_name
        FROM tbl_employee e,tbl_dept d
        WHERE e.d_id = d.id AND e.id = #{id}
    </select>

association 分步查询

    <!-- 使用association进行分布查询
           1、先按照员工id查询员工信息
           2、根据查询员工信息中的d_id值去部门表查出部门信息
           3、部门设置到员工中
    -->

    <resultMap id="com.atguigu.mybatis.bean.Employee" type="MyEmpByStep">
        <id column="id" property="id"></id>
        <result column="last_name" property="lastName"/>
        <result column="email" property="email"/>
        <result column="gender" property="gender"/>
        <!--关联对象的封装规则
            select:表明当前属性是调用select指定的方法查出的结果
            column:将那一列的值传给这个方法

            流程:使用select指定的方法(闯入column指定的这列参数的值)查出对象,并封装给property指定的属性
        -->
        <association property="dept"
                     select="com.atguigu.mybatis.dao.DepartmentMapper.getDeptById"
                      column="d_id"></association>

    </resultMap>
    

延迟加载

    <!-- 可以延迟加载(懒加载) 按需加载
        我们每次查询Employee对象的时候,都将一起查询出来
        部门信息在我们使用的时候再去查询
        分步查询的基础上加上 两个配置
        在全部配置文件中加上
       <setting name="lazyLoadingEnabled" value="true"/>
        <setting name="aggressiveLazyLoading" value="false"/>
    -->

collection定义关联集合封装规则

        <!--
        场景二:
            查询部门的时候将部门对应的所有员工信息也查询出来
    -->

	<resultMap id="MyDept" type="com.atguigu.mybatis.bean.Department">
        <id column="did" property="id"></id>
        <result column="dept_name " property="departmentName"/>
        <!-- collection定义集合类型的属性的封装规则
            ofType:指定集合中元素的封装规则
        -->
        <collection property="emps" ofType="com.atguigu.mybatis.bean.Employee">
            <!-- 定义这个集合中元素的封装规则-->
            <id column="eid" property="id"></id>
            <result column="last_name" property="lastName"/>
            <result column="email" property="email"/>
            <result column="gender" property="gender"/>
        </collection>
    </resultMap>
<!--    public Department getDeptByIdPlus(Integer id);-->
    <select id="getDeptByIdPlus" resultMap="">
        select d.id did,dept_name dept_name,
               e.id eid,e.last_name last_name,e.mail main,e.gender gender
        from tbl_dept d
        left join tbl_employee e
        on d.id=e.id_id
        where d,id=#{id}

    </select>

collection分布查询

    <resultMap id="MyDeptStep" type="com.atguigu.mybatis.bean.Department">
        <id column="id" property="id"/>
        <id column="dept_name" property="department"/>
        <collection property="emps"
        select="com.atgui.mybatis.dao.EmployeeMapperPlus.getEmpsbyDeptId"
        column="id"></collection>
    </resultMap>
<!--    public Department getDeptByIdStep(Integer id);-->
    <select id="getDeptByIdStep" resultMap="">
        select id,dept_name departmentName from  tbl_dept where id = #{id}
    </select>


扩展:多列的值传递过去

将多列的值封装map传递

colum="{key1=column1,key2=column2}"

fetchType="lazy" 表示使用延迟加载

eager:立即

lazy:延迟

    <resultMap id="MyDeptStep" type="com.atguigu.mybatis.bean.Department">
        <id column="id" property="id"/>
        <id column="dept_name" property="department"/>
        <collection property="emps"
        select="com.atgui.mybatis.dao.EmployeeMapperPlus.getEmpsbyDeptId"
        column="{deptId=id}" fetchType="lazy"></collection>
    </resultMap>

discriminator鉴别器

    <!--  <discriminator javaType=""></discriminator>

        鉴别器:mybatis可以使用discriminator判断某列的值,然后根据某列的值改变封装行为封装Employee
        实例:如果查出的女生,就把部门信息 查询出来,否则不查询
        如果是男生,把lst_name这一列的值赋值给email
    -->
    <resultMap type="com.atguigu.mybatis.bean.Employee" id="myEmp">
        <id column="id" property="id"></id>
        <result column="last_name" property="lastName"/>
        <result column="email" property="email"/>
        <result column="gender" property="gender"/>
        <!--
            column:指定判定的列名
            javaType:列值对应的Java类型-->
        <discriminator javaType="string" column="gender">
            <!-- 女生 javaType:指定封装的结果类型-->
            <case value="0" resultType="com.atguigu.mybatis.bean.Employee">
                <association property="dept"
                             select="com.atguigu.mybatis.dao.DepartmentMapper.getDeptById"
                             column="d_id">
                </association>
            </case>
            <!--男生-->
            <case value="1" resultType="com.atguigu.mybatis.bean.Employee">
                <id column="id" property="id"></id>
                <result column="last_name" property="lastName"/>
                <result column="last_name" property="email"/>
                <result column="gender" property="gender"/>
            </case>
        </discriminator>

    </resultMap>

标签:封装,name,映射,05,dept,tbl,id,select
来源: https://www.cnblogs.com/flypigggg/p/15362459.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有