我用mybatis 3.2.3版本接口编程,实现查询
配置如下:
<select id="selectOnly" resultType="Student" parameterType="int">
select * from student where stuid = #{stuid}
</select>
然后定义接口
public interface IStudentDAO{
//方法名与配置文件中的id相同
Student selectOnly(int stuid);
}
接下来:
SqlSession session = sqlfactory.openSession();
IStudentDAO isdao = session.getMapper(IStudentDAO.class);
Student stu = isdao.selectOnly(id);
System.out.println(stu);
session.close();
注:sqlfactory不为空,测试过
然后运行时报错:
Exception in thread "main" org.apache.ibatis.binding.BindingException: Type interface com.lc.inter.IStudentDAO is not known to the MapperRegistry.
at org.apache.ibatis.binding.MapperRegistry.getMapper(MapperRegistry.java:42)
at org.apache.ibatis.session.Configuration.getMapper(Configuration.java:655)
at org.apache.ibatis.session.defaults.DefaultSqlSession.getMapper(DefaultSqlSession.java:218)
at com.lc.dao.StudentDao.TestInterfaceOnly(StudentDao.java:67)
at com.lc.dao.StudentDao.main(StudentDao.java:77)
求解答啊,困惑了很久了!
这应该是你的xml文件里的<mapper namespace="">配置错误这个要和你的IStudentDAO路径一样才行。