Interview: MyBatis interview summary

1. What is Mybatis?

(1) Mybatis is a semi-ORM (object-relational mapping) framework that encapsulates the internal JDBC, SQL statements only need to focus on the development itself, does not need to spend energy to handle the loading drive, create a connection, create complicated process statement and so on. Programmers write directly to the original ecology sql, sql execution performance can be tightly controlled, high flexibility.

(2) MyBatis annotations may be using XML or native configuration and mapping information to map POJO database records, to avoid almost all JDBC code and manual setting parameters and obtaining the result set.

(3) various annotations or xml file statement by way of the configuration to be performed together, and the final map generation sql statement executed by a java objects and dynamic parameters of the sql statement, the last frame is performed by mybatis and mapping the result to sql java object and returns. (Process from the execution result returned to the sql).

2, Mybaits advantages:

(1) SQL statements based programming, is quite flexible and will not cause any impact on existing application or database design, SQL written in XML, decouple sql and program code, to facilitate unified management; providing XML tags, support the preparation of dynamic SQL statements can be reused.

(2) Compared with the JDBC, reducing the amount of code more than 50%, eliminating redundant code JDBC large, no manual switch is connected;

(3) very good compatibility with a variety of databases (because MyBatis to use the JDBC database connection, so as long as JDBC support database MyBatis support).

(4) can be well integrated with the Spring;

(5) provides a mapping label, field support ORM object-relational mapping database; providing an object-relational mapping label, object-relational component maintenance support.

 

 

3, MyBatis framework disadvantages:

 

Written work (1) SQL statement is large, especially when the field is more associated table for a long time, writing SQL statements for developer skills have certain requirements.

 

(2) SQL statement depends on the database, resulting in poor portability database, the database can not be replaced.

 

 

4, MyBatis framework applicable occasions:

 

(1) MyBatis focused on SQL itself, is a sufficiently flexible DAO layer solutions.

 

(2) high performance requirements, or more changes in project requirements, such as the Internet project, MyBatis would be a good choice.

 

5, MyBatis with Hibernate What are the differences?

Different (1) Mybatis and hibernate, it is not exactly an ORM framework, because MyBatis require programmers to write their own Sql statement.

(2) Mybatis write directly to the original ecology sql, sql execution can be tightly controlled performance, high flexibility, ideal for less demanding on the relational data model of software development, such software because of frequent changes in demand, but demand a change called for the speedy achievement output . But that flexibility can not be done on the premise that mybatis database-independent, if the need to implement the software supports multiple databases, you need a custom map file sets sql heavy workload. 

(3) Hibernate object / relational mapping capabilities strong, independent of the database is good for high-relational model requires software, if hibernate developers can save a lot of code and improve efficiency

 

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

 # {} Is a pre-compiler process, $ {} string is replaced.

(. 1) Mybatis when processing # {}, will be in sql number # {} with the call set assignment method PreparedStatement to?;

(2) Mybatis when handling $ {}, {} $ is to replace the value of the variable.

 Use # {} can effectively prevent SQL injection, improve system security.

 

7, when the field names in the entity class attribute name and the table is not the same, how do?

Unanimously adopted the definition of field names in the sql statement query aliases, so that the field name of the attribute name aliases and entity classes: the first kind.

    <select id=”selectorder” parametertype=”int” resultetype=”me.gacl.domain.order”>
       select order_id id, order_no orderno ,order_price price form orders where order_id=#{id};
    </select>

No. 2: one to one relationship to the entity class mapping field names and attribute names by <resultMap>.

<SELECT ID = "getOrder" the parameterType = "int" The resultMap = "orderresultmap"> 
        SELECT * WHERE Orders from order_id = # {ID}
     </ SELECT> 
 
   <The resultMap type = "me.gacl.domain.order" ID = "orderresultmap "> 
        <! - with the id attribute to map the primary key field -> 
        <id property =" id "column =" order_id "> 
 
        <! - with the result attribute to map the non-primary key field, property of the entity class attribute names, column data table attributes -> 
        <property Result = "OrderNo" column = "order_no" /> 
        <property Result = ". price" column = "order_price" /> 
    </ reslutMap>

 

8, fuzzy query like how to write the statement?

Item 1: Adding sql wildcards in Java code.

    string wildcardname = “%smi%”;
    list<name> names = mapper.selectlike(wildcardname);
 
    <select id=”selectlike”>
     select * from foo where bar like #{value}
    </select>

No. 2: In sql statement stitching wildcard, will cause sql injection

    string wildcardname = “smi”;
    list<name> names = mapper.selectlike(wildcardname);
 
    <select id=”selectlike”>
         select * from foo where bar like "%"#{value}"%"
    </select>

 

9, usually a Xml mapping file, will write a corresponding interface to Dao, may I ask, what is the working principle of the interface is Dao? Dao interfaces in the methods, parameters are different, the method can reload it?

Dao interfaces namely Mapper interface. The fully qualified name of the interface, is the value of the mapping file of the namespace; interface method name, id value map is a file Mapper Statement; a parameter in the interface method, the parameter is passed to sql.

Mapper not interface implementation class, when a method call interface, the interface name + fully qualified method name as a string concatenation key value uniquely locating a MapperStatement. In Mybatis, each <select>, <insert>, <update>, <delete> tag is resolved to a MapperStatement object.

For example: com.mybatis3.mappers.StudentDao.findStudentById, you can find unique namespace for the com.mybatis3.mappers.StudentDao below the id findStudentById of MapperStatement.

Mapper interfaces in the method, is not overloaded, because it is using to save the fully qualified name + method name and look for strategies. Mapper interface that works JDK dynamic proxy, use JDK dynamic proxy objects proxy for the proxy interface generation Mapper, proxy objects will intercept interface methods in favor of the implementation of MapperStatement represented sql Mybatis run, sql execution then returns the results.

10, Mybatis how to page through? What is the principle pagination plug-in?

        Mybatis RowBounds objects using paging, it is for the memory paging ResultSet result set to perform, rather than the physical page. Parameter may be directly written with the physical pages in the physical page is accomplished sql function may be used to complete the physical page tab widget.

       The basic principle is to use the plug-in tab Mybatis plugin interface provided, to achieve a custom plug-ins, sql intercept interception to be performed within the insert method, and then rewrite the sql, according dialect dialect, add statements and physical page corresponding to the physical paging parameters.

 

11, Mybatis is how to execute sql package is the result of the target object and returns? What are mapping form?

The first is to use the <resultMap> tag, of defining the mapping between the attributes and object database column names.

The second function is to use an alias sql column, the column alias name is written as object properties.

有了列名与属性名的映射关系后,Mybatis通过反射创建对象,同时使用反射给对象的属性逐一赋值并返回,那些找不到映射关系的属性,是无法完成赋值的。

 

12、如何执行批量插入?

首先,创建一个简单的insert语句:

    <insert id=”insertname”>
         insert into names (name) values (#{value})
    </insert>

然后在java代码中像下面这样执行批处理插入:

  list<string> names = new arraylist();
    names.add(“fred”);
    names.add(“barney”);
    names.add(“betty”);
    names.add(“wilma”);
 
    // 注意这里 executortype.batch
    sqlsession sqlsession = sqlsessionfactory.opensession(executortype.batch);
    try {
     namemapper mapper = sqlsession.getmapper(namemapper.class);
     for (string name : names) {
         mapper.insertname(name);
     }
     sqlsession.commit();
    }catch(Exception e){
     e.printStackTrace();
     sqlSession.rollback(); 
     throw e; 
    }
     finally {
         sqlsession.close();
    }

 

13、如何获取自动生成的(主)键值?

insert 方法总是返回一个int值 ,这个值代表的是插入的行数。

如果采用自增长策略,自动生成的键值在 insert 方法执行完后可以被设置到传入的参数对象中。

示例:

<insert id=”insertname” usegeneratedkeys=”true” keyproperty=”id”>
     insert into names (name) values (#{name})
</insert>
    name name = new name();
    name.setname(“fred”);
 
    int rows = mapper.insertname(name);
    // 完成后,id已经被设置到对象中
    system.out.println(“rows inserted = ” + rows);
    system.out.println(“generated key value = ” + name.getid());

 

14、在mapper中如何传递多个参数?

(1)第一种:
//DAO层的函数
Public UserselectUser(String name,String area);  
//对应的xml,#{0}代表接收的是dao层中的第一个参数,#{1}代表dao层中第二参数,更多参数一致往后加即可。
<select id="selectUser"resultMap="BaseResultMap">  
    select *  fromuser_user_t   whereuser_name = #{0} anduser_area=#{1}  
</select>  
 
(2)第二种: 使用 @param 注解:
public interface usermapper {
   user selectuser(@param(“username”) string username,@param(“hashedpassword”) string hashedpassword);
}
然后,就可以在xml像下面这样使用(推荐封装为一个map,作为单个参数传递给mapper):
<select id=”selectuser” resulttype=”user”>
         select id, username, hashedpassword
         from some_table
         where username = #{username}
         and hashedpassword = #{hashedpassword}
</select>
 
(3)第三种:多个参数封装成map
try{
//映射文件的命名空间.SQL片段的ID,就可以调用对应的映射文件中的SQL
//由于我们的参数超过了两个,而方法中只有一个Object参数收集,因此我们使用Map集合来装载我们的参数
Map<String, Object> map = new HashMap();
     map.put("start", start);
     map.put("end", end);
     return sqlSession.selectList("StudentID.pagination", map);
 }catch(Exception e){
     e.printStackTrace();
     sqlSession.rollback();
    throw e; }
finally{
 MybatisUtil.closeSqlSession();
 }

 

15、Mybatis动态sql有什么用?执行原理?有哪些动态sql?

Mybatis动态sql可以在Xml映射文件内,以标签的形式编写动态sql,执行原理是根据表达式的值 完成逻辑判断并动态拼接sql的功能。

Mybatis提供了9种动态sql标签:trim | where | set | foreach | if | choose | when | otherwise | bind。

 

16、Xml映射文件中,除了常见的select|insert|updae|delete标签之外,还有哪些标签?

答:<resultMap>、<parameterMap>、<sql>、<include>、<selectKey>,加上动态sql的9个标签,其中<sql>为sql片段标签,通过<include>标签引入sql片段,<selectKey>为不支持自增的主键生成策略标签。

 

17、Mybatis的Xml映射文件中,不同的Xml映射文件,id是否可以重复?

不同的Xml映射文件,如果配置了namespace,那么id可以重复;如果没有配置namespace,那么id不能重复;

原因就是namespace+id是作为Map<String, MapperStatement>的key使用的,如果没有namespace,就剩下id,那么,id重复会导致数据互相覆盖。有了namespace,自然id就可以重复,namespace不同,namespace+id自然也就不同。

但是,在以前的Mybatis版本的namespace是可选的,不过新版本的namespace已经是必须的了。

 

18、为什么说Mybatis是半自动ORM映射工具?它与全自动的区别在哪里?

Hibernate属于全自动ORM映射工具,使用Hibernate查询关联对象或者关联集合对象时,可以根据对象关系模型直接获取,所以它是全自动的。而Mybatis在查询关联对象或关联集合对象时,需要手动编写sql来完成,所以,称之为半自动ORM映射工具。

 

19、 一对一、一对多的关联查询 ? 

<mapper namespace="com.lcb.mapping.userMapper">  
    <!--association  一对一关联查询 -->  
    <select id="getClass" parameterType="int" resultMap="ClassesResultMap">  
        select * from class c,teacher t where c.teacher_id=t.t_id and c.c_id=#{id}  
    </select>  
 
    <resultMap type="com.lcb.user.Classes" id="ClassesResultMap">  
        <!-- 实体类的字段名和数据表的字段名映射 -->  
        <id property="id" column="c_id"/>  
        <result property="name" column="c_name"/>  
        <association property="teacher" javaType="com.lcb.user.Teacher">  
            <id property="id" column="t_id"/>  
            <result property="name" column="t_name"/>  
        </association>  
    </resultMap>  
 
 
    <!--collection  一对多关联查询 -->  
    <select id="getClass2" parameterType="int" resultMap="ClassesResultMap2">  
        select * from class c,teacher t,student s where c.teacher_id=t.t_id and c.c_id=s.class_id and c.c_id=#{id}  
    </select>  
 
    <resultMap type="com.lcb.user.Classes" id="ClassesResultMap2">  
        <id property="id" column="c_id"/>  
        <result property="name" column="c_name"/>  
        <association property="teacher" javaType="com.lcb.user.Teacher">  
            <id property="id" column="t_id"/>  
            <result property="name" column="t_name"/>  
        </association>  
 
        <collection property="student" ofType="com.lcb.user.Student">  
            <id property="id" column="s_id"/>  
            <result property="name" column="s_name"/>  
        </collection>  
    </resultMap>  
</mapper> 

 

20、MyBatis实现一对一有几种方式?具体怎么操作的?

有联合查询和嵌套查询,联合查询是几个表联合查询,只查询一次, 通过在resultMap里面配置association节点配置一对一的类就可以完成;

嵌套查询是先查一个表,根据这个表里面的结果的 外键id,去再另外一个表里面查询数据,也是通过association配置,但另外一个表的查询通过select属性配置。

 

21、MyBatis实现一对多有几种方式,怎么操作的?

        有联合查询和嵌套查询。联合查询是几个表联合查询,只查询一次,通过在resultMap里面的collection节点配置一对多的类就可以完成;嵌套查询是先查一个表,根据这个表里面的 结果的外键id,去再另外一个表里面查询数据,也是通过配置collection,但另外一个表的查询通过select节点配置。

 

22、Mybatis是否支持延迟加载?如果支持,它的实现原理是什么?

答:Mybatis仅支持association关联对象和collection关联集合对象的延迟加载,association指的就是一对一,collection指的就是一对多查询。在Mybatis配置文件中,可以配置是否启用延迟加载lazyLoadingEnabled=true|false。

它的原理是,使用CGLIB创建目标对象的代理对象,当调用目标方法时,进入拦截器方法,比如调用a.getB().getName(),拦截器invoke()方法发现a.getB()是null值,那么就会单独发送事先保存好的查询关联B对象的sql,把B查询上来,然后调用a.setB(b),于是a的对象b属性就有值了,接着完成a.getB().getName()方法的调用。这就是延迟加载的基本原理。

当然了,不光是Mybatis,几乎所有的包括Hibernate,支持延迟加载的原理都是一样的。

 

 23、Mybatis的一级、二级缓存:

1)一级缓存: 基于 PerpetualCache 的 HashMap 本地缓存,其存储作用域为 Session,当 Session flush 或 close 之后,该 Session 中的所有 Cache 就将清空,默认打开一级缓存。

2)二级缓存与一级缓存其机制相同,默认也是采用 PerpetualCache,HashMap 存储,不同在于其存储作用域为 Mapper(Namespace),并且可自定义存储源,如 Ehcache。默认不打开二级缓存,要开启二级缓存,使用二级缓存属性类需要实现Serializable序列化接口(可用来保存对象的状态),可在它的映射文件中配置<cache/> ;

3)对于缓存数据更新机制,当某一个作用域(一级缓存 Session/二级缓存Namespaces)的进行了C/U/D 操作后,默认该作用域下所有 select 中的缓存将被 clear 掉并重新更新,如果开启了二级缓存,则只根据配置判断是否刷新。

 

24、什么是MyBatis的接口绑定?有哪些实现方式?

接口绑定,就是在MyBatis中任意定义接口,然后把接口里面的方法和SQL语句绑定, 我们直接调用接口方法就可以,这样比起原来了SqlSession提供的方法我们可以有更加灵活的选择和设置。

接口绑定有两种实现方式,一种是通过注解绑定,就是在接口的方法上面加上 @Select、@Update等注解,里面包含Sql语句来绑定;另外一种就是通过xml里面写SQL来绑定, 在这种情况下,要指定xml映射文件里面的namespace必须为接口的全路径名。当Sql语句比较简单时候,用注解绑定, 当SQL语句比较复杂时候,用xml绑定,一般用xml绑定的比较多。

 

25、使用MyBatis的mapper接口调用时有哪些要求?

①  Mapper接口方法名和mapper.xml中定义的每个sql的id相同;
②  Mapper接口方法的输入参数类型和mapper.xml中定义的每个sql 的parameterType的类型相同;
③  Mapper接口方法的输出参数类型和mapper.xml中定义的每个sql的resultType的类型相同;
④  Mapper.xml文件中的namespace即是mapper接口的类路径。

 

26、Mapper编写有哪几种方式?

第一种:接口实现类继承SqlSessionDaoSupport:使用此种方法需要编写mapper接口,mapper接口实现类、mapper.xml文件。1)在sqlMapConfig.xml中配置mapper.xml的位置
<mappers>
    <mapper resource="mapper.xml文件的地址" />
    <mapper resource="mapper.xml文件的地址" />
</mappers>2)定义mapper接口
(3)实现类集成SqlSessionDaoSupport
mapper方法中可以this.getSqlSession()进行数据增删改查。
(4)spring 配置
<bean id=" " class="mapper接口的实现">
    <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>

 
第二种:使用org.mybatis.spring.mapper.MapperFactoryBean:1)在sqlMapConfig.xml中配置mapper.xml的位置,如果mapper.xml和mappre接口的名称相同且在同一个目录,这里可以不用配置
<mappers>
    <mapper resource="mapper.xml文件的地址" />
    <mapper resource="mapper.xml文件的地址" />
</mappers>2)定义mapper接口:
①mapper.xml中的namespace为mapper接口的地址
②mapper接口中的方法名和mapper.xml中的定义的statement的id保持一致
③Spring中定义
<bean id="" class="org.mybatis.spring.mapper.MapperFactoryBean">
    <property name="mapperInterface"   value="mapper接口地址" /> 
    <property name="sqlSessionFactory" ref="sqlSessionFactory" /> 
</bean>


第三种:使用mapper扫描器:1)mapper.xml文件编写:
mapper.xml中的namespace为mapper接口的地址;
mapper接口中的方法名和mapper.xml中的定义的statement的id保持一致;
如果将mapper.xml和mapper接口的名称保持一致则不用在sqlMapConfig.xml中进行配置。 
(2)定义mapper接口:
注意mapper.xml的文件名和mapper的接口名称保持一致,且放在同一个目录
(3)配置mapper扫描器:
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="mapper接口包地址"></property>
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> 
</bean>4)使用扫描器后从spring容器中获取mapper的实现对象。

 

27、简述Mybatis的插件运行原理,以及如何编写一个插件。

答:Mybatis仅可以编写针对ParameterHandler、ResultSetHandler、StatementHandler、Executor这4种接口的插件,Mybatis使用JDK的动态代理,为需要拦截的接口生成代理对象以实现接口方法拦截功能,每当执行这4种接口对象的方法时,就会进入拦截方法,具体就是InvocationHandler的invoke()方法,当然,只会拦截那些你指定需要拦截的方法。

编写插件:实现Mybatis的Interceptor接口并复写intercept()方法,然后在给插件编写注解,指定要拦截哪一个接口的哪些方法即可,记住,别忘了在配置文件中配置你编写的插件。

 

文章转载至:https://blog.csdn.net/a745233700/article/details/80977133

Guess you like

Origin www.cnblogs.com/nhdlb/p/12123453.html