Many-to-configure MyBatis

Note: This needs to check out the data using an alias name, so we packaged entity classes when they could use, because the database alias is generated randomly

 select e.id,e.name,e.age,d.id did,d.name dname  from employee e
        left join dept d
        on  e.dept_id = d.id

id and name did, dname sector is

There is also need to use resultMap, instead of using resulttype

There are two ways many to

<? Xml Version = "1.0" encoding = "UTF-8" ?> 
<! DOCTYPE Mapper 
        the PUBLIC "- // mybatis.org//DTD Mapper 3.0 // EN" 
        "http://mybatis.org/dtd/mybatis mapper.dtd--3 " > 

<-! namespace namespace ensure that it represents a unique ID = + cn.itsource.mybatis.dao.impl.ProductDaoImpl" getUserById " -> 
< Mapper namespace =" _ 03_manytoone.EmployeeMapper " > 


    <! - many-to-operate simultaneously query a department employee query SQL -> 
    < the SELECT the above mentioned id = "query01" resultMap = "employeeMp" > 
        the SELECT e.id, e.name, e.age, d.id DID, d.name dname  from employee ename dname  from employee e
        left join dept d
        on  e.dept_id = d.id
    </select>
    <!--id 结果Map的值 type:返回类型 (嵌套结果一条sql)-->
   <resultMap id="employeeMp" type="_03_manytoone.Employee">
        <id property="id" column="id"></id>
        <result property="name" column="name"></result>
        <result property="age" column="age"></result>
        <association property= "Dept" the javaType = "_ 03_manytoone.Dept" > 
            < ID Property = "ID" column = "DID" > </ ID > 
            < Result Property = "name" column = "DNAME" > </ Result > 
        </ Association > 
    </ the resultMap > 

    <-! many-nested query operations (query dept_id out value, in addition to what the query according to the value dept_id) 1 + N Article SQL -> 
    < SELECT ID = "query02" the resultMap = "employeeMp1"  >
        select *  from employee e
    </select>
    <resultMap id="employeeMp1" type="_03_manytoone.Employee">
        <id property="id" column="id"></id>
        <result property="name" column="name"></result>
        <result property="age" column="age"></result>
        <association property="dept" column="dept_id" select="getDeptById">
        </association>
    </resultMap>
    <select id="getDeptById" parameterType="long" resultType="_03_manytoone.Dept" >
        select *  from dept where id=#{id}
    </select>





</mapper>
View Code

 

Guess you like

Origin www.cnblogs.com/xiaoruirui/p/11776305.html