Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the correct posture for Mybatis to reset Criteria?

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, the editor will bring you about the correct posture of Mybatis reset Criteria. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

Mybatis reset Criteria

The development encounters a situation where the Example generated by Mybatis calls createCriteria () to create the Criteria and set the query criteria.

However, when you need to change the query condition to query again, you will find that calling the method again is "ineffective".

Seeing that most of the practices are queried through a new Example in new, and considering that it may not be the best or most reasonable approach, a simple study of this has been conducted.

The reasons for the problems are as follows

Let's look at the source code of the generated Exmaple:

Public Criteria createCriteria () {Criteria criteria = createCriteriaInternal (); if (oredCriteria.size () = = 0) {oredCriteria.add (criteria);} return criteria;} protected Criteria createCriteriaInternal () {Criteria criteria = new Criteria (); return criteria;}

Find a call to createCriteria (), which creates a new Criteria object at a time, but only

Protected List oredCriteria

This list will not be added until the length of the list is 0.

When you look at the mapped xml file, you find that the query condition is taken from oredCriteria.

Added to the collection only when or is called

Public void or (Criteria criteria) {oredCriteria.add (criteria);} what if we want to reset the query criteria?

There are two ways:

One is to clear only the oredCriteria property of Example.

Then you can get the list through the getOredCriteria () function in the Example class and clear it through the clear method.

Public List getOredCriteria () {return oredCriteria;}

A more thorough approach is to directly call the clear () method of Example to "reset" the exmple object to its initial state.

Public void clear () {oredCriteria.clear (); orderByClause = null; distinct = false; pageParam = null;} draw a conclusion

If you encounter problems, look more at the source code and the official documents.

Summary of Criteria usage of Mybatis

When using an one-to-many introverted query, some old friends proposed left join in, but my colleagues and I discussed the result that the code was written in various list and then stored in the database. In this way, the computing pressure is stored from the database to the server. When the concurrency is high, the benefit of doing so is reflected in the performance.

This is how the correct posture of the Mybatis reset Criteria is shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report