重温MyBatis(六、注解之与四:基于注解的多对多关联映射)

33 篇文章 0 订阅
13 篇文章 0 订阅

AdminInfoMapper.java的代码

public interface AdminInfoMapper {
    
    /**
     * 注解配置===开始
     */

    @Select("select * from admin_info where id = #{id}")
    AdminInfo findAdminInfoByIdWithAnnotations(int id);             // 根据id查单表

    @Select("select * from admin_info where id = #{id}")
    @Results({
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "name", column = "name"),
            @Result(property = "systemFuncsList", column = "id",
            many = @Many(select = "com.springmvc.mapper.SystemFuncsMapper.findSystemFuncsByAiidWithAnnotations"))
    })
    AdminInfo findAdminInfoOfAllByIdWithAnnotations(int id);        // 根据id查两个表


    @Select("select * from admin_info where id " +
            "in (" +
            "select aiid from connect_ai_sf where sfid = #{id} " +
            ")")
    List<AdminInfo> findAdminInfoBySfidWithAnnotations(int sfid);   //


    /**
     *  注解配置===结束
     */
}

SystemFuncsMapper.java的代码:

public interface SystemFuncsMapper {
   
    /**
     * 注解配置===开始
     */

    @Select("select * from system_funcs where id = #{id}")
    SystemFuncs findSystemFuncsByIdWithAnnotations(int id);


    @Select("select * from system_funcs where id = #{id}")
    @Results({
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "name", column = "name"),
            @Result(
                    property = "adminInfoList",
                    column = "id",
                    many = @Many(select = "com.springmvc.mapper.AdminInfoMapper.findAdminInfoBySfidWithAnnotations")),
    })
    SystemFuncs findSystemFuncsOfAllByIdWithAnnotations(int id);





    @Select("select * from system_funcs where id " +
            "in (" +
            "select sfid from connect_ai_sf where aiid = #{id} " +
            ")")
    List<SystemFuncs> findSystemFuncsByAiidWithAnnotations(int aiid);


    /**
     *  注解配置===结束
     */
}

/**
     * 注解配置===开始
     */

//    @Test
    public void testFindSystemFuncsByIdWithAnnotations() {

        // 获得SystemFuncsMapper接口的代理对象
        SystemFuncsMapper systemFuncsMapper = sqlSession.getMapper(SystemFuncsMapper.class);
        // 直接调用接口的方法
        SystemFuncs systemFuncs = systemFuncsMapper.findSystemFuncsByIdWithAnnotations(1);

        System.out.println(systemFuncs.toString());
    }

//    @Test
    public void testFindSystemFuncsOfAllByIdWithAnnotations() {

        // 获得SystemFuncsMapper接口的代理对象
        SystemFuncsMapper systemFuncsMapper = sqlSession.getMapper(SystemFuncsMapper.class);
        // 直接调用接口的方法
        List<SystemFuncs> systemFuncsList = systemFuncsMapper.findSystemFuncsByAiidWithAnnotations(1);

        for(SystemFuncs systemFuncs : systemFuncsList){
            System.out.println(systemFuncs.toString());
        }
    }

//    @Test
    public void testFindSystemFuncsByAiidWithAnnotations() {

        // 获得SystemFuncsMapper接口的代理对象
        SystemFuncsMapper systemFuncsMapper = sqlSession.getMapper(SystemFuncsMapper.class);
        // 直接调用接口的方法
        SystemFuncs systemFuncs= systemFuncsMapper.findSystemFuncsOfAllByIdWithAnnotations(1);

            System.out.println(systemFuncs.toString());
    }
    -----------------------------
    /**
     * 注解配置===开始
     */

//    @Test
    public void testFindAdminInfoByIdWithAnnotations() {

        // 获得SystemFuncsMapper接口的代理对象
        AdminInfoMapper adminInfoMapper = sqlSession.getMapper(AdminInfoMapper.class);
        // 直接调用接口的方法
        AdminInfo adminInfo = adminInfoMapper.findAdminInfoByIdWithAnnotations(1);

        System.out.println(adminInfo.toString());
    }

//    @Test
    public void testFindAdminInfoOfAllByIdWithAnnotations() {

        // 获得SystemFuncsMapper接口的代理对象
        AdminInfoMapper adminInfoMapper = sqlSession.getMapper(AdminInfoMapper.class);
        // 直接调用接口的方法
        AdminInfo adminInfo = adminInfoMapper.findAdminInfoOfAllByIdWithAnnotations(1);

        System.out.println(adminInfo.toString());
    }

        @Test
    public void testFindAdminInfoBySfidWithAnnotations() {

        // 获得SystemFuncsMapper接口的代理对象
        AdminInfoMapper adminInfoMapper = sqlSession.getMapper(AdminInfoMapper.class);
        // 直接调用接口的方法
        List<AdminInfo> adminInfoList = adminInfoMapper.findAdminInfoBySfidWithAnnotations(1);

        for (AdminInfo adminInfo : adminInfoList){
            System.out.println(adminInfo.toString());
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值