我是一颗小虎牙 2017-12-05 06:30 采纳率: 100%
浏览 19945
已采纳

java mybatis 多表联查,多个实体类,如何返回一个List?

如题,*Mapper.xml 里面我是这么写的,

 <resultMap id="BaseResultMap" type="com.steam.entity.Goods" >
        <id column="gid" property="gid" />
        <result column="gname" property="gname" jdbcType="VARCHAR" />
        <result column="desc" property="desc" jdbcType="VARCHAR" />
        <result column="price" property="price" jdbcType="INTEGER"/>
        <result column="status" property="status" jdbcType="INTEGER" />
        <result column="pname" property="pname" jdbcType="VARCHAR" />
        <result column="type_name" property="typeName" jdbcType="VARCHAR" />
        <result column="pid" property="pid" />
        <result column="tid" property="tid"/>
        <result column="model_number" property="modelNumber" jdbcType="VARCHAR" />
        <result column="goods_bianma" property="goodsBianma" jdbcType="VARCHAR" />
        <result column="type_name" property="typeName" jdbcType="VARCHAR" />
        <!-- <result column="goods_img" property="goodsImg" jdbcType="VARCHAR" />  -->
    </resultMap>

        <select id="goodsListAll" resultMap="BaseResultMap">
        select a.*,b.type_name,c.pname from tb_goods a left join tb_product b on a.tid=b.tid left join tb_brand c on a.pid=c.pid
    </select>

Controller层

 @RequestMapping(value="/goodsList",method=RequestMethod.GET)
    public String goodsListAll(HttpServletRequest request){

            List<Goods> glist = this.goodsService.goodsListAll();

            request.setAttribute("glist", glist);

            return "goods/goods_list";
    }

jsp页面:

 <c:forEach items="${glist}" var="g">
                        <tr class="text-c">
                            <td>${g.gid}</td>
                            <td>${g.gname}</td>
                            <td>${g.desc}</td>
                            <td>${g.plist.typeName}</td>
                            <td>${g.price}</td>
                            <td>${g.blist.pname}</td>
                            <td>${g.modelNumber}</td>
                            <td>${g.goodsBianma}</td>
                    </tr>

实体类

 public class Goods {
    private Long gid;

    private String gname;

    private String desc;

    private Double price;

    private Integer status;

    private Long pid;

    private Long tid;

    private String modelNumber;

    private String goodsBianma;


    private List<Product> plist;

    private List<Brand> blist;

还有两个实体类就是Product和Brand,不多说了,现在死活就是不好使,求各位大神帮忙看看

  • 写回答

6条回答 默认 最新

  • xiaozhutou_love 2017-12-05 06:33
    关注

    自己重新定义一个实体类 吧查询结果放到这个实体类中,实体类包含所有的查询结果的字段

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 操作系统相关算法中while();的含义
  • ¥15 CNVcaller安装后无法找到文件
  • ¥15 visual studio2022中文乱码无法解决
  • ¥15 关于华为5g模块mh5000-31接线问题
  • ¥15 keil L6007U报错
  • ¥15 webapi 发布到iis后无法访问
  • ¥15 初学者如何快速上手学习stm32?
  • ¥15 如何自动更换布娃娃图片上的衣服
  • ¥15 心理学eprime编程
  • ¥15 arduino esp8266开发