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

The usage of parsing package in mybatis

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly introduces "the usage of parsing package in mybatis". In daily operation, I believe many people have doubts about the usage of parsing package in mybatis. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "the usage of parsing package in mybatis"! Next, please follow the editor to study!

Private final String openToken; private final String closeToken; private final TokenHandler handler; public GenericTokenParser (String openToken, String closeToken, TokenHandler tokenHandler) {this.openToken = openToken; this.closeToken = closeToken; this.handler = tokenHandler;} public String parse (String text) {StringBuilder builder = new StringBuilder () / / if the string passed in has a value if (text! = null & & text.length () > 0) {/ / convert the string to a character array char [] src = text.toCharArray (); int offset = 0 / / determine the position of openToken in text. Note that the return value of indexOf function-1 indicates that it does not exist, 0 means int start = text.indexOf (openToken, offset) at the beginning; / / # {favouriteSection,jdbcType=VARCHAR} / / here is the loop parsing parameter, refer to GenericTokenParserTest, for example, you can parse ${first_name} ${initial} ${last_name} reporting. Such a string contains three ${} / / to verify whether the start is greater than-1 (that is, there is an open token substring in the given parameter text). If it is greater than-1 (open loop), verify whether the first character in the start position of the given text is "\" (backslash). If it is a backslash, it means that the obtained parameter is masked. We need to remove the backslash and relocate the token. Of course, if it is not backslash, the parameter is normal while (start >-1) {/ / determine whether ${is preceded by a backslash. This logic does not exist in the old version of mybatis (start > 0 & & src [start-1] = ='\\') {/ / the variable is escaped. Remove the backslash. / / the new version no longer calls substring, but calls the following offset instead, which improves the efficiency. / / if there is an escape character before openToken in text, the escape character is removed. If there is an escape character before openToken, the value of start must be greater than 0, and the minimum is 1 / / because openToken does not need to be processed at this time, so there is no need to deal with endToken. Then find the next openToken builder.append (src, offset, start-offset-1) .append (openToken); offset = start + openToken.length () / / reset offset} else {/ / get the last position end of the first matching substring. If end is-1, it means that there is no closeToken, then get all the strings before the last bit end and relocate the offset to the length of the src array. If the end value is not-1, it means that there is a closing tag closeToken int end = text.indexOf (closeToken, start) in the text string. / / if openToken does not exist, add the characters after the offset position directly to the builder if (end = =-1) {builder.append (src, offset, src.length-offset); offset = src.length / / reset offset} else {/ / get the substring before the start tag, and relocate the offset offset (the length of the start+ start tag = the starting position of the specific parameter), get the parameter string as content, and then call the handleToken () method of TokenHandler to process the obtained parameter string (such as replacing parameters, etc.) Then add the processed string to the previous substring and reposition the offset offset to the next bit of the closing tag (end+closeToken length = end+1) builder.append (src, offset, start-offset) Offset = start + openToken.length (); / / reset offset String content = new String (src, offset, end-offset); / / call handler.handleToken after getting a pair of strings in curly braces, such as builder.append (handler.handleToken (content)) Offset = end + closeToken.length (); / / reset offset}} / / get the start position of the next openToken in the text, reset the start, perform steps 3 to 6 of the loop body, and process each parameter until the last parameter, and the loop ends with start = text.indexOf (openToken, offset) } / / verify the length of the offset offset and src array. If the offset is small, the original string still has some parts not added to the new string. Add the rest at the end to the new string, and then return the new string. If the offset is not less than the array length of src, directly return the new string if (offset < src.length) {builder.append (src, offset, src.length-offset). }} return builder.toString ();} at this point, the study of "the use of parsing packages in mybatis" is over. I hope I can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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

Internet Technology

Wechat

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

12
Report