三、MyBatis系列:Mapper 映射 之 通過mapper對應檔來讀取資料庫記錄

來源:互聯網
上載者:User

標籤:

在使用Mybatis從資料庫中擷取記錄時,僅編寫一個映射xml檔案即可。這便是mybatis的強大之處,據說可以比普通jdbc要節省95%的代碼。
先來看看如何擷取記錄,工程配置請參考《一、MyBatis系列:第一個MyBatis工程》:

1、Mapper設定檔內容
 1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 3 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 4 <mapper namespace="test"> 5     <select id="getUserById" parameterType="int" resultType="map"> 6         SELECT * FROM user where id = #{value} 7     </select> 8     <select id="getUsers" resultType="map"> 9         SELECT * FROM user10     </select>11 </mapper>

理解一下節點元素:

  • mapper : 該對應檔的根節點,所有配置均填寫在內;
  • namespace :該映射所屬的命名空間;
  • select :表示用於讀取記錄的節;
    • id :命名空間下的唯一標識;
    • parameterType : 傳入該SQL語句的參數類型,可接收的類型有簡單類型,pojo類型,map 類型等;
    • resultType : 該語句的傳回型別,可以為簡單類型,pojo類型,map 類型等;
    • SELECT * FROM user : 使用者編寫的標準SQL語句,用於查詢想要的結果;
    • #{value} : 如果傳入的參數類型是一個簡單類型,那麼 value 可以用任何字母書寫;

注意 parameterType 是不接受多個參數的,如果存在多個參數,需要封裝為一個 POJO 類型。

2、寫一個單元測試讀取資料記錄
 1 @Test 2 public void getUsersTest() throws IOException { 3     InputStream stream = Resources.getResourceAsStream("MybatisConfig.xml"); 4     SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(stream); 5     SqlSession sqlSession = factory.openSession(); 6     try { 7         // 讀取一條記錄,返回  Map 結果類型. 8         Map<Object, Object> user = sqlSession.selectOne("test.getUserById", 1); 9         System.out.println(user);10         11         // 讀取多條記錄,返回 List<Map> 結果類型.12         List<Map<Object, Object>> users = sqlSession.selectList("test.getUsers");13         System.out.println(users);            14     } finally {15         sqlSession.close();16     }17 }

 查看日誌輸出:

DEBUG [main] - Logging initialized using ‘class org.apache.ibatis.logging.log4j.Log4jImpl‘ adapter.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - Opening JDBC Connection
DEBUG [main] - Created connection 603650290.
DEBUG [main] - Setting autocommit to false on JDBC Connection [[email protected]]
DEBUG [main] - ==>  Preparing: SELECT * FROM user where id = ?
DEBUG [main] - ==> Parameters: 1(Integer)
DEBUG [main] - <==      Total: 1
{birthday=2014-07-10, address=北京市, sex=2, id=1, username=王五, attrs={"參數1":"參數值1"}}
DEBUG [main] - ==>  Preparing: SELECT * FROM user
DEBUG [main] - ==> Parameters:
DEBUG [main] - <==      Total: 6
[{birthday=2014-07-10, address=北京市, sex=2, id=1, username=王五, attrs={"參數1":"參數值1"}}, {birthday=2014-07-10, address=北京市, sex=1, id=10, username=張三, attrs={"參數1":"參數值1"}}, ..... }]
DEBUG [main] - Resetting autocommit to true on JDBC Connection [[email protected]]
DEBUG [main] - Closing JDBC Connection [[email protected]]
DEBUG [main] - Returned connection 603650290 to pool.

 

三、MyBatis系列:Mapper 映射 之 通過mapper對應檔來讀取資料庫記錄

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.