(3)mapper接口類中添加相應方法
[java] view plain copy//用戶管理的Dao接口 public interface UserMapper { public User findUserByResultMap(int id) throws Exception; ...... } 測試:[java] view plain copy@Test public void testFindUserByResultMap() throws Exception{ SqlSession sqlSession=sqlSessionFactory.openSession(); //創建UserMapper代理對象 UserMapper userMapper=sqlSession.getMapper(UserMapper.class); //調用userMapper的方法 User user=userMapper.findUserByResultMap(1); System.out.println(user.getUsername()); } 測試結果:張三輸出日志:
[plain] view plain copyEBUG [main] - Opening JDBC Connection DEBUG [main] - Created connection 1465214. DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.Connection@165b7e] DEBUG [main] - ==> Preparing: select id _id,username _username from user where id=? DEBUG [main] - ==> Parameters: 1(Integer) DEBUG [main] - <== Total: 1 小結使用resultType進行輸出映射,只有查詢出來的列名和pojo中的屬性名一致,該列才可以映射成功。如果查詢出來的列名和pojo的屬性名不一致,通過定義一個resultMap對列名和pojo屬性名之間作一個映射關系。
轉載請注明出處:http://blog.csdn.net/acmman/article/details/46509375
新聞熱點
疑難解答