多对一测试的时候无法初始化MyBatisUtils
之前写goods的时候测试还好好的,然后写了一对多的也可以运行,写完这个就说无法初始化了。。
MyBatisUtils
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(); } } }
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"> <environment id="dev"> <transactionManager type="JDBC"></transactionManager> <dataSource type="POOLED"> <property name="driver" value="com.mysql.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 resource="mappers/players.xml"/> <mapper resource="mappers/goods_detail.xml"/> <mapper resource="mappers/club.xml"/> </mappers> </configuration>
player.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="players"> <!-- 开启二级缓存 --> <cache eviction="LRU" flushInterval="600000" size="512" readOnly="true"/> <select id="selectAll" resultType="com.lzq.mybatis.entity.Player"> select * from student </select> <insert id="insert" parameterType="com.lzq.mybatis.entity.Player"> INSERT INTO student(reg_no,name, sex, age, grade, major) VALUES (#{regNo},#{name},#{sex},#{age},#{grade},#{major}) <selectKey resultType="Integer" keyProperty="id" order="AFTER"> select last_insert_id() </selectKey> </insert> <select id="dynamicSQL" parameterType="java.util.Map" resultType="com.lzq.mybatis.entity.Player"> select * from student <where> <if test="age != null"> and age > #{age} </if> <if test="sex != null"> and sex = #{sex} </if> </where> </select> <select id="selectById" resultType="com.lzq.mybatis.entity.Player" parameterType="String"> select * from student where clubno = #{value} </select> <resultMap id="rmPlayers" type="com.lzq.mybatis.entity.Player"> <id column="id" property="id"/> <association property="club" select="club.selectById"/> </resultMap> <select id="selectManyToOne" resultMap="rmPlayers"> select * from players limit 0,5 </select> </mapper>
club.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="club"> <select id="selectById" parameterType="String" resultType="com.lzq.mybatis.entity.Club"> select * from club where clubno = #{value} </select> <resultMap id="rmClub" type="com.lzq.mybatis.entity.Club"> <!-- 映射club对象的主键到goods_id字段 --> <id column="id" property="id"/> <!-- collection的含义是,在 select * from t_goods limit 0,1 得到结果后,对所有Goods对象遍历得到goods_id字段值, 并代入到goodsDetail命名空间的findByGoodsId的SQL中执行查询, 将得到的"商品详情"集合赋值给goodsDetails List对象. <collection property="goodsDetails" select="goodsDetail.selectByGoodsId" column="goods_id"/> --> <collection property="player" select="players.selectById" column="clubno"/> </resultMap> <select id="selectOneToMany" resultMap="rmClub"> select * from club limit 0,3 </select> </mapper>
测试单元
@Test public void testManyToOne1(){ SqlSession sqlSession = null; try{ sqlSession = MyBatisUtils.openSession(); List<Player> list = sqlSession.selectList("players.selectManyToOne"); for(Player player : list){ System.out.println(player.getName() + player.getClub().getCountry()); } }catch(Exception e){ e.printStackTrace(); }finally { MyBatisUtils.closeSession(sqlSession); } }
报错信息
12
收起
正在回答 回答被采纳积分+1
1回答
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星