mybatis--field names and entity class attribute names conflict resolution

Source: Internet
Author: User

First, create the order form

order_id Order_name Order_price

1 Apples 6.5

2 Banana 3

3. Orange 2

Ii. Definition of entity classes

Public Order

{

Private Integer ID;

private String name;

private float price;

}

C. xml file code

The data XML is not found as follows

<mapper namespace= "Me.gacl.mapping.orderMapper" > <!--to get an order object based on the ID query, the query is not the result we want, Because the attribute name of the entity class does not correspond to the field name of the database, the corresponding record cannot be queried--<select id= "Getorderbyid" parametertype= "int" resulttype= "me.g Acl.domain.Order "> select * from Orders where Order_id=#{id} </select> </mapper>

The solution is as follows, method one

Directly sets the returned database field name to the same as the attribute name of the entity class, and the return type (Resulttype) is set directly to the received entity class name.

<mapper namespace= "Me.gacl.mapping.orderMapper" > <!--to get an order object based on the ID query, the query can be used to query the results we want, This is because we will query the field name of one and the entity class property name of the same alias, so that the attribute name of the entity class and the field names in the query results can correspond to--<select id= "Selectorder" parametertype= "int" resulttype= "Me.gacl.domain.Order" > select order_id ID, order_name name,order_price price from orders W Here Order_id=#{id} </select> </mapper>

Method Two

Note that this is a resultmap conversion, so change the Resulttype to Resultmap.

<mapper namespace= "Me.gacl.mapping.orderMapper" >           <!--          Get an Order object based on the ID query, The use of this query is normal query to the results we want,          because we have <resultMap> Map entity class Property name and table field name one by one correspondence relationship      -->    <select id= " Selectorderresultmap " parametertype=" int " resultmap=" Orderresultmap ">              select * from orders where  order_id=#{id}    </select>        <!--through <resultMap> mapping entity class Property names and table field names corresponds to the relationship  -->    <resultmap type= " Me.gacl.domain.Order " id=" Orderresultmap ">        <!--   Use the id attribute to map the primary key field  -->        <id property= "id"  column= "order_id"/>        <!--   Use the result property to map a non-primary key field  -->        <result property= " Name " column=" Order_name "/>        <result property=" Price " column=" Order_price "/>    </resultmap>         </mapper>

Summarize:

When using MyBatis query operation can not query the corresponding results, but pure SQL can also detect data, you need to consider whether the field corresponding entity class Property problems, the solution has the following two points

1> the alias of the field name to the name of the entity class by defining the alias of the field name in the SQL Statement of the query , so that the field name of the table corresponds to the attribute name one by one of the entity class, which is An alias is defined in an SQL statement to resolve the mapping of the field name and property name.

2> maps the one by one correspondence between field names and entity class property names through <resultMap>. This approach uses the workaround provided by MyBatis to resolve the mapping of field names and property names.




mybatis--field names and entity class attribute names conflict resolution

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.