Mybatis - select a plurality of tag parameters passed (Map mode, JavaBean mode)

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/cold___play/article/details/102768355

SELECT tag

In SQL mapping file used for mapping the elements of the SQL select statement, the following sample code which

<!--根据uid查询一个用户信息 -->
<select id="selectUserById" parameterType="Integer" resultType="com.mybatis.po.MyUser">
    select * from user where uid = #{uid}
</select>

In the sample code above, id is a unique identifier value, it receives a parameter of type Integer, returns the object type of a MyUser result set automatically mapped to MyUser properties.

In addition to the above-described elements of the sample code than several properties, there are some common properties, as shown in the following table:

Property Name description
id It namespace Mapper is used in combination, it is a unique identifier for calling MyBatis
parameterType Means that the incoming SQL statement fully qualified name or alias parameter type. It is an optional attribute, MyBatis can be inferred parameters passed in statement
resultType Type (fully qualified name or an alias) return after the SQL statement executed. If the type is a collection, return the set of types of elements may be used one or resultMap return resultType
resultMap It is set a reference map, for use with an element, or can use one resultType return resultMap
flushCache For setting in the local cache and secondary cache query whether the requirements before emptying after calling MyBatis SQL statements, the default value is false, if set to true, whenever SQL statement is invoked will clear the local cache and secondary cache
useCache Secondary cache start switch, the default value is true, which indicates the result of the query is stored in the secondary cache
timeout To set the timeout parameter in seconds (S), a timeout will throw an exception
fetchSize Gets the total number of records set
statementType Which tells MyBatis to use the JDBC Statement of work, the value is STATEMENT (Statement), PREPARED (PreparedStatement), CALLABLE (CallableStatement)
resultSetType It was meant for JDBC ResultSet interface, and its value can be set to FORWARD_ONLY (only allow access to forward), SCROLL_SENSITIVE (two-way scroll, but do not update), SCROLLJNSENSITIVE (two-way scroll, update)

Map interface using a plurality of transmission parameters

In the actual development, the SQL query often require multiple parameters, such as multi-criteria query. When passing multiple parameters, what type parameterType property value element is it? Map interface allows to pass with MyBatis plurality of key parameters.

Suppose there is data in the user interface implemented method of Chen male user information query function:

public List<MyUser> selectAllUser(Map<String,Object> param);

In this case, the mapper is passed to a Map object, use it to set a parameter corresponding to the SQL file, the SQL file corresponding to the code is as follows:

<!-- 查询陈姓男性用户信息 -->
<select id="selectAllUser" resultType="com.mybatis.po.MyUser">
    select * from user
    where uname like concat('%',#{u_name},'%')
    and usex = #{u_sex}
</select>

In the SQL file, parameter names u_name and u_sex is the Map key.

Passing multiple parameters using a Java Bean

Create a pojo: SeletUserParam

package com.pojo;

public class SeletUserParam {
    private String u_name;
    private String u_sex;
    // 此处省略setter和getter方法
}

Dao interface selectAllUser follows:

public List<MyUser> selectAllUser(SelectUserParam param);

SQL mapping file UserMapper.xml the "code is as follows:

<select id="selectAllUser" resultType="com.po.MyUser" parameterType="com.pojo.SeletUserParam">
   select * from user
   where uname like concat('%',#{u_name},'%')
   and usex=#{u_sex}
</select>

UserController "Query multiple users," the code is as follows:

SeletUserParam su = new SelectUserParam();
su.setU_name("陈");
su.setU_sex("男");
List<MyUser> list = userDao.selectAllUser(su);
for (MyUser myUser : list) {
   System.out.println(myUser);
}

In practice, selecting Map or select the Java Bean pass multiple parameters should be based on the actual situation, if few parameters, it is recommended to select Map; if many parameters, it is recommended to select Java Bean.

Guess you like

Origin blog.csdn.net/cold___play/article/details/102768355