In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains the "mybatis the same sql query the second time can not find the results how to do", the article explained the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in-depth, together to study and learn "mybatis the same sql query the second time can not find the results how to do" it!
Analysis of the problem that the same sql query can not find the result for the second time
Today, when I am doing company business, roughly the order number (buying card) and mobile phone number are sent from the user to bind the card. If it has been done, the status after the operation is returned. The problem is that if the project starts the first visit, the result will be returned normally, and the error that the card information can not be found on the second visit will be reported. But the code is the same, and the query statement is the same. Why is this the case? Looking all over Baidu, without success, I groped to find the problem.
Problem exploration
First of all, the query method was excluded, but failed; then look for problems in the code, found that traversing card information (multiple cards can be found), remove () operation will be carried out for those that have already been operated, comment out this line, the program has results, but will update the card information again, not the desired result; finally, it is found that there will be errors in the remove () operation on list, so it is associated with the cache of MyBatis.
Introduction to MyBatis caching
First-level cache: that is, session cache, and the scope is Session. After Session flush or close, all Cache in the Session will be emptied and enabled by default. Note: when integrating spring (using mybatis-spring):
Every time you query spring, the SqlSession is recreated, so the first-level cache is not valid.
When the transaction is started, the spring uses the same SqlSession for the query, so the first-level cache is valid in this case.
Secondary cache: that is, global cache, whose scope is Mapper (Namespace) and is turned off by default.
Cause of the problem
The caching mechanism of MyBatis is mentioned above. After looking at the project configuration, it is found that the problem is that the result of the first query will be put in the cache, and the program will remove the result list, so the list in the cache will change. The second query will look up the operated list from the cache (the memory address of the entity class returned by mybatis is the same), resulting in a result that we do not need. In the previous project, the integration with Spring is used, and the session is SqlSessionTemplate. Here, the first-level cache is turned off by default, but today's project is not integrated with Spring. The openSession () method of SqlsessionFactory is used to create session. When searching here, it is queried from the cache by default. To sum up, the second time we only find the data in the cache [2].
Solution
Since the data will be read from the cache for the second time, it is impossible to change the project configuration to turn off the first-level cache, so we can achieve what we need by refreshing the cache.
Scenario 1: refresh the cache through the SqlSessionUtils.getSqlSession (sqlSessionFactory) .clearCache () method
Scenario 2: add flushCache= "true" to the lookup statement corresponding to mapper.xml
Errors easily encountered in mybatis conditional query
When using mybatis's conditional query
It is easy to make this mistake if you are not careful.
19-Dec-2017 16 org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service 04 19-Dec-2017 38.742 serious [http-nio-8090-exec-6] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service () for servlet [SpringMVC] in context with path [] threw exception] Nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'endoscopeType' in' class java.lang.String'] with root cause org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'endoscopeType' in' class java.lang.String'at org.apache.ibatis.reflection.Reflector.getGetInvoker (Reflector.java:380) at org.apache.ibatis.reflection.MetaClass.getGetInvoker (MetaClass.java:170) at org.apache.ibatis .reflection. Wrapper.BeanWrapper.getBeanProperty (BeanWrapper.java:152) at org.apache.ibatis.reflection.wrapper.BeanWrapper.get (BeanWrapper.java:48)
This is the mapper.xml file:
SELECT * FROM endoscope endoscope_type = # {endoscopeType,jdbcType=VARCHAR}
The reason for the error is that when tagged, the endoscope attribute is not included in the String endoscopeType
Two solutions
1. Remove the label
two。 The incoming parameters are passed in the object
SELECT * FROM endoscope endoscope_type = # {endoscopeType,jdbcType=VARCHAR} Thank you for your reading. The above is the content of "what to do if you can't find the result the second time in the sql query with the same mybatis". After the study of this article, I believe you have a deeper understanding of what to do if you can't find the result the second time in the sql query with the same mybatis. The specific usage still needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.