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

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条回答 默认 最新

相关推荐 更多相似问题