1 使用map
<select id="selectRole" parameterType="map" resultType="RoleMap">SELECT id, roleName, noteFROM roleWHERE roleName LIKE Concat('%',#{roleName},'%')and note like Concat('%',#{note},'%')</select>
在接口中如下定義
List<Role> selectRole(Map map);
說明:這種方法簡單易用,弊端是業務關聯性不強,查看參數需要查看代碼,可讀性下降。
?
2 使用注解傳遞參數
xml文件中定義sql語句如下
<select id="findRoleByAnnotation" resultType="roleMap">SELECT id, roleName, noteFROM roleWHERE roleName LIKE Concat('%',#{roleName},'%')and note like Concat('%',#{note},'%')</select>
接口如下
List<Role> findRoleByAnnotation(@Param("roleName") String rolename, @Param("note") String note);
說明:通過@Param 提供的名稱mybatis就知道?#{roleName} 代表?rolename 。參數可讀性大大提高了。適用于參數數量較少的情況。如果參數數量過多,推薦使用javabean方式。
?
3 在參數過多的情況下,使用 javabean 傳遞參數
參數類RoleParam
public class RoleParam {private String roleName;private String note;public String getRoleName() {return roleName;}public void setRoleName(String roleName) {this.roleName = roleName;}public String getNote() {return note;}public void setNote(String note) {this.note = note;} }
xml文件查詢sql
<select id="findByJavaBean" resultType="roleMap" parameterType="com.huitong.service.command.RoleParam">SELECT id, roleName, noteFROM roleWHERE roleName LIKE Concat('%',#{roleName},'%')and note like Concat('%',#{note},'%')</select>
接口文件中
List<Role> findByJavaBean(RoleParam roleparam);
說明:當參數個數多于5個時,推薦使用javabean方式。
?