一对多没问题,测试多对一对象关联映射遇到问题,代码都是粘贴源代码,运行还是由空指针异常

来源:3-2 ManyToOne对象关联查询

do_continue

2019-08-22 10:11:41

java.lang.NullPointerException???

goods_detail.xml中

<resultMap id="rmGoodsDetail" type="com.imooc.mybatis.entity.GoodsDetail">
   <id column="gd_id" property="gdId"/>
   <result column="goods_id" property="goodsId"/>
   <association property="goods" select="goods.selectById" column="goods_id"></association>
</resultMap>
<select id="selectManyToOne" resultMap="rmGoodsDetail">
   select * from t_goods_detail limit 0,20
</select>

goods.xml中

<select id="selectById" parameterType="Integer" resultType="com.imooc.mybatis.entity.Goods">
   select * from t_goods where goods_id = #{value }
</select>

GoodsDetail中加上了private Goods goods;以及get和set方法

public class GoodsDetail {
   private Integer gdId;
   private Integer goodsId;
   private String gdPicUrl;
   private Integer gdOrder;
   private Goods goods;

   public Integer getGdId() { return gdId; }
   public void setGdId(Integer gdId) { this.gdId = gdId; }

   public Integer getGoodsId() {
       return goodsId;
   }

   public void setGoodsId(Integer goodsId) {
       this.goodsId = goodsId;
   }

   public String getGdPicUrl() {
       return gdPicUrl;
   }

   public void setGdPicUrl(String gdPicUrl) {
       this.gdPicUrl = gdPicUrl;
   }

   public Integer getGdOrder() {
       return gdOrder;
   }

   public void setGdOrder(Integer gdOrder) {
       this.gdOrder = gdOrder;
   }

   public Goods getGoods() {
       return goods;
   }

   public void setGoods(Goods goods) {
       this.goods = goods;
   }
}

测试方法

/**
* 测试多对一对象关联映射
*/
@Test
public void testManyToOne() throws Exception {
   SqlSession session = null;
   try {
       session = MyBatisUtils.openSession();
       List<GoodsDetail> list = session.selectList("goodsDetail.selectManyToOne");
       for(GoodsDetail gd:list) {
           System.out.println(gd.getGdPicUrl()+": "+gd.getGoods().getTitle());
       }
   } catch (Exception e) {
       throw e;
   } finally {
       MyBatisUtils.closeSession(session);
   }
}


写回答

3回答

慕神9304019

2019-09-15

我也遇到了和你一样的问题,问题是在之前的删除那一课中,把商品表里的goods_id为739的记录删除了,结果导致在多对一课程中两张表goodsid对不上,也就是老师说的那个问题

http://img.mukewang.com/climg/5d7db9a309ec418805180225.jpg

http://img.mukewang.com/climg/5d7db9130935a62306940057.jpg

http://img.mukewang.com/climg/5d7db8bf0969e24f07640055.jpg

2
hkebono
h 对就是这个问题...我也试了好久
h020-04-26
共1条回复

好帮手慕柯南

2019-08-22

同学你好!

1.同学的428行是指这一行吗?

http://img.mukewang.com/climg/5d5e6354000157ff04440139.jpg

这是由于list的值为空,说明在数据库中没有查询到相关的数据,建议同学导入源码中老师所提供的数据库数据测试一下(导入时间较长,建议同学耐心等待)。

2.建议同学贴代码时以回答这个问题的方式贴出,回复里会改变代码的格式

如果我的回答解决了你的疑惑,请采纳,祝学习愉快~

0

好帮手慕柯南

2019-08-22

同学你好!

建议同学贴一下完整的报错信息,老师看一下。

祝学习愉快~

0
ho_continue
h java.lang.NullPointerException at com.imooc.mybatis.MyBatisTestor.testManyToOne(MyBatisTestor.java:428) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) 。。。
h019-08-22
共1条回复

0 学习 · 8016 问题

查看课程