Spring mybatis core knowledge

Posted by Kazlaaz on Sat, 12 Feb 2022 10:40:21 +0100

Write in front

This is the core knowledge of spring mybatis, which can be used to review the eight part essay and deepen knowledge. It is recommended that you read it in the mode of breaking through the customs, and then check and fill the gaps according to the content. You are welcome to ask questions and learn from each other.

It was done before

  1. The essential core knowledge of the new version of javase can be learned by clicking
  2. Necessary core knowledge of concurrent programming, click to learn
  3. Message queue of Middleware
  4. Mysql core knowledge
  5. http protocol core knowledge

Each relevant article has the latest learning harvest and will be updated and modified accordingly

The last article will be updated later

  1. Core knowledge points of distributed cache

Quick view of spring mybatis core knowledge
What are DI and IOC of Spring framework
What are the scope scopes of bean s in Spring
What is AOP and what are the usage scenarios
Common concepts in AOP
What is a static proxy
What is a dynamic agent and what agent is spring aop using
Differences between JDK dynamic agent and CGLib dynamic agent
Development steps of JDBC connection database
#What is the difference between {} and ${}?
Mybatis L1 cache
Mybatis L2 cache
What is the query order when L1 cache and L2 cache are enabled at the same time
What is mybatis3 Lazy loading of X

What are DI and IOC of Spring framework

IOC Control reversal refers to reversing the creation right of an object to Spring container
DI Dependency injection Spring In the process of creating an object, the object dependent properties are injected through configuration,Can't exist alone, need to be in IOC Complete the operation on the basis of
 Dependency injection(DI)And control reversal(IOC)Is to describe the same thing from different angles by introducing IOC Container, using dependency injection to realize the decoupling between objects.

What are the scope scopes of bean s in Spring

singleton: Single case, Default, call getBean Method returns the same object,The instance will be cached, which is more efficient bean Identified as singleton When, spring of IOC Only one of this will exist in the container bean

prototype: Multiple cases, call getBean Method creates different objects, which will frequently create and destroy objects, resulting in great overhead

Other less used (scope only in WebApplicationContext)
    request : each Http Every request creates a new one bean
    session: each Http Session Every request creates a new one bean
    global session((basically not used)

What is AOP and what are the usage scenarios

Aspect Oriented Program Aspect oriented programming adds additional functions without changing the original logic

AOP The idea divides the function into two parts to separate various concerns in the system

    Core concerns
        Main business functions

    crosscutting concern 
        Non core, additional features

    User orders as an example
        Core focus: creating orders
        Crosscutting concerns: logging, controlling transactions

benefit
    Reduce code intrusion and decouple
    Crosscutting logic can be handled uniformly
    Easy to add and delete crosscutting logic

Common concepts in AOP

crosscutting concern 
    What methods to intercept and how to deal with them after interception are called crosscutting concerns
    Such as authority authentication, logs, and events

notice Advice
    Enhanced processing performed on specific pointcuts
    What are you doing? For example, you need to record logs, control transactions, write general modules in advance, and call them directly where needed

Connection point JointPoint
    Where notification is required, the business process needs to be inserted into the specific location of the section during operation,
    Generally, before and after the method call, all methods can be connection points
    It's just a concept, nothing special

breakthrough point Pointcut
    Not all methods are connection points. Filter connection points through specific rules, namely Pointcut,Choose the methods you want
    In the program, it is mainly reflected in writing pointcut expressions (through general matching and regular expressions) to filter out a specific group JointPoint Connection point
    Filter out the corresponding Advice What will happen joinpoint local

section Aspect
    It is usually a class that defines a pointcut+notice , Where is the definition; When and what to do
    notice advice Indicates when and what to do (pre, post, etc.)
    breakthrough point pointcut Specify where to do it
    web In the interface design, web layer->Gateway layer->Service layer->The data layer is also a section between each layer. Objects and objects, methods and methods are all sections

target target
    The target class, the real business logic, can add new functions to the link of the target class without the knowledge of the target class

Weave in Weaving
    The process of applying a slice (a class) to the objective function is called weaving

What is a static proxy

    The source code is created by the program or automatically generated by a specific tool. Before the program runs, the proxy class.class The file already exists
    By implementing the same interface between the target class and the proxy class, the proxy class holds the real class object, then the real class method is invoked in the proxy class method, and the function extension code that we need is added before and after the real class method is added to achieve the purpose of enhancement.
    A -> B -> C

advantage
    Proxy makes the client do not need to know what the implementation class is and how to do it, while the client only needs to know the proxy
    It is convenient to add functions and expand business logic

shortcoming
    There are a lot of redundant code in the agent class, which is very unfavorable to expansion and maintenance
    If a method is added to the interface, all proxy classes need to implement this method in addition to all implementation classes. It increases the complexity of code maintenance

What is the dynamic proxy and what is the proxy used by spring aop

When the program is running, it is dynamically created by using the reflection mechanism, and there is no need to write code manually
SpringAOP What's commonly used in it is JDK Dynamic agent CGLIB Dynamic agent

Differences between JDK dynamic agent and CGLib dynamic agent

    JDK Dynamic proxy requires the target object to implement an interface, but sometimes the target object is just a separate object,No interface is implemented,It can be used at this time CGLib Dynamic agent
    JDK Dynamic agents are built-in, CGlib Third party packages need to be introduced

 
    CGLib Dynamic agent,It is to build a subclass object in memory to expand the function of the target object
    CGLib Dynamic proxy implements proxy based on inheritance, so it cannot final Class private Methods and static Method implementation agent





 Spring AOP Default policy used by agents in
    If the target object implements the interface, it is used by default JDK Dynamic agent
    If the target object does not implement the interface, the CgLib Dynamic proxy
    If the target object implements the interface, it can still be specified in the program CGlib Dynamic agent

Development steps of JDBC connection database

Load database driver

Get data connection object

Get statement object

    There are two types of session objects Statement and PreparedStatement Execute statements. What's the difference between them?
        PreparedStatement Precompiled before execution
        Higher efficiency Statement,And can effectively prevent SQL injection
        PreparedStatement support?Placeholder instead of direct splicing to improve readability   

Processing result set

close resource
  When closing, close it first ResultSet,Re close Statement,Last closed Connection Pay attention to the closing sequence and handling exceptions

#What is the difference between {} and ${}?

#{} is precompiled processing and ${} is string substitution.

Mybatis In processing#{}When, will sql Medium#Replace {} with? Number, call the set method of PreparedStatement to assign value;

Mybatis In processing ${}When, is to ${}Replace with the value of the variable.

use#{} can effectively prevent SQL injection and improve system security

Mybatis L1 cache

Introduction: the scope of L1 cache is SQLSession,Same SqlSession Perform the same in SQL query(same SQL And parameters),For the first time, it will query the database and write it in the cache, and for the second time, it will get it directly from the cache
 be based on PerpetualCache of HashMap Local cache. The L1 cache is enabled by default
 Failure strategy: when executed SQL At that time, the operations of adding, deleting and modifying occurred between the two queries, that is insert,update,delete Other operations commit The will be emptied after SQLSession cache; such as sqlsession Close, or empty, etc

Mybatis L2 cache

L2 cache is namespace Level, multiple SqlSession To operate the same namespace Lower Mapper of sql Statement, multiple SqlSession Can share L2 cache,If two mapper of namespace Same, (even two) mapper,So these two mapper Medium execution sql The queried data will also exist in the same L2 cache area, but the last is each Mapper (separate namespace)

be based on PerpetualCache of HashMap Local cache, customizable storage source, such as Ehcache/Redis etc.

By default, L2 cache is not enabled

Operation process:
    First call to a namespace Lower SQL Go to query the information, and the queried information will be stored in the mapper The corresponding L2 cache area.
    Call the same for the second time namespace Lower mapper In the mapping file, the same sql When you query information, you will fetch the results from the corresponding L2 cache

Failure strategy: execute the same namespace Lower mapepr Addition, deletion and modification in mapping file sql,And implemented commit operation,The L2 cache will be emptied

Note: when implementing L2 cache, MyBatis Recommended return POJO It is serializable, that is, it is recommended to implement Serializable Interface

Cache obsolescence strategy: the default will be used LRU Algorithm to recover (least recently used)

What is the query order when L1 cache and L2 cache are enabled at the same time

Query L2 cache first->Query L1 cache->database

What is mybatis3 Lazy loading of X

Load on demand, query from a single table first, and then associate the query from the associated table when necessary, which can greatly improve the performance of the database, Not all scenarios use lazy loading to improve efficiency

Which query configurations support lazy loading

resultMap Inside association,collection With delayed loading function

Topics: Java Spring Back-end