MyBatis field names conflict with class property names

Source: Internet
Author: User

Create a new table

CREATE TABLEOrders (order_idINT PRIMARY KEYAuto_increment,order_noVARCHAR( -), Order_priceFLOAT);INSERT  intoOrders (Order_no, Order_price)VALUES('AAAA', at);INSERT  intoOrders (Order_no, Order_price)VALUES('bbbb', -);INSERT  intoOrders (Order_no, Order_price)VALUES('CCCC', A);

and build a JavaBean.

 Public class Order {privateint  ID; Private String OrderNo; Private float Price ;}

Creating a mapping File

<id= "GetOrder"  parametertype= "int"  resulttype= "Order">        select * from Orders where Order_id=#{id}</Select >

This time because JavaBean inside the ID and the database inside the order_id does not correspond, after the execution is returned by the database to obtain the name of the field value, the bean does not have these field names, so the assignment failed.

There are two ways to resolve this:

Way one: By defining an alias in an SQL statement

<id= "Selectorder"  parametertype= "int"  resulttype = "_order" > Select order_id ID, order_no orderno,order_price price from orders where Order_id=#{id} </ Select >

Way two: Define a map

<SelectID= "GetOrder2"ParameterType= "int"Resultmap= "Getorder2map">SELECT * FROM Orders where Order_id=#{id}</Select>    <Resultmaptype= "Order"ID= "Getorder2map">        <ID Property= "id"column= "order_id"/>        <result Property= "OrderNo"column= "Order_no"/>        <result Property= "Price"column= "Order_price"/></Resultmap>

  

MyBatis field names conflict with class property names

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.