mybatis中mappers配置:
<mappers> <!-- <mapper resource="com/hxtg/sqlmapper/User.xml" /> --> <package name="com.hxtg.dao"/><!-- 用这种方式就不行了呢? --> </mappers>
这是官网的:
<!-- Register all interfaces in a package as mappers --> <mappers> <package name="org.mybatis.builder"/> </mappers>
本人的测试代码:
String resource = "Mybatis.xml"; SqlSession session = null; try { InputStream input = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(input); session = sqlSessionFactory.openSession(); UserDao userDao = session.getMapper(UserDao.class); List<User> users = userDao.getUserList(); for (User user : users) { System.out.println(user.getAccountid()); } System.out.println("SUCCESS"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ session.close(); }
解决方案:20分
让dao层的接口和xml文件保持同名,同路径,就可以了