• Mybatis学习笔记9


    鉴别器:mybatis可以使用discriminator判断某列的值,然后根据某列的值改变封装行为。

    示例如下:

    DeptmentMapper接口定义:
    package com.mybatis.dao;
    
    import com.mybatis.bean.Department;
    
    public interface DeptmentMapper {
        public Department getDeptById(Integer id);
    }
    
    DeptmentMapper.xml文件定义:
    <?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="com.mybatis.dao.DeptmentMapper">
        <!--public Department getDeptById(Integer id);-->
        <select id="getDeptById" resultType="com.mybatis.bean.Department">
            select id,dept_name departmentName from tbl_dept where id=#{id}
        </select>
    </mapper>
    
    EmployeeMapper接口定义:
    package com.mybatis.dao;
    
    import com.mybatis.bean.Employee;
    
    public interface EmployeeMapper {
        public Employee getEmpByIdWithDept(Integer id);
    }
    
    EmployeeMapper.xml文件定义:
    <?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="com.mybatis.dao.EmployeeMapper">
        <!-- <discriminator javaType=""></discriminator>
    		鉴别器:mybatis可以使用discriminator判断某列的值,然后根据某列的值改变封装行为
    		封装Employee:
    			如果查出的是女生:就把部门信息查询出来,否则不查询;
    			如果是男生,把last_name这一列的值赋值给email;
    	 -->
        <resultMap type="com.mybatis.bean.Employee" id="MyEmpDis">
            <id column="id" property="id"/>
            <result column="last_name" property="lastName"/>
            <result column="email" property="email"/>
            <result column="gender" property="gender"/>
            <!--
                column:指定判定的列名
                javaType:列值对应的java类型  -->
            <discriminator javaType="java.lang.Integer" column="gender">
                <!--女生  resultType:指定封装的结果类型;不能缺少。/resultMap-->
                <case value="0" resultType="com.mybatis.bean.Employee">
                    <association property="dept"
                                 select="com.mybatis.dao.DeptmentMapper.getDeptById"
                                 column="d_id">
                    </association>
                </case>
                <!--男生 ;如果是男生,把last_name这一列的值赋值给email; -->
                <case value="1" resultType="com.mybatis.bean.Employee">
                    <id column="id" property="id"/>
                    <result column="last_name" property="lastName"/>
                    <result column="last_name" property="email"/>
                    <result column="gender" property="gender"/>
                </case>
            </discriminator>
        </resultMap>
    
        <select id="getEmpByIdWithDept" resultMap="MyEmpDis">
           select * from tbl_employee where id=#{id}
        </select>
    </mapper>
    
    测试代码:
    package com.mybatis.demo;
    
    import com.mybatis.bean.Department;
    import com.mybatis.bean.Employee;
    import com.mybatis.dao.DeptmentMapper;
    import com.mybatis.dao.EmployeeMapper;
    import org.apache.ibatis.io.Resources;
    import org.apache.ibatis.session.SqlSession;
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.apache.ibatis.session.SqlSessionFactoryBuilder;
    import org.junit.Test;
    
    import java.io.IOException;
    import java.io.InputStream;
    
    public class MyTest {
        public SqlSessionFactory getSqlSessionFactory() throws IOException {
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            return new SqlSessionFactoryBuilder().build(inputStream);
        }
    
        @Test
        public void testResultMap() throws IOException {
            SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
            SqlSession openSession = sqlSessionFactory.openSession(true);
            try {
                EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
                Employee girl = mapper.getEmpByIdWithDept(1);
                System.out.println(girl.getDept());
                Employee boy = mapper.getEmpByIdWithDept(7);
                System.out.println(boy.getDept());
            } finally {
                openSession.close();
            }
        }
    }
    
  • 相关阅读:
    macOS 遇到 svnadmin无法使用的情况
    语音识别进化简史:从造技术到建系统
    你是什么垃圾?人工智能面对干垃圾和湿垃圾“有点蒙”
    垃圾分类的事,让机器人做去吧!
    怎样才能叫一只奶牛自愿挤奶?
    第一次,脑机接口可以实时读取人类语言了
    机器人工作原理的超详细解析,生动、形象!
    1900页数学基础:面向CS的线性代数、拓扑、微积分和最优化
    微软Azure AI负责人:OpenAI只在微软云上训练模型
    Velodyne收购高清地图公司 将研发更安全的ADAS系统
  • 原文地址:https://www.cnblogs.com/xidian2014/p/10351043.html
Copyright © 2020-2023  润新知