我在视频中代码的基础上改写的代码 然后显示的数据为null 麻烦老师看看

我在视频中代码的基础上改写的代码 然后显示的数据为null 麻烦老师看看

@Test
public void testInsert() throws Exception {
    SqlSession session = null;
    int num = 0;
    try{
        session = MyBatisUtils.openSession();
        Goods g = new Goods();
        g.setTitle("测试商品");
        g.setSubTitle("测试子标题");
        g.setOriginalCost(200f);
        g.setCurrentPrice(100f);
        g.setDiscount(0.5f);
        g.setIsFreeDelivery(1);
        g.setCategoryId(43);
        //insert()方法返回值代表本次成功插入的记录总是
        num = session.insert("goods.insert",g);
        System.out.println("id为:"+g.getGoodsId());

        System.out.println(session.selectOne("goods.selectById",num));

        session.commit();//提交事务数据
    }catch (Exception e){
        if (session!=null) {
        }
        throw e;
    }finally {
        MyBatisUtils.closeSession(session);
    }
}

goods.selectById和goods.insert都是跟着视频中老师一起写的代码 我拿来改写之前并无报错

我就想把新插入的数据的id读出来然后根据id再反过来查找 但是以上代码是显示数据为null

Goods goods = session.selectOne("goods.selectById",1602);
System.out.println(goods.getTitle());

所以例如运行这俩行代码的话就会报空指针的错误   恕我愚钝  麻烦老师看看有什么讲究

正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

3回答
好帮手慕阿满 2019-11-04 09:59:40

同学你好,同学是想查询插入的数据是吗 ?这里selectOne传入的参数应该是goodsId,如:

http://img1.sycdn.imooc.com//climg/5dbf856f0921971409030187.jpg

另外如果Goods类没有重写toString()方法,需要重写一下,测试结果如:

http://img1.sycdn.imooc.com//climg/5dbf85d0099dc10711560049.jpg

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

提问者 shuaiyi 2019-11-03 20:06:55

http://img1.sycdn.imooc.com//climg/5dbec29d09f2e48416470901.jpg

这里是我的goods.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- namespace 命名空间 -->
<mapper namespace="goods">
    <!-- resultType 结果 说明了数据返回的对象是什么 -->
    <!-- 第3步 编写<select>SQL标签 -->
    <select id="selectAll" resultType="com.imooc.mybatis.entity.Goods">
        select * from t_goods order by goods_id desc limit 10
    </select>

    <!-- 单参数传递,使用parameterType指定参数的数据类型即可 SQL中的#{value}提取参数 -->
    <select id="selectById" parameterType="Integer" resultType="com.imooc.mybatis.entity.Goods">
        select * from t_goods where goods_id = #{value}
    </select>
    <!-- 多参数传递时,使用parameterType指定Map接口,SQL中#{key}提取参数 -->
    <select id="selectByPriceRange" parameterType="java.util.Map" resultType="com.imooc.mybatis.entity.Goods">
        select * from t_goods1
        where
          current_price between #{min} and #{max}
        order by current_price
        limit 0,#{limt}
    </select>
    <!-- 利用LinkedHashMap保存多表关联结果
        MyBatis会将每一条记录包装为LinkedHashMap对象
        key是字段名 value是字段对应的值 字段类型根据表结构进行自动判断
        优点:易于扩展和使用
        缺点:太过灵活,无法进行编译时检查
     -->
    <select id="selectGoodsMap" resultType="java.util.LinkedHashMap">
        select g.* ,c.category_name
        from t_goods g,t_category c
        where g.category_id = c.category_id;
    </select>
    <!-- 结果映射 -->
    <resultMap id="rmGoods" type="com.imooc.mybatis.dto.GoodsDTO">
        <!-- 设置主键字段与属性映射 -->
        <id property="goods.goodsId" column="goods_id"></id>
        <!-- 设置非主键字段与属性映射 -->
        <result property="goods.title" column="title"></result>
        <result property="goods.originalCost" column="original_cost"></result>
        <result property="goods.currentPrice" column="current_price"></result>
        <result property="goods.discount" column="discount"></result>
        <result property="goods.isFreeDelivery" column="is_free_delivery"></result>
        <result property="goods.categoryId" column="category_id"></result>
        <result property="category.categoryId" column="category_id"></result>
        <result property="category.categoryName" column="category_name"></result>
        <result property="category.parentId" column="parent_id"></result>
        <result property="category.categoryLevel" column="category_level"></result>
        <result property="category.categoryOrder" column="category_order"></result>
        <result property="test" column="test"/>
    </resultMap>
    <select id="selectGoodsDTO" resultMap="rmGoods">
        select g.* ,c.*,'1' as test
        from t_goods g,t_category c
        where g.category_id = c.category_id;
    </select>

    <insert id="insert" parameterType="com.imooc.mybatis.entity.Goods">
      insert into t_goods(title, sub_title, original_cost, current_price, discount, is_free_delivery)
      values (#{title},#{subTitle},#{originalCost},#{currentPrice},#{discount},#{isFreeDelivery});
      <selectKey resultType="Integer" keyProperty="goodsId" order="AFTER">
          select last_insert_id();
      </selectKey>
    </insert>
</mapper>


好帮手慕阿满 2019-11-03 11:30:45

同学你好,建议同学查看一下good.xml配置的insert语句中,是否有selectKey,如:

http://img1.sycdn.imooc.com//climg/5dbe49a409243e2613040253.jpg

这个是主键回填,将生成的主键回填到goods对象中。

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

  • 提问者 shuaiyi #1
    写了selectKey 显示了id号为多少 后面那句代码才报空指针错误 回复这里不能贴图 贴代码也不方便 所以我放回答里面了 麻烦老师看看
    2019-11-03 20:05:36
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师