执行上一个测试用例也没问题,新写了一个报了这个错误,再返回去执行前面没问题的test也开始报这个错
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; } }
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(); } } }
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); } } }
相关代码:
<?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>
相关代码:
<?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&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>
16
收起
正在回答
1回答
同学你好,单词拼写错误,将其修改一致后重新运行测试试下,需修改位置如下:
祝学习愉快~
相似问题
登录后可查看更多问答,登录/注册
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星