突然报错测试不能正常运行
相关截图:
MyBatiUtils
相关代码:
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 MyBatiUtils { private static SqlSessionFactory sqlSessionFactory=null; //初始化静态对象 static { try (Reader reader = Resources.getResourceAsReader("mybatis-config.xml")) { sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); }catch (IOException e){ e.printStackTrace(); throw new ExceptionInInitializerError(e); } } //在后面利用sqlsession对数据库进行增删改查 public static SqlSession openSession(){ return sqlSessionFactory.openSession(); } public static void closeSession(SqlSession sqlSession){ if (sqlSession!=null){ sqlSession.close(); } } }
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"> <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="categoryName" column="category_Name"></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>
mybatis-config.xml相关代码:
<?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方式对数据库事务进行commit/rollback ,配置的是数据库的事务--> <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> <environment id="prd"> <!-- 采用JDBC方式对数据库事务进行commit/rollback --> <transactionManager type="JDBC"></transactionManager> <!--采用连接池方式管理数据库连接--> <dataSource type="POOLED"> <property name="driver" value="com.mysql.cj.jdbc.Driver"/> <property name="url" value="jdbc:mysql://192.168.1.155: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>
6
收起
正在回答 回答被采纳积分+1
1回答
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星