mybatis三(关联查询)

一、类属性

@Alias("depart")
public class Department {

private Integer id;
private String departName;
private List<User> users;

@Alias("user")
public class User {

private int id;
private String lastName;
private String email;
private String gender;
private Department dept;

二、定义方法

public User selectUserByIdStep(Integer id);

三、mapper配置

<resultMap type="depart" id="dMap">
        <id column="depart_id" property="id" />
        <result column="depart_name" property="departName"/>
        <!-- collection的属性封装 -->
        <collection property="users" ofType="user">
            <id column="id" property="id" />
            <result column="last_name" property="lastName"/>
            <result column="email" property="email"/>
            <result column="gender" property="gender"/>
        </collection>
    </resultMap>
    <select id="selectDepartAndUser" resultMap="dMap">
        SELECT
        u.id,u.depart_id,u.email,u.gender,u.last_name,d.depart_name FROM USER
        u LEFT JOIN department d ON u.depart_id=d.id WHERE d.id=#{id}
    </select>

猜你喜欢

转载自www.cnblogs.com/flgb/p/10280105.html