我在视频中代码的基础上改写的代码 然后显示的数据为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());
所以例如运行这俩行代码的话就会报空指针的错误 恕我愚钝 麻烦老师看看有什么讲究
11
收起
正在回答 回答被采纳积分+1
3回答
shuaiyi
2019-11-03 20:06:55

这里是我的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>
3. Java 数据库开发与实战应用
- 参与学习 人
- 提交作业 357 份
- 解答问题 8016 个
本阶段将带你学习MySQL数据库,JDBC接口,MyBatis框架等,带你掌握的数据的存放和管理。
了解课程



恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星