老师,为什么我Debug方法testInsert(),点goods上面没有显示属性与具体数值?

老师,为什么我Debug方法testInsert(),点goods上面没有显示属性与具体数值?

我的IDE界面:

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



慕课网的IDE界面:

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


代码如下:

<?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">
<mapper namespace="goods">
   <select id="selectAll" resultType="com.imooc.mybatis.entity.Goods">
       select * from t_goods order by goods_id desc limit 10
   </select>

   <select id="selectById" parameterType="Integer" resultType="com.imooc.mybatis.entity.Goods">
       select * from t_goods where goods_id = #{value}
   </select>
   
   <select id="selectByPriceRange" parameterType="java.util.Map" resultType="com.imooc.mybatis.entity.Goods">
       select * from t_goods
       where
         current_price between #{min} and #{max}
       order by current_price
       limit 0, #{limt}
   </select>

   <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"></result>
   </resultMap>
   <select id="selectGoodsDTO" resultMap="rmGoods">
       select g.*, c.* 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, category_id)
       VALUES (#{title}, #{subTitle}, #{originalCost}, #{currentPrice}, #{discount}, #{isFreeDelivery}, #{categoryId})
       <selectKey resultType="Integer" keyProperty="goodsId" order="AFTER">
         select last_insert_id
       </selectKey>
   </insert>
</mapper>



package com.imooc.mybatis.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.Reader;

/*
* MyBatisUtils工具类,创建全局唯一的SqlSessionFactory对象
* */
public class MyBatisUtils {
   //利用static(静态),属于类不属于对象,且全局唯一
   private static SqlSessionFactory sqlSessionFactory = null;
   //利用静态块在初始化类时,实例化sqlSessionFactory
   static{
       Reader reader = null;
       try {
           reader = Resources.getResourceAsReader("mybatis-config.xml");
       } catch (IOException e) {
           e.printStackTrace();
           //初始化错误时,通过抛出异常ExceptionInInitializerError通知调用者
           throw new ExceptionInInitializerError(e);
       }
       sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
   }

   /**
    * openSession 创建一个新的SqlSession对象
    * @return SqlSession对象
    */
   public static SqlSession openSession(){
       //默认SqlSession自动提交事务数据
       //设置false关闭自动提交,改为手动提交事务数据
       return sqlSessionFactory.openSession(false);
   }

   /**
    * 释放一个有效的SqlSession对象
    * @param session 准备释放SqlSession对象
    */
   public static void closeSession(SqlSession session){
       if(session != null){
           session.close();
       }
   }
}




package com.imooc.mybatis;

import com.imooc.mybatis.dto.GoodsDTO;
import com.imooc.mybatis.entity.Goods;
import com.imooc.mybatis.utils.MyBatisUtils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.Reader;
import java.sql.Connection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//JUNIT单元测试类
public class MyBatisTestor {
   @Test
   public void testSqlSessionFactory() throws IOException {
       //利用reade加载classpath下的mybatis-config.xml核心配置文件
       Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
       //初始化sqlSessionFactory对象,同时解析mybatis-config.xml文件
       SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
       System.out.println("SessionFactory加载成功!");
       SqlSession sqlSession = null;
       try {
           //创建SqlSession对象,SqlSession是JDBC的扩展类,用于与数据库交互
           sqlSession = sqlSessionFactory.openSession();
           //创建数据库连接(测试用)
           Connection connection = sqlSession.getConnection();
           System.out.println(connection);
       }catch(Exception e){
           e.printStackTrace();
       }finally {
           if(sqlSession != null){
               //如果type="POOLED",代表使用连接池,close则是将连接回收到连接池中
               //如果type="UNPOOLED",代表直连,close则会调用Connecton.close()
               sqlSession.close();
           }
       }
   }

   @Test
   public void testMyBatisUtils() throws Exception {
       SqlSession sqlSession = null;
       try {
           sqlSession = MyBatisUtils.openSession();
           Connection connection = sqlSession.getConnection();
           System.out.println(connection);
       }catch (Exception e){
           throw e;
       }finally {
           MyBatisUtils.closeSession(sqlSession);
       }
   }

   @Test
   public void testSelectAll() throws Exception {
       SqlSession session = null;
       try{
           session = MyBatisUtils.openSession();
           List<Goods> list = session.selectList("goods.selectAll");
           for(Goods g : list){
               System.out.println(g.getTitle());
           }
       }catch(Exception e){
           throw e;
       }finally {
           MyBatisUtils.closeSession(session);
       }
   }

   @Test
   public void testSelectById() throws Exception {
       SqlSession session = null;
       try{
           session = MyBatisUtils.openSession();
           Goods goods = session.selectOne("goods.selectById",1603);
           System.out.println(goods.getTitle());
       }catch(Exception e){
           throw e;
       }finally {
           MyBatisUtils.closeSession(session);
       }
   }

   @Test
   public void testSelectByPriceRange() throws Exception {
       SqlSession session = null;
       try{
           session = MyBatisUtils.openSession();
           Map param = new HashMap();
           param.put("min", 100);
           param.put("max", 500);
           param.put("limt", 10);
           List<Goods> list = session.selectList("selectByPriceRange", param);
           for(Goods g : list){
               System.out.println(g.getTitle() + ":" +g.getCurrentPrice());
           }
       }catch(Exception e){
           throw e;
       }finally {
           MyBatisUtils.closeSession(session);
       }
   }

   @Test
   public  void  testSelectGoodsMap() throws Exception {
       SqlSession session = null;
       try{
           session = MyBatisUtils.openSession();
           List<Map> list =  session.selectList("goods.selectGoodsMap");
           for(Map map : list){
               System.out.println(map);
           }
       }catch(Exception e){
           throw e;
       }finally {
           MyBatisUtils.closeSession(session);
       }
   }

   @Test
   public  void  testSelectGoodsDTO() throws Exception {
       SqlSession session = null;
       try{
           session = MyBatisUtils.openSession();
           List<GoodsDTO> list =  session.selectList("goods.selectGoodsDTO");
           for(GoodsDTO g : list){
               System.out.println(g.getGoods().getTitle());
           }
       }catch(Exception e){
           throw e;
       }finally {
           MyBatisUtils.closeSession(session);
       }
   }

   @Test
   public void testInsert() throws Exception {
       SqlSession session = null;
       try{
           session = MyBatisUtils.openSession();
           Goods goods = new Goods();
           goods.setTitle("测试商品");
           goods.setSubTitle("测试子标题");
           goods.setOriginalCost(200f);
           goods.setCurrentPrice(100f);
           goods.setDiscount(0.5f);
           goods.setIsFreeDelivery(1);
           goods.setCategoryId(43);
           //insert()方法返回值代表本次成功插入的记录总数
           int num = session.insert("goods.insert",goods);
           session.commit();//提交事务数据
           System.out.println(goods.getGoodsId());
       }catch(Exception e){
           if(session != null){
               session.rollback();//回滚事务
           }
       }finally {
           MyBatisUtils.closeSession(session);
       }
   }
}

正在回答

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

2回答

同学你好,同学是否有成功启动Debug,启动后界面如下所示:

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

建议同学选择Debug进行运行。然后进行测试。如下所示:

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

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

  • 慕粉1465475474 提问者 #1
    我启动了Debug,老师请看我顶部的截图,那个红色的圆点里面(中间)没有像教程一样打勾,并且我的控制台也不显示goodsId
    2020-08-19 22:32:47
  • 慕粉1465475474 提问者 #2
    控制台内容如下: "E:\Program Files\Java\jdk1.8.0_121\bin\java"。。。。。。 Connected to the target VM, address: '127.0.0.1:57085', transport: 'socket' Disconnected from the target VM, address: '127.0.0.1:57085', transport: 'socket' Process finished with exit code 0
    2020-08-19 22:35:53
  • 慕粉1465475474 提问者 #3
    我发现了,我在goods.xml的select last_insert_id()把函数的()忘记写了,哈哈,谢谢老师!
    2020-08-20 19:47:33
好帮手慕小尤 2020-08-20 10:09:32

同学你好,测试同学的代码是没有问题的,如下图所示:

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

可能是同学并未运行此方法导致的,则建议同学点击方法前的小三角,然后选择Debug。如下图所示:

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

祝学习愉快!

问题已解决,确定采纳
还有疑问,暂不采纳

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

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

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

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

帮助反馈 APP下载

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

公众号

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

在线咨询

领取优惠

免费试听

领取大纲

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