MyBatis optimization Precautions

We often use Mybatis in the project, but how can we flexible application mybatis, to make our program better performance? Next we take a look at these points. 

1, bulk operations 

We often encounter the operation of bulk data, in general, we think that the use of recycled and processed one by one. However, such a process is not very efficient. mybatis provides us with a method of bulk operations, let us take a look. 

// bulk insert 

<insert id="addTrainRecordBatch" parameterType="java.util.List"> 

insert into t_train_record (add_time,emp_id,activity_id,flag) 

values 

<foreach collection="list" item="item" index="index" separator="," > 

      (#{item.addTime},#{item.empId},#{item.activityId},#{item.flag}) 

     </foreach> 

 </insert> 


foreach resolve 

 Foreach element attribute mainly item, index, collection, open, separator, close. 

 Each item represents the elements in the set of alias iteration, 

 specify a name index for representing the iterative process, each iteration of the location, 

 open statement to indicate the start of what, 

 separator between each iteration represents what symbol as a delimiter, 

 It represents close to what end, 


Note Collection 

When using foreach of the most critical and most error-prone is a collection property, which must be specified, but under different circumstances, the value of the property is not the same, there are about three cases: 

1. If one parameter is passed in the parameter type is a List and time, collection attribute value list 

2. If one parameter is passed and the time parameter is an array type array, collection attribute value array 

3. If the argument passed is more, we need to package them into a Map, of course, it can also be packaged as a single parameter map 


2, resultMap and resultType 

Here we want to explain this relationship between the two to prevent someone to get him mixed. 

resultType is a direct representation of the type of return (corresponding to our model object entity), and resultMap is ResultMap external references (in advance defined insinuate between db and model key-> value relationship) 


Compared 

① When the type attribute is provided to return resultType, MyBatis Map will be taken inside the key-value pair is assigned to the specified target resultType corresponding attribute. So in fact MyBatis query mapping for each type of return is ResultMap, only when the type attribute is provided resultType return time, automatic MyBatis to assign the value corresponding to the specified attribute object resultType. 

② When the return type is provided resultMap time, because the Map is not well represent the domain model, we need to put yourself further into its corresponding object, which is often useful role in complex queries.

 

3, lightweight data query 

If we refer to when we first multi-table queries What comes to mind? NATURAL join query is divided into inner and outer joins, there is no longer connected to elaborate several queries. 

We all know that the connection queries than in the query efficiency is higher, but should be noted that, if we are the Internet companies, the pursuit of higher performance, the use of sub-library sub-table, so we are required to make a connection query is split into a number of simple query, this time with in relatively good. 

// en 

select name,age,userRow from user as u inner join rol as r on u.userId=r.userId 

// in (multiple statements) 

select name,age,userRow from user where userId in (select userRow,userId from rol) 


to sum up: 

Each tool has a range of his probation, according to our actual situation requires flexible application. And every question many ways, we have to think more about those simple, available and efficient way. Constantly thinking, in order to progress.


Reproduced in: https: //juejin.im/post/5d072a8451882548ac439c2b

Guess you like

Origin blog.csdn.net/weixin_33713503/article/details/93178099