mybatis做简单的查询怎么总是报空指针
<?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEmapperPUBLIC"-//mybatis.org//DTDMapper3....
<?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="com.mxl.bean.TeacherMapper">
<select id="getTeacher" parameterType="int" resultType="Teacher">
select t_id,t_name from teacher where t_id= #{tid}
</select>
</mapper>
@Test
public void testTeacher(){
SqlSession session = MybatisUtil.getSesssion();
String statem = "com.mxl.bean.TeacherMapper.getTeacher";
Teacher t = session.selectOne(statem,1);
System.out.println(t.toString());
}
控制台输出
DEBUG 11-15 23:39:50,392 ==> Preparing: select t_id,t_name from teacher where t_id= ? (BaseJdbcLogger.java:139)
DEBUG 11-15 23:39:50,435 ==> Parameters: 1(Integer) (BaseJdbcLogger.java:139)
DEBUG 11-15 23:39:50,458 <== Total: 1 (BaseJdbcLogger.java:139)
sql语句在数据库里可以执行 展开
<mapper namespace="com.mxl.bean.TeacherMapper">
<select id="getTeacher" parameterType="int" resultType="Teacher">
select t_id,t_name from teacher where t_id= #{tid}
</select>
</mapper>
@Test
public void testTeacher(){
SqlSession session = MybatisUtil.getSesssion();
String statem = "com.mxl.bean.TeacherMapper.getTeacher";
Teacher t = session.selectOne(statem,1);
System.out.println(t.toString());
}
控制台输出
DEBUG 11-15 23:39:50,392 ==> Preparing: select t_id,t_name from teacher where t_id= ? (BaseJdbcLogger.java:139)
DEBUG 11-15 23:39:50,435 ==> Parameters: 1(Integer) (BaseJdbcLogger.java:139)
DEBUG 11-15 23:39:50,458 <== Total: 1 (BaseJdbcLogger.java:139)
sql语句在数据库里可以执行 展开
2015-11-16
展开全部
哪个语句报空null异常的。。。。。。。
更多追问追答
追问
Teacher t = session.selectOne(statem,1);
返回对象为空,打印的sql语句在数据库里面能查出来
追答
那就是说明session未创建好
去这里面 MybatisUtil.getSesssion(); 找原因
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询