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

Interceptor pagination of MyBatis

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

Lu Chunli's work notes are not as good as bad notes.

The paging of the database mainly includes physical paging and logical paging.

Physical paging: the database itself provides paging methods, such as MySQL's limit, Oracle's rownum, SqlServer's top, the advantage is high efficiency, the downside is that different databases have different query methods.

Logical paging: all records are queried from the database and stored in memory, and then the data is directly obtained from memory and screened for pagination. The advantage is that the query method can be unified, but the downside is inefficiency. Because every time all the data have to be queried and then processed.

Paging techniques commonly used in orm frameworks:

①: hibernate uses physical paging

②: MyBatis uses RowBounds to implement logical paging, that is, query all the data records first, and then truncate the records according to offset and limit.

MetaObject

Org.apache.ibatis.reflection.MetaObject is a utility class provided by Mybatis, and Mybatis often uses this object in sql parameter settings and result set mappings.

Attributes:

/ / original object private Object originalObject; / / A parent class private ObjectWrapperFactory objectWrapperFactory of a parent class private ObjectFactory objectFactory; / / org.apache.ibatis.reflection.wrapper.DefaultObjectWrapperFactory that encapsulates private ObjectWrapper objectWrapper; / / org.apache.ibatis.reflection.factory.DefaultObjectFactory of a pair of original objects

Methods:

/ / used to wrap the object MetaObject forObject (Object object,ObjectFactory objectFactory, ObjectWrapperFactory objectWrapperFactory) / / to get the value of the property (method that supports OGNL) Object getValue (String name) / / to set the value of the property (method that supports OGNL) void setValue (String name, Object value)

Construction method

Private MetaObject (Object object, ObjectFactory objectFactory, ObjectWrapperFactory objectWrapperFactory) {this.originalObject = object; this.objectFactory = objectFactory; this.objectWrapperFactory = objectWrapperFactory; if (object instanceof ObjectWrapper) {this.objectWrapper = (ObjectWrapper) object;} else if (objectWrapperFactory.hasWrapperFor (object)) {this.objectWrapper = objectWrapperFactory.getWrapperFor (this, object);} else if (object instanceof Map) {this.objectWrapper = new MapWrapper (this, (Map) object) } else if (object instanceof Collection) {this.objectWrapper = new CollectionWrapper (this, (Collection) object);} else {this.objectWrapper = new BeanWrapper (this, object);}}

ForObject method

Public static MetaObject forObject (Object object, ObjectFactory objectFactory, ObjectWrapperFactory objectWrapperFactory) {if (object = = null) {return SystemMetaObject.NULL_META_OBJECT;} else {return new MetaObject (object, objectFactory, objectWrapperFactory);}}

GetValue

Public Object getValue (String name) {PropertyTokenizer prop = new PropertyTokenizer (name); if (prop.hasNext ()) {MetaObject metaValue = metaObjectForProperty (prop.getIndexedName ()); if (metaValue = = SystemMetaObject.NULL_META_OBJECT) {return null;} else {/ / this is equivalent to a recursive call up to the last layer. For example, user.cust.custId / / the first recursive cust.custId / / the second recursive custId, this is the return metaValue.getValue to be returned for real access (prop.getChildren ());}} else {return objectWrapper.get (prop);}}

SetValue

Public void setValue (String name, Object value) {PropertyTokenizer prop = new PropertyTokenizer (name); if (prop.hasNext ()) {MetaObject metaValue = metaObjectForProperty (prop.getIndexedName ()); if (metaValue = = SystemMetaObject.NULL_META_OBJECT) {if (value = = null & & prop.getChildren ()! = null) {return / / don't instantiate child path if value is null} else {metaValue = objectWrapper.instantiatePropertyValue (name, prop, objectFactory);}} metaValue.setValue (prop.getChildren (), value);} else {objectWrapper.set (prop, value);}}

Interceptor signature

@ Intercepts ({@ Signature (type = StatementHandler.class, method = "prepare", args = {Connection.class}) public class PreparePaginationInterceptor extends BaseInterceptor {

You can see from the signature that the target type to be intercepted is StatementHandler (Note: type can only be configured as an interface type), and the method to be intercepted is a method named prepare and the parameter Connection.

Note: for information about why interceptors are added to StatementHandler, see MyBatis's introduction to SqlSession.

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

Database

Wechat

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

12
Report