mybatis 輸入映射和輸出映射

來源:互聯網
上載者:User

標籤:mybatis

開發步驟:

1)對應檔UserMapper,xml中進行配置

2)介面UserMapper中增加方法

3)測試

輸入映射:

需求一.:輸入封裝類型 UserQueryVo中定義Customer類 Customer繼承User類

UserQueryVo

<span style="font-family:Courier New;font-size:14px;">public class UserQueryVo {private Customer customer;public void setCustomer(Customer customer) {this.customer = customer;}public Customer getCustomer() {return customer;}}</span>

Customer類繼承User類

<span style="font-family:Courier New;font-size:14px;">public class Customer extends User{}</span>

UserMapper.xml中的配置

<span style="font-family:Courier New;font-size:14px;"><!-- 封裝類型的 --><select id="findAllUsersList" parameterType="cn.itcast.domain.UserQueryVo" resultType="cn.itcast.domain.Customer">select * from user  where user.sex=#{customer.sex} and user.username like "%${customer.username}%"</select></span>
這裡的輸入類型是封裝類型 輸出的是Customer類型

UserMapper介面中定義方法

<span style="font-family:Courier New;font-size:14px;">public List<Customer> findAllUsersList(UserQueryVo userQueryVo);</span>
輸出映射:

需求: 查出資料庫user表中的總記錄數

UserMapper.xml中的配置

<span style="font-family:Courier New;font-size:14px;"><select id="findUserCount" resultType="int">select count(*)  from user</select></span>
輸入類型沒有 輸出類型是int

UserMapper介面中定義方法

<span style="font-family:Courier New;font-size:14px;">public int findUserCount();</span>
需求:如果查詢語句中使用別名!

<!-- 輸出類型 --><resultMap type="user" id="userId"><id column="id_" property="id"/><result column="username_" property="username"/></resultMap><select id="findUserResultType" parameterType="int" resultMap="userId">select id id_,username username_ from user where id =#{id}</select>

輸入類型是int 輸出類型是引用的別名資料型別這個時候使用resultMap

UserMapper介面中定義方法

public  User findUserResultType(int id);








著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

mybatis 輸入映射和輸出映射

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.