Detailed explanation of mybatis

Mybatis section

1. What are the shortcomings of JDBC programming and how does MyBatis solve these problems?

①The  frequent creation and release of database links causes waste of system resources and affects system performance. This problem can be solved by using the database link pool.

Solution: Configure the data link pool in SqlMapConfig.xml and use the connection pool to manage database links.


②  Sql statements are written in the code, which makes the code difficult to maintain. The actual application of sql may change greatly, and the change of sql needs to change the java code.

Solution: Configure the Sql statement in the XXXXmapper.xml file to separate it from the java code.


③  It is troublesome to pass parameters to the sql statement, because the where condition of the sql statement is not necessarily, there may be more or less, and the placeholders need to correspond to the parameters one by one.

Solution:  Mybatis automatically maps java objects to sql statements.


④It  is troublesome to parse the result set, the sql change leads to the change of the parsing code, and it needs to be traversed before parsing. It is more convenient if the database records can be encapsulated into pojo objects for parsing.

Solution: Mybatis automatically maps sql execution results to java objects.

2. What are the MyBatis programming steps like?

①Create  SqlSessionFactory ②Create  SqlSession through SqlSessionFactory ③Perform database operations through sqlsession  ④Call  session.commit() to commit the transaction ⑤Call session.close (  ) to close the session



                                                     

3. What is the difference between MyBatis and Hibernate?

    Mybatis is different from hibernate. It is not exactly an ORM framework, because MyBatis requires programmers to write SQL statements by themselves, but mybatis can flexibly configure the SQL statements to be run through XML or annotations, and map java objects and SQL statements to generate final execution. sql, and finally remap the result of sql execution to generate a java object. Mybatis has a low learning threshold and is easy to learn. Programmers can directly write the original ecological sql, which can strictly control the performance of sql execution and has high flexibility. It is very suitable for software development that does not require high relational data models, such as Internet software, enterprise operation software, etc. Because the requirements of this type of software change frequently, once the requirements change, the results are required to be output quickly. However, the premise of flexibility is that mybatis cannot achieve database independence. If you need to implement software that supports multiple databases, you need to customize multiple sets of sql mapping files, which is a lot of work. Hibernate has strong object/relational mapping capabilities and good database independence. For software with high requirements for relational models (such as customized software with fixed requirements), if you use hibernate to develop it, you can save a lot of code and improve efficiency. However, the disadvantage of Hibernate is that the learning threshold is high, the threshold for mastering is higher, and how to design O/R mapping, how to balance performance and object model, and how to use Hibernate well requires strong experience and ability. In short, according to the needs of users, as long as a software architecture with good maintainability and scalability can be made in a limited resource environment, it is a good architecture, so the framework is the best only if it is suitable.
 
    
 
    
 

4. What are the requirements for using the mapper interface of MyBatis?

①   The Mapper interface method name is the same as the id of each sql defined in mapper.xml  The input parameter type of the Mapper interface method is the same as the parameterType of each sql defined in mapper.xml ③   The output parameter type of the Mapper interface method is the same as the mapper The type of resultType of each sql defined in .xml is the same. ④   The namespace in the Mapper.xml file is the class path of the mapper interface.
 

5. What is configured in SqlMapConfig.xml?

The content and order of configuration in SqlMapConfig.xml are as follows: 

properties

settings

typeAliases

typeHandlers

objectFactory

plugins

environments (environment collection property object)

environment (environment sub-property object)

transactionManager

dataSource (data source)

mappers

6. Briefly talk about the first-level cache and second-level cache of MyBatis?

Mybatis first goes to the cache to query the result set. If not, it queries the database. If there is, it retrieves the returned result set from the cache and does not go to the database. Mybatis internal storage cache uses a HashMap, the key is hashCode+sqlId+Sql statement. value is the java object generated from the query map


The second level cache of Mybatis is the query cache. Its scope is the namespace of a mapper, that is, querying SQL in the same namespace can obtain data from the cache. The second level cache is available across SqlSession.

7. What are the ways to write Mapper?

①The interface implementation class inherits SqlSessionDaoSupport

To use this method, you need to write mapper interface, mapper interface implementation class, mapper.xml file

1. Configure the location of mapper.xml in sqlMapConfig.xml

<mappers>

    < mapper resource = "address of mapper.xml file" /> 

    < mapper resource = "address of mapper.xml file" /> 

</mappers>

2. Define the mapper interface

3. Implement class integration SqlSessionDaoSupport

In the mapper method, this.getSqlSession() can be used for data addition, deletion and modification.

4. Spring configuration

< bean id = " " class = "implementation of the mapper interface" > 

   <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>     

</bean>

使用org.mybatis.spring.mapper.MapperFactoryBean

1. Configure the location of mapper.xml in sqlMapConfig.xml

If mapper.xml and mappre interface have the same name and are in the same directory, no configuration is required here

<mappers>

    < mapper resource = "address of mapper.xml file" /> 

    < mapper resource = "address of mapper.xml file" /> 

</mappers>

2. Define the mapper interface

Notice

1. The namespace in mapper.xml is the address of the mapper interface

2. The method name in the mapper interface is consistent with the id of the defined statement in mapper.xml

3. Defined in Spring

<bean id=""class="org.mybatis.spring.mapper.MapperFactoryBean"> 

    <property name="mapperInterface"   value="mapper接口地址"/>   

    <property name="sqlSessionFactory" ref="sqlSessionFactory"/>   

</bean>

③Use mapper scanner

1. Write mapper.xml file,

Notice:

The namespace in mapper.xml is the address of the mapper interface

The method name in the mapper interface is consistent with the id of the defined statement in mapper.xml

If you keep the name of mapper.xml and mapper interface consistent, you do not need to configure it in sqlMapConfig.xml 

2. Define the mapper interface

Note that the file name of mapper.xml is the same as the interface name of mapper and placed in the same directory

3. Configure the mapper scanner

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

    < property  name = "basePackage"  value = "mapper interface package address" ></ property >

    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> 

</bean>

4. After using the scanner, get the implementation object of mapper from the spring container

The scanner generates the implementation object through the proxy method , and the name is the name of the mapper interface to be automatically registered in the spring container.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325932795&siteId=291194637