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

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());
        }
    }

你可能感兴趣的:(Java,springmvc,mybatis,java,mybatis,mysql)