MyBatis database field and Entity object property name inconsistency solution

Source: Internet
Author: User

Database and object property name inconsistency is a very common problem, this time in accordance with the table field to the object property name matching map has been uncertain about this, the following are several solutions.

1. Turn on hump conversion

If the field name in the database is simply inconsistent with the object, such as the name is the same, but the separation method is not the same, in the database using an underscore delimited, and in the object using the hump separated, if it is such a child is not particularly troublesome, Only the hump conversion can be turned on in the MyBatis configuration file.

<setting name= "Mapunderscoretocamelcase" value= "true"/>

2. Use aliases in SQL query statements to fit

There is also the case that most of the cases are adaptable, but only a few of the columns fit, this time to define a separate resultmap feel a bit unworthy (in fact, from the maintainability of the definition resultmap is definitely preferred), So we use aliases in SQL statements when querying data to make database column names fit on the object's property names.

For example, in the database user name is called username, in the object username is called name, for various reasons we can not modify these two names but also to allow them to correspond, here is a possible solution:

<select id= "Loadbyid" parametertype= "string" resulttype= "user" >select ID, username as name, passwd from T_user wher E id=#{id}</select>

Even when you specify an alias, you can include a dot symbol in the alias to navigate, such as using Role.name to specify the name attribute of the role attribute injected into the user object, here is a simple example:

<select id= "Load" parametertype= "Long" resulttype= "Org.cc11001100.mybatis.domain.User" >    Select        T1.id, username, passwd, t3.id as ' role.id '        ,        t3.name as ' Role.name '    from        t_user as T1        JOIN T_user_ role as T2 on t1.id=t2.user_id        JOIN t_role as T3 on t3.id=t2.role_id    WHERE      t1.id=#{id};</select>

3. Custom Resultmap

Finally, the inconsistency is quite outrageous, basically most of the database fields and object names are not corresponding, in this case it is absolutely best to define a resultmap to solve the problem.

<select id= "Loadbyid" parametertype= "string" resultmap= "Userresultmap" >select * from T_user WHERE id=#{id}</ Select>

Then configure the mappings for the different fields in the Resultmap tab:

<resultmap id= "UserMap" type= "Org.cc11001100.mybatis.domain.User" automapping= "true" >    <id column= "id" property= "id"/>    <result column= "username" property= "name"/></resultmap>
Resultmap tag has a property called Automapping, when specified as true indicates that there is no manual use of the ID or the result tag to specify the mapping of the property is automatically mapped, so that the property is set to True, you can only specify the table field name and object property name does not match the fields , greatly reducing the workload.

.

MyBatis database fields and entity object property names are not consistent with the solution

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.