23.MyBatis学习--映射文件_select_resultMap_discriminator鉴别器

鉴别器

<discriminator javaType=""></discriminator>鉴别器:mybatis可以使用discriminator判断某列的值,然后根据某列的值改变封装行为。
例如:封装Employee:
如果查出的是女生:就把部门信息查询出来,否则不查询;
如果是男生,把last_name这一列的值赋值给email;

  1. 在dao接口创建对应的查询方法
public interface EmployeeMapper {

    public Employee testDiscriminator(Integer id);
}
  1. 通过鉴别器实现改变封装行为。
    <resultMap id="MyDiscri" type="com.fzl.mybatis.bean.Employee">
        <id column="id" property="id"/>
        <result column="last_name" property="lastName"/>
        <result column="gender" property="gender"/>
        <result column="email" property="email"/>
        <!--如果是女性,则查询其部门信息,如果是男性,则不查部门信息,将gender的值赋给last_name,last_name赋值给gender -->
        <discriminator javaType="string" column="gender">
            <!-- 如果是女性 ,resultType:指定封装的结果类型;不能缺少。-->
            <case value="0" resultType="com.fzl.mybatis.bean.Employee">
                <association property="dept" select="com.fzl.mybatis.dao.DepartmentMapper.getDeptById" column="d_id"></association>
            </case>
            <!--如果是男性,把last_name这一列的值赋值给email; -->
            <case value="1" resultType="com.fzl.mybatis.bean.Employee">
                <id column="id" property="id"/>
                <result column="last_name" property="lastName"/>
                <result column="gender" property="gender"/>
                <result column="last_ame" property="email"/>
            </case>
        </discriminator>
    </resultMap>
    <select id="testDiscriminator" resultMap="MyDiscri">
        select * from tbl_employee where id=#{id}
    </select>
  1. 测试
 @Test
    public void test5() throws IOException {
        SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
        SqlSession openSession = sqlSessionFactory.openSession();
        try{
            EmployeeMapper employeeMapper = openSession.getMapper(EmployeeMapper.class);
            Employee employee = employeeMapper.testDiscriminator(1);
            System.out.println(employee.getLastName());
            System.out.println(employee);
        }finally{
            openSession.close();
        }
    }
  1. 测试结果
    (1)女生
    在这里插入图片描述
    (2)男生
    在这里插入图片描述
关于Mybatis在insert中嵌套子查询的问题,可以使用Mybatis的动态SQL语句来实现,具体方法如下: 1. 在mapper文件中定义一个包含子查询的SQL语句,比如: ``` <select id="getUserIdByName" parameterType="java.lang.String" resultType="java.lang.Integer"> SELECT user_id FROM user WHERE user_name = #{name} </select> ``` 2. 在insert语句中使用动态SQL语句来调用子查询,比如: ``` <insert id="insertOrder" parameterType="Order"> INSERT INTO order (order_no, user_id, order_time) VALUES (#{orderNo}, #{userId, jdbcType=INTEGER}, #{orderTime}) <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer"> SELECT LAST_INSERT_ID() </selectKey> </insert> ``` 其中,#{userId, jdbcType=INTEGER}是插入语句中的一个参数,它的值通过调用getUserIdByName子查询来获取。 关于Mybatis中查询结果resultMap的使用概述,可以参考以下几点: 1. resultMapMybatis中用于映射查询结果集的标签,它可以将查询结果集的列名映射为Java对象的属性名。 2. resultMap标签可以定义在mapper文件中,也可以定义在公共的resultMap文件中,以便在多个mapper文件中复用。 3. resultMap标签支持多种映射方式,如一对一、一对多、多对一、多对多等,可以根据查询结果集的实际情况选择不同的映射方式。 4. resultMap标签还支持映射嵌套对象、映射对象属性、映射枚举值等高级功能,可以根据具体情况选择使用。 总之,Mybatis中的resultMap标签是非常强大且灵活的,可以帮助我们快速、方便地实现查询结果集的映射
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值