1. 程式人生 > >Mybatis進階學習筆記——輸出映射

Mybatis進階學習筆記——輸出映射

字段 str integer ima println myba get 實現類 動態代理

輸出映射(例如一個方法的返回至使用什麽類型去接收)

1.基本類型

1     <!-- 統計記錄數 -->
2     <select id="queryTotalCount" resultType="long">
3         SELECT COUNT(*) FROM t_customer
4     </select>

1 public Long queryTotalCount();

    /**
     * 輸出映射
     */
    @Test
    public void test2() {
        SqlSession sqlSession 
= SessionUtils.getSession(); // getMapper(): 返回指定接口的動態代理的實現類對象 CustomerDao dao = sqlSession.getMapper(CustomerDao.class); Long count = dao.queryTotalCount(); System.out.println(count); sqlSession.commit(); sqlSession.close(); }

2.JavaBean類型(*常用類型)

1
<select id="queryCustomer" parameterType="int" resultType="Customer"> 2 SELECT * FROM t_customer WHERE id=#{value} 3 </select>

1 public Customer queryCustomer(Integer id);

 1     /**
 2      * 輸出映射
 3      */
 4     @Test
 5     public void test2() {
 6         SqlSession sqlSession = SessionUtils.getSession();
7 // getMapper(): 返回指定接口的動態代理的實現類對象 8 CustomerDao dao = sqlSession.getMapper(CustomerDao.class); 9 Customer c = dao.queryCustomer(1); 10 System.out.println(c); 11 sqlSession.commit(); 12 sqlSession.close(); 13 }

3.ResultMap類型(用於解決表的字段名稱和實體類的屬性名稱不一致的情況)

resultType使用要求:JavaBean中的屬性名要和數據庫字段名保持一致。

 1     <!-- 定義ResultMap -->
 2     <!-- 
 3         含義說明:
 4         type:我們需要封裝成的實體類
 5         id:定義的名稱,供下方代碼使用
 6      -->
 7     <resultMap type="CustomerRM" id="customerResultMap">
 8         <!-- 
 9             id:映射主鍵
10             column:數據庫字段
11             property:實體類中的命名
12          -->
13         <id column="id" property="custId"/>
14         <result column="name" property="custName"/>
15         <result column="gender" property="custGender"/>
16         <result column="telephone" property="custTelephone"/>
17     </resultMap>
1     <select id="queryCustomerResultMap" parameterType="int" resultMap="customerResultMap">
2     <!-- resultMap:方法的返回值類型,也就是上方定義的type="CustomerRM"中的CustomerRM對象 -->
3         SELECT * FROM t_customer WHERE id=#{value}
4     </select>
 1     /**
 2      * 輸出映射
 3      */
 4     @Test
 5     public void test2() {
 6         SqlSession sqlSession = SessionUtils.getSession();
 7         // getMapper(): 返回指定接口的動態代理的實現類對象
 8         CustomerDao dao = sqlSession.getMapper(CustomerDao.class);
 9         CustomerRM c =  dao.queryCustomerResultMap(1);
10         System.out.println(c);
11         sqlSession.commit();
12         sqlSession.close();
13     }

CustomerRM.java:

 1 package cn.sm1234.domain;
 2 
 3 public class CustomerRM {
 4 
 5     private Integer custId;
 6     private String custName;
 7     private String custGender;
 8     private String custTelephone;
 9     public Integer getCustId() {
10         return custId;
11     }
12     public void setCustId(Integer custIid) {
13         this.custId = custIid;
14     }
15     public String getCustName() {
16         return custName;
17     }
18     public void setCustName(String custName) {
19         this.custName = custName;
20     }
21     public String getCustGender() {
22         return custGender;
23     }
24     public void setCustGender(String custGender) {
25         this.custGender = custGender;
26     }
27     public String getCustTelephone() {
28         return custTelephone;
29     }
30     public void setCustTelephone(String custTelephone) {
31         this.custTelephone = custTelephone;
32     }
33     @Override
34     public String toString() {
35         return "CustomerRM [custId=" + custId + ", custName=" + custName + ", custGender=" + custGender
36                 + ", custTelephone=" + custTelephone + "]";
37     }
38     
39 }

數據庫字段截圖:

技術分享圖片

Mybatis進階學習筆記——輸出映射