• association实现懒加载分段级联查询


    这里讲的是人员和部门进行级联分布查询

    1.首先在mybatis-config.xml里进行配置

    <settings>
    <!--显示的指定每个我们需要更改的配置的值,即使他是默认的。防止版本更新带来的问题 -->
    <setting name="lazyLoadingEnabled" value="true"/>
    <setting name="aggressiveLazyLoading" value="false"/>
    </settings>

    2 在department接口添加相关方法

    public Department getDeptById(Integer id);

    3在departmen映射文件中进行配置

    <!--public Department getDeptById(Integer id); -->
    <select id="getDeptById" resultType="com.atguigu.mybatis.bean.Department">
    select id,dname departmentName from department where id=#{id}
    </select>

    4在employee接口添加相关方法

    public Employee getEmpByIdStep(Integer id);

    5在departmen映射文件中进行配置

    <!-- id last_name email gender d_id -->
    <resultMap type="com.atguigu.mybatis.bean.Employee" id="MyEmpByStep">
    <id column="id" property="id"/>
    <result column="last_name" property="lastName"/>
    <result column="email" property="email"/>
    <result column="gender" property="gender"/>
    <!-- association定义关联对象的封装规则
    select:表明当前属性是调用select指定的方法查出的结果
    column:指定将哪一列的值传给这个方法

    流程:使用select指定的方法(传入column指定的这列参数的值)查出对象,并封装给property指定的属性
    -->
    <association property="dept"
    select="com.atguigu.mybatis.dao.DepartmentMapper.getDeptById"
    column="did">
    </association>
    </resultMap>
    <!-- public Employee getEmpByIdStep(Integer id);-->
    <select id="getEmpByIdStep" resultMap="MyEmpByStep">
    select * from tbl_employee where id=#{id}
    </select>

    6在junit进行测试

    Employee employee = mapper.getEmpByIdStep(1);
    System.out.println(employee.getEmail());

    运行结果如下:只有一条sql语句

    Employee employee = mapper.getEmpByIdStep(1);
    System.out.println(employee.getDept());

    运行结果如下:有二条sql语句

  • 相关阅读:
    JAVA中的继承
    各种浏览器设置背景颜色线性渐变的方式
    JavaScript原型链
    JavaScript运算符
    QQ聊天:终结编程语言和编程职业
    virutal dom patch
    关于编辑器和语言的一些启示
    node-webkit 资源
    我的程序,你的生活
    过早优化是万恶之源
  • 原文地址:https://www.cnblogs.com/zhangzhiqin/p/8548920.html
Copyright © 2020-2023  润新知