1 預設參數綁定
預設Mapper映射檔案中的變量名隻能是 #{arg0},#{arg1} ...或者是#{param1},#{param2} ...。其中arg變量名從0開始,param變量名從1開始。
1.1 UserMapper.java
/**
* 擷取使用者資訊
*
* @param username 使用者名
* @param password 變量名
* @return
*/
List<UserEntity> getUserList(String username,String password);
1.2 UserMapper.xml
第一種變量命名方式:
<select id="getUserList" resultType="com.entity.UserEntity">
SELECT *
from users
where username = #{arg0} and password=#{arg1}
</select>
第二種變量命名方式:
<select id="getUserList" resultType="com.entity.UserEntity">
SELECT *
from users
where username = #{param1} and password=#{param2}
</select>
2 @Param注解參數綁定
2.1 UserMapper.java
/**
* 擷取使用者資訊
*
* @param username 使用者名
* @param password 變量名
* @return
*/
List<UserEntity> getUserList(@Param("username") String username, @Param("password") String password);
2.2 UserMapper.xml
<select id="getUserList" resultType="com.entity.UserEntity">
SELECT *
from users
where username = #{username} and password=#{password}
</select>
3 parameterType參數綁定
3.1 UserMapper.java
/**
* 擷取使用者資訊
*
* @param username 使用者名
* @param password 變量名
* @return
*/
List<UserEntity> getUserList(String username,String password);
3.2 UserMapper.xml
<select id="getUserList" resultType="com.entity.UserEntity" parameterType="java.util.HashMap">
SELECT *
from users
where username = #{username} and password=#{password}
</select>
4 自定義類參數綁定
4.1 UserEntity
package com.entity;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
/**
* 使用者資訊實體類
*/
@Data
@TableName("users")
public class UserEntity {
/**
* 使用者名
*/
@TableField("username")
@TableId
private String username;
/**
* 密碼
*/
@TableField("password")
private String password;
}
4.2 UserMapper.java
/**
* 擷取使用者資訊
*
* @param userEntity 查詢條件
* @return
*/
List<UserEntity> getUserList(UserEntity userEntity);
4.3 UserMapper.xml
<select id="getUserList" resultType="com.entity.UserEntity">
SELECT *
from users
where username = #{username} and password=#{password}
</select>