<?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.mapper.AccountMapper">
    <resultMap id="accountResultMap" type="com.pojo.entity.AccountRealEntity">
        <id property="id" column="id"></id>
        <result property="userName" column="user_name"></result>
        <result property="balance" column="balance"></result>
    </resultMap>
    <resultMap id="voMap" type="com.pojo.dto.AccountVO">
        <id property="id" column="id"></id>
        <result property="balance" column="balance"></result>
        <result property="depId" column="dep_id"></result>
        <result property="userName" column="user_name"></result>
<!--        association专门用来处理多对一关系映射-->
        <association property="departmentEntity" javaType="com.pojo.entity.DepartmentEntity">
            <id property="depId" column="dep_id"></id>
            <result property="depName" column="dep_name"></result>
        </association>
    </resultMap>
    <resultMap id="finByStepMap" type="com.pojo.dto.AccountVO">
        <id property="id" column="id"></id>
        <result property="userName" column="user_name"></result>
        <result property="balance" column="balance"></result>
        <result property="depId" column="dep_id"></result>
        <association property="departmentEntity"
            select="com.mapper.DepartmentMapper.findById"
            column="dep_id"
        ></association>
    </resultMap>

    <select id="findAll" resultType="account"
            parameterType="java.lang.Integer"
    >
        select id,user_name,balance from account where id= #{id}
    </select>
    <insert id="insertOne" parameterType="account">
        insert into account(user_name,balance) value (#{user_name},#{balance})
    </insert>
    <update id="updateOne" parameterType="account">
        update account set user_name= #{user_name},balance=#{balance} where id = #{id}
    </update>
    <select id="findByDepId" resultType="com.pojo.entity.AccountRealEntity">
        select * from account where dep_id = #{dep_id}
    </select>
    <select id="findVO" resultMap="voMap" parameterType="java.lang.Integer">
        select a.*,b.dep_name from account a left join department b on a.dep_id = b.dep_id
        where a.id = #{id}
    </select>
<!--    分步查询 1-->
    <select id="findByStep" parameterType="java.lang.Integer" resultMap="finByStepMap">
        select * from account where id= #{id}
    </select>
    <select id="findAllMap"  resultType="map">
        select * from account
    </select>
</mapper>