MyBatis study concluded (14) - Mybatis use skills summary

1, to distinguish $ # {} {} and different application scenarios

1) # {} will generate the SQL precompiled, the type of processing data correctly, but only $ {} text replacement.
For SQL: select * from student where xCode = 'S123456';
If # {}
then the generated SQL is:
? SELECT * WHERE xCode = Student pass from the value 'S123456';
If $ {}
then the generated SQL to: select * from student where xCode = S123456
if xCode data type varchar, $ {} will then use the error.
2) {} $ generally used in order by, limit, group by other places.
Suppose we use # {} to specify order by fields, such as
the SELECT * from Student order by # {xCode},
SQL then generated for the
select * from student order by?, The replacement value for the
select * from student order by 'xCode '
Mybatis of xCode in quotes causes the sort fail

2, Spring atmospheres Mybatis-Spring native interface instead interface Mybatis

The benefits of spring environments mybatis-spring are:
1) We can use Sping declarative transaction model (@Transactional), instead of manually return the transaction.
2) mybatis-spring gracefully shut down SqlSession, instead of manually closed
3) database connection pool management can be handed over to spring, when the program is stopped when, spring will close the connection right

3, the return Map <ID, Entity> List not easy to find

Sometimes too many table joins (join) poor performance, we will split this into multiple SQL SQL, and then assembled in the code. For example, students table and class tables, the results need to query is "student number, class, name," we can first query "study, class ID, name," and "class ID, class rank", we can query the class table when return Map <class ID, the class>, then the iteration the result set students table, with the class ID Map <class ID, the class> find information corresponding to the class, the class ID and then replaced with a class name.
Interface declared SqlSession.selectMap (String statement, String mapKey)

4, using the Map package query results

Sometimes we are tired of writing an Entity class for each query, then began to play it Map effect.
To return to the "student number, class, name," the results of a query can be written Mapper:

Our method dao following statement:

If the result of the query into a JSON string to return, then we can directly List <Map <String, Object> into JSON, logic layer does not require any code.
If the result set returned by the need to return to select the order of the fields, then resultType = "java.util.HashMap" replaced resultType = "java.util.LinkedHashMap"

5、使用Map封装查询结果时注意数据的类型映射

对于如下的Mapper

Mybatis会傻傻的将sName的数据类型映射为byte[], 因为我们没有提供entity,mybatis也不知道我们想要什么类型,而sName是计算出来的值,mybatis也没有办法从数据库中获取字段的值,所以它就将其封装为byte[],解决办法很简单,加一个cast 函数

6、正确的配置Mybatis 的Log

1)一个应用一般会使用很多的jar,各个jar依赖的log 实现不一样,Mybatis查找Log的顺序为(SLF4J,Apache Commons Logging,Log4j 2,Log4j,JDK logging),如果classpath中有slf4j记得添加相应的桥接jar,比如slf4j-log4j。许多web 服务器的classpath 会含有Apache Commons Logging,因此如果要使用Log4j,要么使用SLF4J桥接Log4j,要么在配置中强制指定使用Log4J。

7、警惕Mybatis的Foreach的的副作用

对于如下SQL:
假设有如下的mapper:

当我们传入的IDArr时,最后产生的SQL为:
Select * from student where ID = ‘998’ AND ID IN ( ‘123’, ’234’,…..,’998’)
解决办法:
解决办法有
1) 将红色的ID 换成别的名称,比如“item”。
2) 这两个if 是对同一个字段判断,改为choose… when 结构

8、使用原生的SQL操作数据以提高效率

对于一次插入多条数据,将其组装成 insert into xxx values (), () ()格式一次插入多行数据往往能极大的提高性能。

9、警惕MyBatis封装数据时性能损耗

For the following mapper

In a batch program cycle calls the method 250 times to return about 1w records, found that the program runs very slowly, see the individual time-consuming method with jrofiler, 80 percent of the time actually spent on the student's on the setter, in the process it produced about 250w objects, and mybatis emission package using the Entity, the code is as follows:

The solution is to return the number of rows and fields to return as much as possible less.

Reproduced in: https: //my.oschina.net/zhanghaiyang/blog/606719

Guess you like

Origin blog.csdn.net/weixin_33958585/article/details/92661383