List<GoodsDTO> list = session.selectList("goods.selectGoodsDTO");有一行报错

List<GoodsDTO> list = session.selectList("goods.selectGoodsDTO");有一行报错

相关截图:

https://img1.sycdn.imooc.com//climg/6447854a0943917312180533.jpg

相关代码:

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;

public class MybatisTestor {
    @Test
    public  void testSqlSessionFactory() throws IOException {
        Reader reader=Resources.getResourceAsReader("mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(reader);
        System.out.println("SessionFactory加载成功");
        SqlSession sqlSession=null;
        try {
             sqlSession=sqlSessionFactory.openSession();
            Connection connection=sqlSession.getConnection();
            System.out.println(connection);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (sqlSession!=null){
                sqlSession.close();
            }
        }
    }
    @Test
    public  void testMyBatisUtils(){
        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(){
        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" , 1602);
            System.out.println(goods.getTitle());
        }catch (Exception e){
            throw e;
        }finally {
            MyBatisUtils.closeSession(session);
        }
    }

    @Test
    public void testSelectByPriceRange() throws Exception{
        SqlSession session=null;
        Map param=new HashMap<>();
        param.put("min",100);
        param.put("max",500);
        param.put("limt",10);
        try {
            session= MyBatisUtils.openSession();
            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(){
        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);
        }
    }

}


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

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

1回答
好帮手慕小蓝 2023-04-25 15:51:31

同学你好,同学提供的代码格式错乱,老师这里无法对代码进行格式化。请同学提供所有类的代码,并且按照以下方式以Java的格式提供,这样老师可以完整的复制同学的代码,确保不会出现偏差。

另外,建议同学提供报错信息时,以复制粘贴的形式提供完整的报错信息,如图中的报错信息,关键部分同学并没有截图进来,老师是无法根据报错信息去排查错误的。


祝学习愉快~https://img1.sycdn.imooc.com//climg/61f49f34090ea8a707790646.jpg

  • 提问者 万物生长5087351 #1
    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;
    
    public class MybatisTestor {
        @Test
        public  void testSqlSessionFactory() throws IOException {
            Reader reader=Resources.getResourceAsReader("mybatis-config.xml");
            SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(reader);
            System.out.println("SessionFactory加载成功");
            SqlSession sqlSession=null;
            try {
                 sqlSession=sqlSessionFactory.openSession();
                Connection connection=sqlSession.getConnection();
                System.out.println(connection);
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                if (sqlSession!=null){
                    sqlSession.close();
                }
            }
        }
        @Test
        public  void testMyBatisUtils(){
            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(){
            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" , 1602);
                System.out.println(goods.getTitle());
            }catch (Exception e){
                throw e;
            }finally {
                MyBatisUtils.closeSession(session);
            }
        }
    
        @Test
        public void testSelectByPriceRange() throws Exception{
            SqlSession session=null;
            Map param=new HashMap<>();
            param.put("min",100);
            param.put("max",500);
            param.put("limt",10);
            try {
                session= MyBatisUtils.openSession();
                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(){
            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);
            }
        }
    
    }


    2023-04-25 15:55:00
  • 同学你好,如上面回答,建议同学一并提供报错信息。

    祝学习愉快~

    2023-04-25 15:58:36
  • https://img1.sycdn.imooc.com//climg/6447854a0943917312180533.jpg

    https://img1.sycdn.imooc.com//climg/64478613096c326812170501.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="test" column="test"></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>
       </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>

    </mapper>


    2023-04-25 16:00:01
问题已解决,确定采纳
还有疑问,暂不采纳

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

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

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

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