MyBatis returns the mystery of null (Java Web) __mybatis

Source: Internet
Author: User

1. Description of the problem

I'm going to stick my mapping code.




There was a problem writing the project that asked me to return the string to the front end without a null value, that is, when the value is null I want to return to the empty string past. But that data is in the MySQL database is a null, then I will be extracted from those null value of the data to do some processing, so I brainwave.



According to the source code of the MyBatis, the MyBatis uses the reflection mechanism to get the set method of the Javabena to assign the property, then I modify the set method to make the return null. When I think of myself for this witty and complacent time, the reality of the bone hit me mercilessly.



The return value, like the figure, is useless. Is my set method fake? does not exist.

So I added a static variable A to Javaben.



Set method Plus



So the test results are:


There's nothing wrong with it. Output 10 Set methods also called 10 times what exactly is the problem ah .... It's going to crumble.


2. Problem analysis

For this set question, I thought about my printf method being added to a set method with no null field, so I'm adding



See how many times this setnickname method was invoked, and by the results of the previous return, nickname is 6 null,




It is obvious that the set method is only invoked 4 times, because the set method is not invoked when the MyBatis returns NULL. Because in the Restultmap source code, the default return is not an entity class is a map, when there is null, the map in the absence of this value is missing the value of the mapping call, so there is no call that set method, all null or null ...




3. The solution

1. I have a solution here. Since he doesn't call my set method, well you're strong still you're strong, I set the string value to initialize




So initialize it, and I'll look at the test results.



Well, all the null is gone.

2. If I am an iron head, my head is very iron, I will mybatis return time call my Set method,

There's a way, of course, to create configuration.xml (this is a spring-mybatis combination of configuration, if you mybatis configuration files can be added directly inside

The statements in <setting>

The contents are as follows:



Add such a statement to the Spring-mybatis



In such a configuration, you can have null also mapped inside the map to call the Set method (value is the path that you just created the file)

Test the results:


The set method was called 10 times, and Null was missing.


3. There is, of course, a way to get a default value for this null value by modifying the MyBatis source code. For example, if you return NULL, change null to "" so that you can set it in.

Transmission door: http://www.mamicode.com/info-detail-1647006.html

Concrete how to operate, too big on the I have not tried .... Interested can go to try


Summarize:

A beginner's little experience, a failed beginner .....


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.