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

ProductInfoMapper.java的代码:

public interface ProductInfoMapper {
     
/**
     * 注解配置===开始
     */
    @Select("select * from product_info where ptid = #{ptid}")
    List<ProductInfo> findProductInfoByPtidWithAnnotations(int ptid);   //  根据类型编号查询所有商品


    @Select("select * from product_info where id = #{id}")
    @Results({
     
            @Result(
                    property = "productType",
                    column = "ptid",
                    one = @One(select = "com.springmvc.mapper.ProductTypeMapper.findProductTypeByIdWithAnnotations")
//                    one = @One(select = "com.springmvc.mapper.ProductTypeMapper.findProductTypeByIdWithAnnotations2")
            )
    })
    ProductInfo findProductInfoByIdWithAnnotations(int id);             //  根据商品编号查询所有商品


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

ProductTypeMapper.java的代码:

public interface ProductTypeMapper {
     
 /**
     * 注解配置===开始
     */
    @Select("select * from product_type where id = #{id}")
    ProductType findProductTypeByIdWithAnnotations2(int id);


    @Select("select * from product_type where id = #{id}")
    @Results({
     
            @Result(
                    id = true,
                    property = "id",
                    column = "id"
            ),
            @Result(
                    property = "name",
                    column = "name"
            ),
            @Result(
                    property = "productInfoList",
                    column = "id",
                    many = @Many(select = "com.springmvc.mapper.ProductInfoMapper.findProductInfoByPtidWithAnnotations")
            )
    })
    ProductType findProductTypeByIdWithAnnotations(int id);


    /**
     *  注解配置===结束
     */
}
/**
     * 注解配置===开始
     */
//    @Test
    public void testFindProductInfoByPtidWithAnnotations() {
     
        ProductInfoMapper productInfoMapper = sqlSession.getMapper(ProductInfoMapper.class);
        // 直接调用接口的方法
        List<ProductInfo> productInfoList = productInfoMapper.findProductInfoByPtidWithAnnotations(1);
        // 打印输出结果
        for (ProductInfo productInfo : productInfoList) {
     
            System.out.println(productInfo.toString());
        }
    }


//    @Test
    public void testFindProductInfoByIdWithAnnotations() {
     
        ProductInfoMapper productInfoMapper = sqlSession.getMapper(ProductInfoMapper.class);
        // 直接调用接口的方法
        ProductInfo productInfo = productInfoMapper.findProductInfoByIdWithAnnotations(1);
        // 打印输出结果
        System.out.println(productInfo.toString());
    }

/**
     * 注解配置===开始
     */
    @Test
    public void testFindProductTypeByIdWithAnnotations() {
     
        ProductTypeMapper productTypeMapper = sqlSession.getMapper(ProductTypeMapper.class);
        // 直接调用接口的方法,查询编号为1的UserInfo对象
        ProductType productType = productTypeMapper.findProductTypeByIdWithAnnotations(1);
        // 打印输出结果
        System.out.println(productType.toString());
    }
    /**
     *
     *  注解配置===结束
     *
     */

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