MyBatis input mapping and output mapping

Source: Internet
Author: User

Development steps:

1) configuration in the mapping file Usermapper,xml

2) Add method in interface Usermapper

3) Testing

Input mappings:

Requirement one.: Input wrapper type USERQUERYVO defines the customer class customer inherits the user class

Userqueryvo

<span style= "Font-family:courier new;font-size:14px;" >public class Userqueryvo {private customer customer;public void Setcustomer (Customer customer) {This.customer = Custo Mer;} Public Customer GetCustomer () {return customer;}} </span>

The customer class inherits the user class

<span style= "Font-family:courier new;font-size:14px;" >public class Customer extends user{}</span>

Configuration in the Usermapper.xml

<span style= "Font-family:courier new;font-size:14px;" ><!--Packaging type--><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>
the type of input here is the type of the customer that the wrapper type outputs

Defining methods in the Usermapper interface

<span style= "Font-family:courier new;font-size:14px;" >public list<customer> findalluserslist (userqueryvo userqueryvo);</span>
Output Mappings:

Requirements: Isolate the total number of records in the database user table

Configuration in the Usermapper.xml

<span style= "Font-family:courier new;font-size:14px;" ><select id= "Findusercount" resulttype= "int" >select count (*) from  user</select></span>
input type no output type is int

Defining methods in the Usermapper interface

<span style= "Font-family:courier new;font-size:14px;" >public int Findusercount ();</span>
Requirement: If an alias is used in a query statement!

<!--output type--><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>

The input type is an int output type is a referenced alias type this time using Resultmap

Defining methods in the Usermapper interface

Public  User finduserresulttype (int id);








Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

MyBatis input mapping and output mapping

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.