执行上一个测试用例也没问题,新写了一个报了这个错误,再返回去执行前面没问题的test也开始报这个错

执行上一个测试用例也没问题,新写了一个报了这个错误,再返回去执行前面没问题的test也开始报这个错


https://img1.sycdn.imooc.com//climg/61ef8bd9096c6a6819060965.jpg

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package com.imooc.mybatis.entity;
 
public class Goods {
    private Integer goodsId;
    private String title;
    private String subTitle;
    private Float originalCost;
    private Float currentPrice;
    private Float discount;
    private Integer isFreeDelivery;
    private Integer categoryId;
 
    public Integer getGoodsId() {
        return goodsId;
    }
 
    public void setGoodsId(Integer goodsId) {
        this.goodsId = goodsId;
    }
 
    public String getTitle() {
        return title;
    }
 
    public void setTitle(String title) {
        this.title = title;
    }
 
    public String getSubTitle() {
        return subTitle;
    }
 
    public void setSubTitle(String subTitle) {
        this.subTitle = subTitle;
    }
 
    public Float getOrginalCost() {
        return originalCost;
    }
 
    public void setOrginalCost(Float orginalCost) {
        this.originalCost = orginalCost;
    }
 
    public Float getCurrentPrice() {
        return currentPrice;
    }
 
    public void setCurrentPrice(Float currentPrice) {
        this.currentPrice = currentPrice;
    }
 
    public Float getDiscount() {
        return discount;
    }
 
    public void setDiscount(Float discount) {
        this.discount = discount;
    }
 
    public Integer getIsFreeDelivery() {
        return isFreeDelivery;
    }
 
    public void setIsFreeDelivery(Integer isFreeDelivery) {
        this.isFreeDelivery = isFreeDelivery;
    }
 
    public Integer getCategoryId() {
        return categoryId;
    }
 
    public void setCategoryId(Integer categoryId) {
        this.categoryId = categoryId;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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;
 
public class MyBatisUtils {
    private static SqlSessionFactory sqlSessionFactory = null;
    static {
        Reader reader= null;
        try {
            reader = Resources.getResourceAsReader("mybatis-config.xml");
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
        catch (IOException e) {
            e.printStackTrace();
            throw new ExceptionInInitializerError(e);
        }
    }
    public static SqlSession openSession(){
        return sqlSessionFactory.openSession();
    }
    public static void closeSession(SqlSession sqlSession){
        if (sqlSession!=null){
            sqlSession.close();
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package com.imooc.mybatis;
 
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 = MyBatisUtils.openSession();
        Connection connection = sqlSession.getConnection();
        System.out.println(connection);
        MyBatisUtils.closeSession(sqlSession);
    }
    @Test
    public void testSelectAll(){
        SqlSession sqlSession = null;
        try{
            sqlSession=MyBatisUtils.openSession();
            List<Goods> list=sqlSession.selectList("goods.selectAll");
            for (Goods g:list){
                System.out.println(g.getTitle());
            }
        }catch (Exception e){
            throw e;
 
        }finally {
            MyBatisUtils.closeSession(sqlSession);
        }
    }
    @Test
    public void testSelectById(){
        SqlSession sqlSession = null;
        try{
            sqlSession=MyBatisUtils.openSession();
            Goods goods=sqlSession.selectOne("goods.selectById",1602);
            System.out.println(goods.getTitle());
        }catch (Exception e){
            throw e;
        }finally {
            MyBatisUtils.closeSession(sqlSession);
        }
    }
    @Test
    public void testSelectByPriceRange(){
        SqlSession sqlSession = null;
        try{
            sqlSession=MyBatisUtils.openSession();
            Map param = new HashMap();
            param.put("min",100);
            param.put("max",500);
            param.put("lmit",10);
            List<Goods> list=sqlSession.selectList("selectByPriceRange",param);
            for (Goods goods:list){
                System.out.println(goods.getTitle()+":"+ goods.getCurrentPrice());
            }
 
        }catch (Exception e){
            throw e;
        }finally {
            MyBatisUtils.closeSession(sqlSession);
 
        }
    }
}

相关代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?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>
</mapper>

相关代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <settings>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>
    <!--设置默认指向的数据库-->
    <environments default="dev">
        <!--配置环境,不同的环境不同的id名字-->
        <environment id="dev">
<!--            采用JDBC方式进行数据库事务管理-->
            <transactionManager type="JDBC"></transactionManager>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/babytun?useUnicode=true&amp;characterEncoding=UTF-8"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
 
        </environment>
    </environments>
    <mappers>
        <mapper resource="mappers/goods.xml"></mapper>
    </mappers>
</configuration>


正在回答

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

1回答

同学你好,单词拼写错误,将其修改一致后重新运行测试试下,需修改位置如下:

https://img1.sycdn.imooc.com//climg/61ef9bf609732cc307410504.jpg

祝学习愉快~

  • weixin_慕侠6347532 提问者 #1

    老师,不是这个问题,我修改了也是报这个错误;

    现在主要问题是无论执行哪个测试用例都会报这个错误

    2022-01-25 14:51:40
  • 同学你好,是的, 若配置文件中出现错误,则程序是无法正常运行的。配置文件中Map书写错误,将map修改为Map后重新运行。需要修改的位置如下:

    https://img1.sycdn.imooc.com//climg/61efa39e09a14bb802350034.jpg

    祝学习愉快~

    2022-01-25 15:15:48
  • 谢谢谢谢老师

    2022-01-25 16:06:53
问题已解决,确定采纳
还有疑问,暂不采纳

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

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

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

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

帮助反馈 APP下载

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

公众号

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

在线咨询

领取优惠

免费试听

领取大纲

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