Getting started with mybatis FAQ (continuous update)
1. Set Parameters
Q: Does parameterType refer to the set type or object type?
A: Yes, even in xml. First, mybatis checks whether the input parameter is a list or array. Second, mybatis is based on the ONGL expression, that is 【Parameter. Attribute] In this format, the attribute values are obtained and injected through reflection. When the input parameter is a set, no matter which parameterType is specified, the two points mentioned above can function.
Q: Parameter '_ frch_item_0' not found. Available parameters are [list]
A: First, it indicates the origin of the frch_item_0 parameter name. In my xml file, <foreach> is defined as follows,
<foreach collection="list" item="item" separator="," open="(" close=")"> #{item.a}</foreach>
'Frch' indicates the prefix of the parameters in the loop, 'item' indicates the name of the item specified in the tag, and the following number indicates the first parameter in the loop.
The methods in the interface are defined as follows:
List<User> listById( List<User> id);
HereItem.It is equivalent to user. a, but the User class does not have the attribute "a" at all, so this problem should be checked immediately.The accessed attribute name is correct
Q: Parameter 'LIST' not found. Available parameters are [0, 1, param1, param2]
A: First, explainAvailable parameters are [0, 1, param1, param2],Mybatish sets the default names of input parameters in sequence, that is, the 0, 1, param1, and param2 values shown here. You can modify the parameter names using the @ Param annotation.There is only oneWhen,You can skip the @ Param annotation,In the defined SQL statement, the parameters written in # {} are random! Because there is only one input parameter, no matter what the name is at # {}, mybatis will pass this unique parameter, but when the @ Param annotation is used, you must use the name specified by the annotation or paramX (X refers to the ordered number) to return to this question. When using List as the unique parameter, when the foreach label is used, the preceding problem does not occur.
List<User> listById( List<String> id);<select id="listById" parameterType="scau.zzf.entity.User" resultType="scau.zzf.entity.User"> SELECT * FROM users WHERE id IN <foreach collection="list" item="item" separator="," open="(" close=")"> #{item} </foreach> </select>
However, when multiple parameters are input
List <User> listById (List <String> id, String username); // do not use @ Param <select id = "listById" parameterType = "scau. zzf. entity. user "resultType =" scau. zzf. entity. user "> SELECT * FROM users WHERE username = # {username} AND id IN <foreach collection =" list "item =" item "separator = ", "open =" ("close =") ">#{ item. id} </foreach> </select>
The error log is displayed as Parameter 'LIST' not found. available parameters are [0, 1, param1, param2]. At this time, we naturally think that when there are multiple parameters, you must use @ Param to specify the parameter name, to use our parameter specifications. Otherwise, we must follow the parameter specifications of mybatis.
However, for new users, it may be a little worse. In the foreach tag, the collection option is mostly list or array. When there is only one input parameter, this is absolutely correct, however, in multiple cases, the collection must follow the name specified by @ Param.
Read this blog to learn more about the mybatis parameter metadata.
Ii. How to Implement fuzzy search
When new users use mybatis for fuzzy search, they will first try to splice % when defining SQL in the xml file, and then try to hit the wall, adding % before passing in the parameter solves the problem. However, this method is not elegant enough. You can use the bind label to implement this function.
<select id="findByUsername" resultType="scau.zzf.entity.User"> <bind name="pattern" value="username+'%'"></bind> SELECT * FROM users WHERE username LIKE #{pattern} </select>
3. disordered referenced data in association query
Q: for one-to-multiple join queries, only one data record is returned for multiple parties, but the SQL statement is correct.
A: In this case, the primary key names returned by the query are often the same, resulting in an error in the returned results. The database fields of the two tables are the same, which is difficult to avoid. You need to use the SQL statement alias and resultMap together to solve the problem.
4. How to obtain the primary key value of the inserted object
After successful insert, mybatis automatically binds the inserted value to the Id attribute of the inserted object. The following code correctly prints the ID value.
<insert id="addUser" > INSERT INTO users (id,username,password,salt) VALUES (#{id},#{username},#{password},#{salt}) </insert>
IUserService. addUser (user); // The System. out. println (user. getId () is successfully inserted. // print the ID
If the primary key name used is not an ID, you can modify the keyProperty or keyColumn In the insert tag.
5. Failed to trigger cache refresh
To put it simply, this happens when the associated query is performed, but the update is only one table, but two tables are queried. When the data of one table is updated, unable to trigger refresh to query the cache data of two tables, the detailed principle can view the http://blog.csdn.net/isea533/article/details/44566257
I have summarized my learning and usage problems, and I will add more in my practical experience in the future. If you have any shortcomings, please note in the comment area, we recommend that you submit more common errors.
If the article needs to be reproduced, please describe the original content.