【 Prepare for spring recruitment series 】50 micro service interview questions in detail
1. What is Mybatis?
2. Advantages of Mybaits:
2, compared with JDBC, reduce more than 50% of the code, eliminate a lot of JDBC redundant code, do not need to manually switch the connection;
3, good compatibility with a variety of databases (because MyBatis uses JDBC to connect to the database, so as long as JDBC support database MyBatis support).
4. Good integration with Spring;
5. Provide mapping labels to support ORM field relational mapping between objects and databases; Provides object-relational mapping labels to support object-relational component maintenance.
3. Disadvantages of MyBatis framework:
2, SQL statements rely on the database, resulting in poor database portability, can not be replaced at will database.
4, MyBatis framework applicable occasions:
2. MyBatis will be a good choice for projects with high performance requirements or more variable requirements, such as Internet projects
5. What are the differences between MyBatis and Hibernate?
2, Mybatis directly write the original SQL, SQL execution performance can be strictly controlled, high flexibility, very suitable for the software development of relational data model requirements are not high, because this kind of software demand changes frequently, but a demand change requires rapid output results. However, the premise of flexibility is that Mybatis cannot achieve database independence. If you need to implement software supporting a variety of databases, you need to customize multiple sets of SQL mapping files, and the workload is heavy.
Hibernate object/relational mapping ability, database independence is good, for software with high requirements of relational model, if Hibernate development can save a lot of code, improve efficiency.
What is the difference between #{} and ${}?
Mybatis will replace #{} with? Call the set method in PreparedStatement to assign the value.
Using #{} can effectively prevent SQL injection and improve system security.
7, What if the name of the attribute in the entity class is different from the name of the field in the table?
<select ID = "selectOrder" parameterType = "int" resulteType = "me.gacl.domain.order" > select order_id ID, order_no orderno ,order_price price form orderswhere order_id=#{id};
</select> Copy the code
to map the one-to-one correspondence between field names and entity class attribute names.
<select id="getOrder" parameterType="int"
resultMap="orderresultmap">
select * from orders where order_id=#{id}
</select>
<resultMap type= "me. Gacl. Domain. The order" id = "orderresultmap" > <! > < ID property= "ID" column= "order_id" > <! -- Use the result attribute to map non-primary key fields. Property is the name of the entity class attribute. Column is an attribute in the data table -- > <result property= "orderno" column= "order_no" /> <result property= "price" column= "order_price" /> </reslutMap>Copy the code
8, fuzzy query like statement how to write?
Smi string wildcardname = "% %"; list<name> names = mapper.selectlike(wildcardname); <select id= "selectlike" > select * from foowhere bar like #{value}
</select>Copy the code
String wildcardname = "smi"; list<name> names = mapper.selectlike(wildcardname); <select id= "selectlike" > select * from foowhere bar like "%"#{value}"%"
</select>Copy the code
9. Usually, an Xml mapping file will write a Dao interface corresponding to it. What is the working principle of this Dao interface? Can methods in the Dao interface be overloaded if their parameters are different?
, < DELETE > tag is parsed into a MapperStatement object.
For example: Com. Mybatis3. Mappers ^ tudentDao findStudentByld, can only find the namespace for co m.m ybatis3. Map PE rs. Stud ent Dao id as below findStudentByld MapperStatement.
Methods in the Mapper interface cannot be overridden because they use the save and find strategy of the full name + method name. Mybatis will use JDK dynamic proxy to generate proxy object for Mapper interface. The proxy object will intercept the interface method, execute the SQL represented by MapperStatement, and return the SQL execution result.
How does Mybatis paginate? How does paging plug-ins work?
Mybatis uses the RowBounds object to talk about row paging, which is memory paging performed against ResultSet result sets rather than physical paging. You can write parameters with physical paging directly in SQL to complete physical paging, or you can use paging broadcast pieces to complete physical paging.
The basic principle of paging multicast is to use the plug-in interface provided by Mybatis to implement a custom plug-in to intercept the SQL to be executed in the interception method of the plug-in’s column intercept method, and then rewrite the SQL to add the corresponding physical paging statement and physical paging parameters according to the dialect.
How does Mybatis encapsulate SQL execution results as target objects and return them? What are the mappings?
tag to define the mapping between database column names and object attribute names one by one.
The second is to use the SQL column alias function to write the column alias as the object property name.
How do I perform batch inserts?
<insert ID = "insertname" > insert into names (name) values (#{value})
</insert> Copy the code
list < string > names = new arraylist(); Names. The add (" Fred "); Names. The add (" barney "); Names. The add (" Betty "); Names. The add (" Wilma "); / / note here 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();
}Copy the code
13. How to obtain the automatically generated (primary) key value?
With a self-growth strategy, automatically generated key values can be set to the parameter object passed in after the INSERT method.
< insert insertname usegeneratedkeys = "id =" "true"Keyproperty =" id "> insert into names (name) values (#{name})</insert> name name = new name(); Name. Elegantly-named setname (" Fred "); int rows = mapper.insertname(name); // When done, the ID has been set to the object system.out.println(" rows = "+ rows); System.out. println(" generated key value = "+ name.getid());Copy the code
14. How to pass multiple parameters in Mapper?
public UserselectUser(String name,String area); The corresponding XML,#{0} represents the first parameter received in the DAO layer, and #{1} represents the second parameter received in the DAO layerParameters, more parameters can be added later.Copy the code
<select id="selectUser"resultMap="BaseResultMap">
select * fromuser_user_t whereuser_name = # {0}
anduser_area=# {1}
</select> Copy the code
Public interface usermapper {user selectUser (@param(" username ") string username,@param(" hashedPassword ") string hashedpassword); }Copy the code
<select ID = "selectUser" resultType = "user" > select ID, username, hashedPassword from some_tablewhere username = #{username}
and hashedpassword = #{hashedpassword}
</select> Copy the code
Try {// Map the file namespace. // Since we have more than two parameters, and only one Object parameter is collected in the method, So we use the Map collection to load our parameters 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();
} Copy the code
15, Mybatis dynamic SQL what to use? How does it work? What dynamic SQL is there?
16, Xml mapping file, in addition to the common select | insert | updae | delete tags, what other tags?
,
,
, plus nine tags for dynamic SQL, where < SQL > is the SQL fragment tag, through
tag introduces SQL fragments, and
generates policy labels for primary keys that do not support self-increment
17. In the Xml mapping files of Mybatis, can the IDS of different Xml mapping files be repeated?
. If there is no namespace, only ids are left. Duplicate ids will overwrite each other. With a namespace, the natural ID can be repeated. With different namespaces, the namespace+ ID will naturally be different.
Why Mybatis is a semi-automatic ORM mapping tool? What’s the difference between it and automatic?
19, one to one, one to many associated query?
<mapper namespace="com.lcb.mapping.userMapper"> <! --association one-to-one associative query --><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"> <! -- mapping entity class field names to data table field names --> <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 1 to many associative query --> <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> Copy the code
20. How many ways can MyBatis achieve one-to-one? How does it work?
21, MyBatis has several ways to achieve one-to-many, how to operate?
Does Mybatis support lazy loading? If so, how does it work?
23, Mybatis level 1, level 2 cache:
24, What is MyBatis interface binding? What are the implementation methods?
25. What are the requirements when using Mapper interface of MyBatis?
26. What are the ways to write Mapper?
SqlSessionDaoSupport: This method needs to be written
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> Copy the code
<bean id="" class="Implementation of mapper Interface">
<property name="sqlSessionFactory"
ref="sqlSessionFactory"></property>
</bean>Copy the code
The second: use the org. Mybatis. Spring. Mapper. MapperFactoryBean:
<mappers>
<mapper resource="Address of mapper. XML file" />
<mapper resource="Address of mapper. XML file" />
</mappers>Copy the code
<bean id="" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="Mapper interface address" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>Copy the code
Third: Using mapper scanner:
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="Mapper interface packet address"></property>
<property name="sqlSessionFactoryBeanName"
value="sqlSessionFactory"/>
</bean>Copy the code
27. Describe the operation principle of Mybatis plug-in and how to write a plug-in.
Finally:
Mybatis interview details here, there is an opportunity to prepare for spring recruitment in the back of the series of topics will share micro services, ZooKeeper, Dubbo, Kafka and other interview topics. I also summarized all the topics in a PDF document
The whole series of about 1000 interviews full explanation, need source documents can follow wechat public number: Java programmers gathering place.