zoukankan      html  css  js  c++  java
  • 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>

    2. <!-- 鉴别器 

    <discriminator javaType="">
    可以根据某列的值改变封装行为
    如果查询出是女生,则把部门查询出来
    如果是男生,把list_name这列赋值给email
    -->

    <select id="selectUserDis" resultMap="disMap">
            SELECT * FROM USER WHERE id =#{id}
        </select>
        <!-- 鉴别器 
            <discriminator javaType="">
             可以根据某列的值改变封装行为
             如果查询出是女生,则把部门查询出来
             如果是男生,把list_name这列赋值给email
        -->
        <resultMap type="user" id="disMap">
            <id column="id" property="id"/>
            <result column="email" property="email"/>
            <result column="last_name" property="lastName"/>
            <result column="gender" property="gender"/>
            <discriminator javaType="string" column="gender">
                <!-- 女生 -->
                <case value="0" resultType="user">
                    <association property="dept" select="mapper.DepartMapper.selectDepartById" column="{id=depart_id}" fetchType="eager">
                    </association>
                </case>
                <!-- 男生 -->
                <case value="1" resultType="user">
                    <id column="id" property="id"/>
                    <result column="last_name" property="email"/>
                    <result column="last_name" property="lastName"/>
                    <result column="gender" property="gender"/> 
                </case>
            </discriminator>
        </resultMap>
  • 相关阅读:
    docker国内镜像地址
    springBoot+websocket集群系列知识
    多个idea项目使用同一个tomcat
    nginx+tomcat遇到的https重定向到http问题
    设置常用错误页面自定义显示
    mysql关于索引的一些零碎知识点(持续更新)
    Idea使用Lombok简化实体类代码
    mysql索引分类及实现原理
    使用SpringSession和Redis解决分布式Session共享问题
    HashMap ConcurrentHashMap解读
  • 原文地址:https://www.cnblogs.com/flgb/p/10280105.html
Copyright © 2011-2022 走看看