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

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 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误