zoukankan      html  css  js  c++  java
  • 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();
            }
        }
    }
    
  • 相关阅读:
    微信小程序入门
    webpack
    模块化开发(1)
    HTML5表单
    移动端入门
    MySQL
    js面向对象与PHP面向对象总结
    PHP
    Git指令
    Redux
  • 原文地址:https://www.cnblogs.com/xidian2014/p/10351043.html
Copyright © 2011-2022 走看看